From 20e4b8ee3a13f76f747b3c865b3b0adb17391070 Mon Sep 17 00:00:00 2001 From: kha7iq Date: Mon, 3 May 2021 02:08:24 +0800 Subject: [PATCH] feat: add new service twillio --- go.mod | 1 + go.sum | 13 +++++ main.go | 3 ++ service/twillio/twillio.go | 102 +++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 service/twillio/twillio.go diff --git a/go.mod b/go.mod index 92ba9aa..5bb0762 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require ( github.com/gregdel/pushover v0.0.0-20210216095829-2131362cb888 github.com/nikoksr/notify v0.15.0 github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5 // indirect github.com/urfave/cli/v2 v2.3.0 ) diff --git a/go.sum b/go.sum index 42a79a1..728a3da 100644 --- a/go.sum +++ b/go.sum @@ -41,6 +41,8 @@ github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14y github.com/dghubble/go-twitter v0.0.0-20201011215211-4b180d0cc78d/go.mod h1:xfg4uS5LEzOj8PgZV7SQYRHbG7jPUnelEiaAVJxmhJE= github.com/dghubble/oauth1 v0.7.0/go.mod h1:8pFdfPkv/jr8mkChVbNVuJ0suiHe278BtWI4Tk1ujxk= github.com/dghubble/sling v1.3.0/go.mod h1:XXShWaBWKzNLhu2OxikSNFrlsvowtz4kyRuXUG7oQKY= +github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/facebookgo/ensure v0.0.0-20160127193407-b4ab57deab51/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64= @@ -71,11 +73,14 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gopackage/ddp v0.0.0-20170117053602-652027933df4 h1:4EZlYQIiyecYJlUbVkFXCXHz1QPhVXcHnQKAzBTPfQo= github.com/gopackage/ddp v0.0.0-20170117053602-652027933df4/go.mod h1:lEO7XoHJ/xNRBCxrn4h/CEB67h0kW1B0t4ooP2yrjUA= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= +github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= @@ -88,6 +93,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA= github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= @@ -124,7 +131,10 @@ github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sendgrid/rest v2.6.3+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE= github.com/sendgrid/sendgrid-go v3.8.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8= +github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5 h1:76NN4jha0iT2Qwfth8Xf8q2LlQEG7jiZ86dFDKHN9l8= +github.com/sfreiberg/gotwilio v0.0.0-20201211181435-c426a3710ab5/go.mod h1:dhtsjtHOWmTLjCOyNloce1diOIs9H1mvVmcOG7qmZUc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/skip2/go-qrcode v0.0.0-20190110000554-dc11ecdae0a9/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= @@ -133,6 +143,7 @@ github.com/slack-go/slack v0.8.1/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs github.com/sony/sonyflake v1.0.0 h1:MpU6Ro7tfXwgn2l5eluf9xQvQJDROTBImNCfRXn/YeM= github.com/sony/sonyflake v1.0.0/go.mod h1:Jv3cfhf/UFtolOTTRd3q4Nl6ENqM+KfyZ5PseKfZGF4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -176,6 +187,8 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index a64d89c..3a79a81 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,8 @@ import ( "log" "os" + "github.com/kha7iq/pingme/service/twillio" + "github.com/kha7iq/pingme/service/discord" "github.com/kha7iq/pingme/service/email" "github.com/kha7iq/pingme/service/mattermost" @@ -41,6 +43,7 @@ RocketChat, Discord, Pushover, Mattermost, Pushbullet, Microsoft Teams and email email.Send(), mattermost.Send(), pushbullet.Send(), + twillio.Send(), } err := app.Run(os.Args) diff --git a/service/twillio/twillio.go b/service/twillio/twillio.go new file mode 100644 index 0000000..066024c --- /dev/null +++ b/service/twillio/twillio.go @@ -0,0 +1,102 @@ +package twillio + +import ( + "log" + "strings" + + "github.com/kha7iq/pingme/service/helpers" + "github.com/sfreiberg/gotwilio" + "github.com/urfave/cli/v2" +) + +// Twillio struct holds data parsed via flags for the service +type Twillio struct { + Title string + Token string + AccountSid string + Sender string + Receiver string + Message string +} + +// Send parse values from *cli.context and return *cli.Command +// and send messages to target numbers. +// If multiple receivers are provided then the string is split with "," separator and +// message is sent to each number. +func Send() *cli.Command { + var twillioOpts Twillio + return &cli.Command{ + Name: "twillio", + Usage: "Send sms via twillio", + UsageText: "pingme twillio --token 'tokenabc' --account 'sid123' " + + "--sender '+140001442' --receiver '+140001442'' --msg 'some message'", + Description: `Twillio provides ability to send sms to multiple numbers. +You can specify multiple receivers by separating the value with a comma.`, + Flags: []cli.Flag{ + &cli.StringFlag{ + Destination: &twillioOpts.Token, + Name: "token", + Aliases: []string{"t"}, + Required: true, + Usage: "Auth token for twillio account.", + EnvVars: []string{"TWILLIO_TOKEN"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.AccountSid, + Name: "account", + Required: true, + Aliases: []string{"a"}, + Usage: "Twillio account sid", + EnvVars: []string{"TWILLIO_ACCOUNT_SID"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Message, + Name: "msg", + Aliases: []string{"m"}, + Usage: "Message content.", + EnvVars: []string{"TWILLIO_MESSAGE"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Title, + Name: "title", + Usage: "Title of the message.", + EnvVars: []string{"TWILLIO_TITLE"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Sender, + Name: "sender", + Aliases: []string{"s"}, + Usage: "Sender's phone number", + EnvVars: []string{"TWILLIO_SENDER"}, + }, + &cli.StringFlag{ + Destination: &twillioOpts.Receiver, + Name: "receiver", + Aliases: []string{"r"}, + Usage: "Receiver's phone number", + EnvVars: []string{"TWILLIO_RECEIVER"}, + }, + }, + Action: func(ctx *cli.Context) error { + client := gotwilio.NewTwilioClient(twillioOpts.AccountSid, twillioOpts.Token) + fullMessage := twillioOpts.Title + "\n" + twillioOpts.Message + + numbers := strings.Split(twillioOpts.Receiver, ",") + for _, v := range numbers { + if len(v) == 0 { + return helpers.ErrChannel + } + + _, exception, err := client.SendSMS(twillioOpts.Sender, twillioOpts.Receiver, fullMessage, "", "") + if err != nil { + return err + } + if exception != nil { + return exception + } + } + log.Println("Successfully sent!") + return nil + }, + } +}