-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7624ce3
commit 8aa5c9d
Showing
9 changed files
with
362 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.