Last active
June 17, 2023 01:19
-
-
Save rickyrobinett/10918615 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"net/url" | |
"fmt" | |
"strings" | |
"io/ioutil" | |
"encoding/json" | |
) | |
func main() { | |
// Set initial variables | |
accountSid := "ACxxxxxxxxxx" | |
authToken := "xxxxxxxxxxxx" | |
urlStr := "https://api.twilio.com/2010-04-01/Accounts/" + accountSid + "/Messages.json" | |
// Build out the data for our message | |
v := url.Values{} | |
v.Set("To","YOUR_PHONE_NUMBER") | |
v.Set("From","YOUR_TWILIO_NUMBER") | |
v.Set("Body","Brooklyn's in the house!") | |
rb := *strings.NewReader(v.Encode()) | |
// Create client | |
client := &http.Client{} | |
req, _ := http.NewRequest("POST", urlStr, &rb) | |
req.SetBasicAuth(accountSid, authToken) | |
req.Header.Add("Accept", "application/json") | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
// Make request | |
resp, _ := client.Do(req) | |
if( resp.StatusCode >= 200 && resp.StatusCode < 300 ) { | |
var data map[string]interface{} | |
bodyBytes, _ := ioutil.ReadAll(resp.Body) | |
err := json.Unmarshal(bodyBytes, &data) | |
if( err == nil ) { | |
fmt.Println(data["sid"]) | |
} | |
} else { | |
fmt.Println(resp.Status); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment