-
Notifications
You must be signed in to change notification settings - Fork 230
/
toolbox.go
95 lines (79 loc) · 2.62 KB
/
toolbox.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
package graphqlapp
import (
"context"
"fmt"
"net/url"
"github.com/target/goalert/notification"
"github.com/target/goalert/validation"
"github.com/target/goalert/graphql2"
"github.com/target/goalert/notification/twilio"
"github.com/target/goalert/permission"
"github.com/target/goalert/validation/validate"
"github.com/ttacon/libphonenumber"
)
type safeErr struct{ error }
func (safeErr) ClientError() bool { return true }
func (q *Query) DebugMessageStatus(ctx context.Context, input graphql2.DebugMessageStatusInput) (*graphql2.DebugMessageStatusInfo, error) {
err := permission.LimitCheckAny(ctx, permission.Admin)
if err != nil {
return nil, err
}
id, err := notification.ParseProviderMessageID(input.ProviderMessageID)
if err != nil {
return nil, validation.NewFieldError("ProviderMessageID", err.Error())
}
status, destType, err := q.NotificationManager.MessageStatus(ctx, id)
if err != nil {
return nil, err
}
return &graphql2.DebugMessageStatusInfo{
State: notificationStateFromSendResult(*status, q.FormatDestFunc(ctx, destType, status.SrcValue)),
}, nil
}
func (a *Mutation) DebugSendSms(ctx context.Context, input graphql2.DebugSendSMSInput) (*graphql2.DebugSendSMSInfo, error) {
err := permission.LimitCheckAny(ctx, permission.Admin)
if err != nil {
return nil, err
}
err = validate.Many(
validate.Phone("To", input.To),
validate.TwilioFromValue("From", input.From),
validate.Text("Body", input.Body, 1, 1000),
)
if err != nil {
return nil, err
}
msg, err := a.Twilio.SendSMS(ctx, input.To, input.Body, &twilio.SMSOptions{
FromNumber: input.From,
})
if err != nil {
return nil, safeErr{error: err}
}
return &graphql2.DebugSendSMSInfo{
ID: notification.ProviderMessageID{
ExternalID: msg.SID,
ProviderName: "Twilio-SMS",
}.String(),
ProviderURL: "https://www.twilio.com/console/sms/logs/" + url.PathEscape(msg.SID),
FromNumber: msg.From,
}, nil
}
func (a *Mutation) DebugCarrierInfo(ctx context.Context, input graphql2.DebugCarrierInfoInput) (*twilio.CarrierInfo, error) {
return a.Twilio.FetchCarrierInfo(ctx, input.Number)
}
func (a *Query) PhoneNumberInfo(ctx context.Context, number string) (*graphql2.PhoneNumberInfo, error) {
p, err := libphonenumber.Parse(number, "")
if err != nil {
return &graphql2.PhoneNumberInfo{
ID: number,
Error: err.Error(),
}, nil
}
return &graphql2.PhoneNumberInfo{
ID: number,
CountryCode: fmt.Sprintf("+%d", p.GetCountryCode()),
RegionCode: libphonenumber.GetRegionCodeForNumber(p),
Formatted: libphonenumber.Format(p, libphonenumber.INTERNATIONAL),
Valid: libphonenumber.IsValidNumber(p),
}, nil
}