-
Notifications
You must be signed in to change notification settings - Fork 0
/
bird_client.go
65 lines (52 loc) · 1.67 KB
/
bird_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package msg
import (
"log"
mb "github.com/messagebird/go-rest-api"
)
// Birdman or simply aviculturist. Knows how to deal with MessageBird.com API.
// Wraps original MessageBird client in order to avoid coupling with vendor specific structs in packages that will consume this functionality.
type Birdman struct {
mbClient *mb.Client
msgChan chan Request
}
// NewMsgBirdClient creates instance of Birdman
func NewMsgBirdClient(token string, mc chan Request) *Birdman {
c := &Birdman{
mbClient: mb.New(token),
msgChan: mc,
}
balance, err := c.mbClient.Balance()
if err != nil {
log.Fatal("Unable to get balance")
}
if balance.Amount < 1 {
log.Fatal("Balance is too low for proper voting campaign")
}
log.Printf("Current MessageBird.com balance type: %q, amount: %f\n", balance.Type, balance.Amount)
return c
}
// SendText sends SMS from sender to a recipient with provided text.
func (c *Birdman) SendText(sender, recipient, text string) error {
m, err := c.mbClient.NewMessage(sender, []string{recipient}, text, &mb.MessageParams{})
if err != nil {
if err == mb.ErrResponse {
for _, mbError := range m.Errors {
log.Printf("Error: %#v\n", mbError)
}
}
return err
}
return nil
}
// Lookup is used to get detailes about MSISDN. We need only country code.
func (c *Birdman) Lookup(msisdn string) (string, error) {
lr, err := c.mbClient.Lookup(msisdn, &mb.LookupParams{})
if err != nil {
return "", err
}
return lr.CountryCode, nil
}
// RequestSMS adds SMS request to the channel, it will be send sometime in the future.
func (c *Birdman) RequestSMS(sender, recipient, text string) {
c.msgChan <- Request{Sender: sender, Recipient: recipient, Text: text}
}