Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Created July 21, 2022 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chtzvt/aa4005c751fcbec14e82fb88055e9a8a to your computer and use it in GitHub Desktop.
Save chtzvt/aa4005c751fcbec14e82fb88055e9a8a to your computer and use it in GitHub Desktop.
package main
import (
"sync"
"net/http"
"net/url"
"strings"
"time"
"fmt"
"io/ioutil"
)
var twSid string = ""
var twTo string = "+11111111111"
var twAuth string = ""
var twFrom [1]string = [1]string{"+10000000000"}
var message string = "My message."
func main() {
wg := &sync.WaitGroup{}
fmt.Printf("Running.\n")
for _, number := range twFrom {
fmt.Printf("\nNumber: %s\n", number)
go (func(wg *sync.WaitGroup, from, to string) {
wg.Add(1)
fmt.Printf("Status: %d", sendSMS(from, to, message))
wg.Done()
})(wg, number, twTo)
}
time.Sleep(30 * time.Second)
wg.Wait()
}
func sendSMS(sender, recipient, message string) int {
httpClient := &http.Client{}
httpClient.Timeout = 30 * time.Second
apiUrl := strings.Join([]string{"https://api.twilio.com/2010-04-01/Accounts/", twSid, "/Messages.json"}, "")
v := url.Values{}
v.Set("To", recipient)
v.Set("From", sender)
v.Set("Body", message)
payload := *strings.NewReader(v.Encode())
req, _ := http.NewRequest("POST", apiUrl, &payload)
req.SetBasicAuth(twSid, twAuth)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
if res, err := httpClient.Do(req); err != nil {
return -1
} else {
body, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
fmt.Printf("\nCode: %d\n", res.StatusCode)
fmt.Printf("Body: %s\n\n", body)
return res.StatusCode
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment