mirror of
https://github.com/nikoksr/notify.git
synced 2025-02-11 13:15:34 +02:00
feat(service) Pushbullet SMS - added new service and usage documentation to pushbullet package
This commit is contained in:
parent
3ba5639feb
commit
609cc3c648
64
service/pushbullet/sms.go
Normal file
64
service/pushbullet/sms.go
Normal file
@ -0,0 +1,64 @@
|
||||
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
|
||||
}
|
@ -39,20 +39,61 @@ func main() {
|
||||
notifier.UseService(service)
|
||||
|
||||
// Send a message
|
||||
_ = notifier.Send(
|
||||
err = 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)
|
||||
// }
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user