forked from kevinburke/twilio-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
104 lines (89 loc) · 2.75 KB
/
example_test.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package twilio_test
import (
"context"
"fmt"
"log"
"net/url"
"time"
"github.com/kevinburke/rest/resterror"
twilio "github.com/kevinburke/twilio-go"
)
var callURL, _ = url.Parse("https://kev.inburke.com/zombo/zombocom.mp3")
var pdfURL, _ = url.Parse("https://kev.inburke.com/foo.pdf")
func Example() {
client := twilio.NewClient("AC123", "123", nil)
// Send a SMS
msg, _ := client.Messages.SendMessage("+14105551234", "+14105556789", "Sent via go :) ✓", nil)
fmt.Println(msg.Sid, msg.FriendlyPrice())
// Make a call
call, _ := client.Calls.MakeCall("+14105551234", "+14105556789", callURL)
fmt.Println(call.Sid, call.FriendlyPrice())
_, err := client.IncomingNumbers.BuyNumber("+1badnumber")
// Twilio API errors are converted to resterror.Error types
if err != nil {
restErr, ok := err.(*resterror.Error)
if ok {
fmt.Println(restErr.Title)
fmt.Println(restErr.Type)
}
}
// Find all calls from a number
data := url.Values{"From": []string{"+14105551234"}}
iterator := client.Calls.GetPageIterator(data)
ctx, cancel := context.WithTimeout(context.TODO(), 1*time.Minute)
defer cancel()
for {
page, err := iterator.Next(ctx)
if err == twilio.NoMoreResults {
break
}
for _, call := range page.Calls {
fmt.Println(call.Sid, call.To)
}
}
}
func ExampleCallService_GetCallsInRange() {
// Get all calls between 10:34:00 Oct 26 and 19:25:59 Oct 27, NYC time.
nyc, _ := time.LoadLocation("America/New_York")
start := time.Date(2016, 10, 26, 22, 34, 00, 00, nyc)
end := time.Date(2016, 10, 27, 19, 25, 59, 00, nyc)
client := twilio.NewClient("AC123", "123", nil)
iter := client.Calls.GetCallsInRange(start, end, url.Values{})
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancel()
for {
page, err := iter.Next(ctx)
if err == twilio.NoMoreResults {
break
}
if err != nil {
log.Fatal(err)
}
for i, call := range page.Calls {
fmt.Printf("%d: %s (%s)", i, call.Sid, call.DateCreated.Time)
}
}
}
func ExampleClient_UseSecretKey() {
client := twilio.NewClient("AC123", "123", nil)
client.UseSecretKey("SK123")
client.Messages.SendMessage("123", "456", "Sending with secret key...", nil)
}
func ExampleFaxService_SendFax() {
faxer := twilio.NewFaxClient("AC123", "123", nil)
faxer.Faxes.SendFax("123", "456", pdfURL)
}
func ExampleFaxService_Cancel() {
faxer := twilio.NewFaxClient("AC123", "123", nil)
faxer.Faxes.Cancel("FX123")
}
func ExampleFax() {
faxer := twilio.NewFaxClient("AC123", "123", nil)
fax, _ := faxer.Faxes.Get(context.TODO(), "FX123")
fmt.Print(fax.Sid)
}
func ExampleNewTaskRouterClient() {
client := twilio.NewTaskRouterClient("AC123", "123", nil)
data := url.Values{"FriendlyName": []string{"On Call"}}
client.Workspace("WS123").Activities.Create(context.TODO(), data)
}