Skip to content

Commit

Permalink
feat(service): Add Viber (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
irfansofyana committed Oct 25, 2022
1 parent 7624ce3 commit 8aa5c9d
Show file tree
Hide file tree
Showing 9 changed files with 362 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Yes, please! Contributions of all kinds are very welcome! Feel free to check our
| [TextMagic](https://www.textmagic.com) | [service/textmagic](service/textmagic) | [textmagic/textmagic-rest-go-v2](https://github.com/textmagic/textmagic-rest-go-v2) | :heavy_check_mark: |
| [Twilio](https://www.twilio.com/) | [service/twilio](service/twilio) | [kevinburke/twilio-go](https://github.com/kevinburke/twilio-go) | :heavy_check_mark: |
| [Twitter](https://twitter.com) | [service/twitter](service/twitter) | [dghubble/go-twitter](https://github.com/dghubble/go-twitter) | :heavy_check_mark: |
| [Viber](https://www.viber.com) | [service/viber](service/viber) | [mileusna/viber](https://github.com/mileusna/viber) | :heavy_check_mark: |
| [WeChat](https://www.wechat.com) | [service/wechat](service/wechat) | [silenceper/wechat](https://github.com/silenceper/wechat) | :heavy_check_mark: |
| [WhatsApp](https://www.whatsapp.com) | [service/whatsapp](service/whatsapp) | [Rhymen/go-whatsapp](https://github.com/Rhymen/go-whatsapp) | :x: |

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/kevinburke/go-types v0.0.0-20210723172823-2deba1f80ba7 // indirect
github.com/kevinburke/rest v0.0.0-20210506044642-5611499aa33c // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mileusna/viber v1.0.1
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ github.com/line/line-bot-sdk-go v7.8.0+incompatible h1:Uf9/OxV0zCVfqyvwZPH8CrdiH
github.com/line/line-bot-sdk-go v7.8.0+incompatible/go.mod h1:0RjLjJEAU/3GIcHkC3av6O4jInAbt25nnZVmOFUgDBg=
github.com/mailgun/mailgun-go/v4 v4.8.1 h1:1+MdKakJuXnW2JJDbyPdO1ngAANOyHyVPxQvFF8Sq6c=
github.com/mailgun/mailgun-go/v4 v4.8.1/go.mod h1:FJlF9rI5cQT+mrwujtJjPMbIVy3Ebor9bKTVsJ0QU40=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mileusna/viber v1.0.1 h1:gWB6/lKoWYVxkH0Jb8jRnGIRZ/9DEM7RBZRJHRfdYWs=
github.com/mileusna/viber v1.0.1/go.mod h1:Pxu/iPMnYjnHgu+bEp3SiKWHWmlf/kDp/yOX8XUdYrQ=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down
6 changes: 2 additions & 4 deletions service/matrix/mock_matrix_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions service/viber/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 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/nikoksr/notify"
"github.com/nikoksr/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/)
39 changes: 39 additions & 0 deletions service/viber/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Package viber provides a service for sending messages to viber.
Usage:
package main
import (
"context"
"log"
"github.com/nikoksr/notify"
"github.com/nikoksr/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")
}
*/
package viber
70 changes: 70 additions & 0 deletions service/viber/mock_viber_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions service/viber/viber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package viber

import (
"context"

vb "github.com/mileusna/viber"
"github.com/pkg/errors"
)

//go:generate mockery --name=viberClient --output=. --case=underscore --inpackage
type viberClient interface {
SetWebhook(url string, eventTypes []string) (vb.WebhookResp, error)
SendTextMessage(receiver, msg string) (uint64, error)
}

// Compile-time check to ensure that vb.Viber implements the viberClient interface.
var _ viberClient = new(vb.Viber)

// Viber struct holds necessary fields to communicate with Viber API
type Viber struct {
Client viberClient
SubscribedUserIDs []string
}

// New returns a new instance of Viber notification service
func New(appKey, senderName, senderAvatar string) *Viber {
return &Viber{
Client: vb.New(appKey, senderName, senderAvatar),
SubscribedUserIDs: []string{},
}
}

// AddReceivers receives subscribed user IDs then add them to internal receivers list
func (v *Viber) AddReceivers(subscribedUserIDs ...string) {
v.SubscribedUserIDs = append(v.SubscribedUserIDs, subscribedUserIDs...)
}

// SetWebhook receives a URL that will we used as a webhook URL for Viber
func (v *Viber) SetWebhook(webhookURL string) error {
_, err := v.Client.SetWebhook(webhookURL, []string{})
return err
}

// Send takes a message subject and a message body and sends them to all previously set userIds
func (v *Viber) Send(ctx context.Context, subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title

for _, subscribedUserID := range v.SubscribedUserIDs {
select {
case <-ctx.Done():
return ctx.Err()
default:
_, err := v.Client.SendTextMessage(subscribedUserID, fullMessage)
if err != nil {
return errors.Wrapf(err, "failed to send message to User ID '%s'", subscribedUserID)
}
}
}

return nil
}
Loading

0 comments on commit 8aa5c9d

Please sign in to comment.