1
0
mirror of https://github.com/nikoksr/notify.git synced 2025-03-29 21:46:58 +02:00

Merge branch 'main' into build/add-basic-ci

This commit is contained in:
Niko Köser 2021-02-02 18:34:23 +01:00 committed by GitHub
commit 49cf64c8c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 214 additions and 1 deletions

View File

@ -66,6 +66,7 @@ _ = notifier.Send(
- *Microsoft Teams*
- *Slack*
- *Telegram*
- *Pushbullet*
## Roadmap <a id="roadmap"></a>
@ -79,6 +80,7 @@ _ = notifier.Send(
- Microsoft Teams support: [atc0005/go-teams-notify](https://github.com/atc0005/go-teams-notify)
- Slack support: [slack-go/slack](https://github.com/slack-go/slack)
- Telegram support: [go-telegram-bot-api/telegram-bot-api](https://github.com/go-telegram-bot-api/telegram-bot-api)
- Pushbullet support: [cschomburg/go-pushbullet](https://github.com/cschomburg/go-pushbullet)
- Logo: [MariaLetta/free-gophers-pack](https://github.com/MariaLetta/free-gophers-pack)
## Author <a id="author"></a>

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.15
require (
github.com/atc0005/go-teams-notify/v2 v2.4.2
github.com/bwmarrin/discordgo v0.23.1
github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/pkg/errors v0.9.1

2
go.sum
View File

@ -2,6 +2,8 @@ github.com/atc0005/go-teams-notify/v2 v2.4.2 h1:3KQ8e8LN4kwuWWHpnCNTXA15JdLRaNWc
github.com/atc0005/go-teams-notify/v2 v2.4.2/go.mod h1:BSlh1HBcgWcGoNM3Abm36WMPcj+k8Wf0ZLZx6lBx2qk=
github.com/bwmarrin/discordgo v0.23.1 h1:xlK4/69bpl/VSoCYaKe3BOc9j1HkNopoRdCppRYu8dk=
github.com/bwmarrin/discordgo v0.23.1/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb h1:7X9nrm+LNWdxzQOiCjy0G51rNUxbH35IDHCjAMvogyM=
github.com/cschomburg/go-pushbullet v0.0.0-20171206132031-67759df45fbb/go.mod h1:RfQ9wji3fjcSEsQ+uFCtIh3+BXgcZum8Kt3JxvzYzlk=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -0,0 +1,52 @@
package pushbullet
import (
"github.com/cschomburg/go-pushbullet"
"github.com/pkg/errors"
)
// Pushbullet struct holds necessary data to communicate with the Pushbullet API.
type Pushbullet struct {
client *pushbullet.Client
deviceNicknames []string
}
// New returns a new instance of a Pushbullet notification service.
// For more information about Pushbullet api token:
// -> https://docs.pushbullet.com/#api-overview
func New(apiToken string) *Pushbullet {
client := pushbullet.New(apiToken)
pb := &Pushbullet{
client: client,
deviceNicknames: []string{},
}
return pb
}
// AddReceivers takes Pushbullet device nicknames and adds them to the internal deviceNicknames list.
// The Send method will send a given message to all those devices.
func (pb *Pushbullet) AddReceivers(deviceNicknames ...string) {
pb.deviceNicknames = append(pb.deviceNicknames, deviceNicknames...)
}
// Send takes a message subject and a message body and sends them to all valid devices.
// you will need Pushbullet installed on the relevant devices
// (android, chrome, firefox, windows)
// see https://www.pushbullet.com/apps
func (pb Pushbullet) Send(subject, message string) error {
for _, deviceNickname := range pb.deviceNicknames {
dev, err := pb.client.Device(deviceNickname)
if err != nil {
return errors.Wrapf(err, "failed to find Pushbullet device with nickname '%s'", deviceNickname)
}
err = dev.PushNote(subject, message)
if err != nil {
return errors.Wrapf(err, "failed to send message to Pushbullet device with nickname '%s'", deviceNickname)
}
}
return nil
}

61
service/pushbullet/sms.go Normal file
View File

@ -0,0 +1,61 @@
package pushbullet
import (
"github.com/cschomburg/go-pushbullet"
"github.com/pkg/errors"
)
// SMS struct holds necessary data to communicate with the Pushbullet SMS API.
type SMS struct {
client *pushbullet.Client
deviceIdentifier string
phoneNumbers []string
}
// NewSMS returns a new instance of a SMS notification service
// tied to an SMS capable device. deviceNickname is the
// Pushbullet nickname of the sms capable device from which messages are sent.
// (https://help.pushbullet.com/articles/how-do-i-send-text-messages-from-my-computer/).
// For more information about Pushbullet api token:
// -> https://docs.pushbullet.com/#api-overview
func NewSMS(apiToken string, deviceNickname string) (*SMS, error) {
client := pushbullet.New(apiToken)
dev, err := client.Device(deviceNickname)
if err != nil {
return nil, errors.Wrapf(err, "failed to locate Pushbullet device with nickname '%s'", deviceNickname)
}
sms := &SMS{
client: client,
deviceIdentifier: dev.Iden,
phoneNumbers: []string{},
}
return sms, nil
}
// AddReceivers takes phone numbers and adds them to the internal phoneNumbers list. The Send method will send
// a given message to all registered phone numbers.
func (sms *SMS) AddReceivers(phoneNumbers ...string) {
sms.phoneNumbers = append(sms.phoneNumbers, phoneNumbers...)
}
// Send takes a message subject and a message body and sends them to all phone numbers.
// see https://help.pushbullet.com/articles/how-do-i-send-text-messages-from-my-computer/
func (sms SMS) Send(subject, message string) error {
fullMessage := subject + "\n" + message // Treating subject as message title
user, err := sms.client.Me()
if err != nil {
return errors.Wrapf(err, "failed to find valid pushbullet user")
}
for _, phoneNumber := range sms.phoneNumbers {
err = sms.client.PushSMS(user.Iden, sms.deviceIdentifier, phoneNumber, fullMessage)
if err != nil {
return errors.Wrapf(err, "failed to send SMS message to %s via Pushbullet", phoneNumber)
}
}
return nil
}

View File

@ -0,0 +1,96 @@
# Pushbullet Usage
Ensure that you have already navigated to your GOPATH and installed the following packages:
* `go get -u github.com/nikoksr/notify`
* `go get github.com/cschomburg/go-pushbullet` - You might need this one too
## Steps for Pushbullet App
These are general and very high level instructions
1. Create a Pushbullet account
2. Download Pushbullet on any devices which are to receive notifications
3. Copy your *Access Token* for usage below form https://www.pushbullet.com/#settings
4. Copy the *Device Nickname* of the device you want to post a message to. See https://www.pushbullet.com/#settings/devices
## Sample Code
```go
package main
import (
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/pushbullet"
)
func main() {
notifier := notify.New()
// Provide your Access Token
service := pushbullet.New("AccessToken")
// Passing a device nickname as receiver for our messages.
service.AddReceivers("DeviceNickname")
// Tell our notifier to use the Pushbullet service. You can repeat the above process
// for as many services as you like and just tell the notifier to use them.
notifier.UseService(service)
// Send a message
err = notifier.Send(
"Hello\n",
"I am a bot written in Go!",
)
if err != nil {
panic(err)
}
}
```
# Steps for Pushbullet SMS
1. Follow the above instructions, ensuring that Pushbullet is installed on an Android device.
2. Enable 'SMS Sync' is turned on (under the SMS tab on the Android app)
## Sample Code
```go
package main
import (
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/pushbullet"
)
func main() {
notifier := notify.New()
// Provide your Access Token and Pushbullet nickname for your mobile device
service, err := pushbullet.NewSMS("AccessToken", "PhoneNickname")
if err != nil {
panic(err)
}
// Passing a phone number as receiver for our messages.
service.AddReceivers("PhoneNumber")
// Tell our notifier to use the Pushbullet service. You can repeat the above process
// for as many services as you like and just tell the notifier to use them.
notifier.UseService(service)
// Send a message
err = notifier.Send(
"Hello\n",
"I am a bot written in Go!",
)
if err != nil {
panic(err)
}
}
```

1
use.go
View File

@ -5,7 +5,6 @@ func (n *Notify) useService(service Notifier) {
if service == nil {
return
}
n.notifiers = append(n.notifiers, service)
}