viber

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2024 License: MIT Imports: 4 Imported by: 0

README

# Viber

## Prerequisites

### Create a Viber Bot

In order to use the Viber notification service, we'll need to create a new Viber bot [here](https://partners.viber.com/account/create-bot-account).

### Setting the webhook

After we have done with the bot setup, we'll need to have a webhook that will be used to receive callbacks from the Viber server otherwise we will not be able to send a message.

Please note that your webhook needs to be valid otherwise it will not work properly. You can read more details about the Viber webhook [here](https://developers.viber.com/docs/api/rest-bot-api/#webhooks) and about the callback [here](https://developers.viber.com/docs/api/rest-bot-api/#callbacks).

#### Tips: Easy setup for webhook

If you need to set up webhook easily like for example only for local testing, you can utilize [Google App Scripts](https://www.google.com/script/start/) and create a simple Web app from it. Here is the example script:

```javascript
function doPost(e) {
  const contents = JSON.parse(e.postData.contents)
  Logger.log(JSON.stringify(contents))
}
```

_In short, it will just receive the POST request, and log the content_.

Don't forget to deploy the script as a web app and share the access with anyone.

You'll get a URL like https://script.google.com/macros/s/xxx/exec and this URL will be your webhook URL.

## Usage

Here is an example use case on how you can use Viber:

```go
package main

import (
	"context"
	"log"

	"github.com/buugaaga/go-notify"
	"github.com/buugaaga/go-notify/service/viber"
)

const appKey = "your-viber-token"
const webhookURL = "https://webhook.com"
const senderName = "vibersofyana"

func main() {
	viberSvc := viber.New(appKey, senderName, "")

	err := viberSvc.SetWebhook(webhookURL) // this only needs to be called once
	if err != nil {
		log.Fatalf("set webhook to viber server failed: %v", err)
	}

	viberSvc.AddReceivers("receiver-viber-user-id") // can add as many as required
	notifier := notify.New()

	notifier.UseServices(viberSvc)
	if err := notifier.Send(context.Background(), "TEST", "Message using golang notifier library"); err != nil {
		log.Fatalf("notifier.Send() failed: %s", err.Error())
	}

	log.Println("Notification sent")
}
```

> ❗️**Viber is only allowing the bot to send the message to their subscriber**. Therefore, in order to send the notification, we need to make sure that the receiver already subscribed to the bot. Read more details here: https://developers.viber.com/docs/api/rest-bot-api/#send-message

## Attachment

- [Viber API Documentation](https://developers.viber.com/docs/)

Documentation

Overview

Package viber provides a service for sending messages to viber.

Usage:

package main

import (
    "context"
    "log"

    "github.com/buugaaga/go-notify"
    "github.com/buugaaga/go-notify/service/viber"
)

const appKey = "your-viber-token"
const webhookURL = "https://webhook.com"
const senderName = "vibersofyana"

func main() {
    viberSvc := viber.New(appKey, senderName, "")

    err := viberSvc.SetWebhook(webhookURL) // this only needs to be called once
    if err != nil {
        log.Fatalf("set webhook to viber server failed: %v", err)
    }

    viberSvc.AddReceivers("receiver-viber-user-id") // can add as many as required
    notifier := notify.New()

    notifier.UseServices(viberSvc)
    if err := notifier.Send(context.Background(), "TEST", "Message using golang notifier library"); err != nil {
        log.Fatalf("notifier.Send() failed: %s", err.Error())
    }

    log.Println("Notification sent")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Viber

type Viber struct {
	Client            viberClient
	SubscribedUserIDs []string
}

Viber struct holds necessary fields to communicate with Viber API

func New

func New(appKey, senderName, senderAvatar string) *Viber

New returns a new instance of Viber notification service

func (*Viber) AddReceivers

func (v *Viber) AddReceivers(subscribedUserIDs ...string)

AddReceivers receives subscribed user IDs then add them to internal receivers list

func (*Viber) Send

func (v *Viber) Send(ctx context.Context, subject, message string) error

Send takes a message subject and a message body and sends them to all previously set userIds

func (*Viber) SetWebhook

func (v *Viber) SetWebhook(webhookURL string) error

SetWebhook receives a URL that will we used as a webhook URL for Viber

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL