mirror of
https://github.com/nikoksr/notify.git
synced 2025-01-20 02:59:56 +02:00
feat(service): Added Pushbullet service (tested)
This commit is contained in:
parent
e65524a334
commit
3ba5639feb
@ -66,6 +66,7 @@ _ = notifier.Send(
|
||||
- *Microsoft Teams*
|
||||
- *Slack*
|
||||
- *Telegram*
|
||||
- *Pushbullet*
|
||||
|
||||
## Roadmap <a id="roadmap"></a>
|
||||
|
||||
@ -80,6 +81,7 @@ _ = notifier.Send(
|
||||
- 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)
|
||||
- Logo: [MariaLetta/free-gophers-pack](https://github.com/MariaLetta/free-gophers-pack)
|
||||
- Pushbullet: [cschomburg/go-pushbullet](github.com/cschomburg/go-pushbullet)
|
||||
|
||||
## Author <a id="author"></a>
|
||||
|
||||
|
1
go.mod
1
go.mod
@ -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
2
go.sum
@ -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=
|
||||
|
67
service/pushbullet/pushbullet.go
Normal file
67
service/pushbullet/pushbullet.go
Normal file
@ -0,0 +1,67 @@
|
||||
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 Pushbulletdevice nicknames and adds them to the internal deviceNicknames list. The Send method will send
|
||||
// a given message to all those devices a matching one be registered.
|
||||
// We only add registered devices
|
||||
func (pb *Pushbullet) AddReceivers(deviceNicknames ...string) {
|
||||
|
||||
devices := []string{}
|
||||
for _, deviceNickname := range deviceNicknames {
|
||||
|
||||
_, err := pb.client.Device(deviceNickname)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
devices = append(devices, deviceNickname)
|
||||
}
|
||||
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
|
||||
}
|
58
service/pushbullet/usage.md
Normal file
58
service/pushbullet/usage.md
Normal file
@ -0,0 +1,58 @@
|
||||
# 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 pusbullet on any devices which are to recieve 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
|
||||
_ = notifier.Send(
|
||||
"Hello\n",
|
||||
"I am a bot written in Go!",
|
||||
)
|
||||
|
||||
// Code isn't working and need to debug? Use this code below:
|
||||
// x := notifier.Send(
|
||||
// "Hello:\n",
|
||||
// "I am a bot written in Go!",
|
||||
// )
|
||||
|
||||
// if x != nil {
|
||||
// fmt.Println(x)
|
||||
// }
|
||||
|
||||
}
|
||||
```
|
Loading…
x
Reference in New Issue
Block a user