Skip to content

Instantly share code, notes, and snippets.

@dtolb
Last active May 1, 2020 19:46
Show Gist options
  • Save dtolb/dc5f6abef49a235994699e83eeb3cd1b to your computer and use it in GitHub Desktop.
Save dtolb/dc5f6abef49a235994699e83eeb3cd1b to your computer and use it in GitHub Desktop.
Send Message in Go
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"log"
"os"
"io"
)
// CreateMessageDataV2 struct
type CreateMessageDataV2 struct {
From string `json:"from,omitempty"`
To interface{} `json:"to,omitempty"`
Text string `json:"text,omitempty"`
Media []string `json:"media,omitempty"`
ApplicationID string `json:"applicationId,omitempty"`
Tag string `json:"tag,omitempty"`
}
func main() {
messaging_token := os.Getenv("BANDWIDTH_MESSAGING_TOKEN")
messaging_secret := os.Getenv("BANDWIDTH_MESSAGING_SECRET")
messaging_application_id := os.Getenv("BANDWIDTH_MESSAGING_APPLICATION_ID")
account_id := os.Getenv("BANDWIDTH_ACCOUNT_ID")
messaging_uri := fmt.Sprintf("https://messaging.bandwidth.com/api/v2/users/%s/messages", account_id)
method := "POST"
body := &CreateMessageDataV2{
ApplicationID: messaging_application_id,
From: "+",
To: "+",
Text: "Hi",
Tag: "test message",
}
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(body)
req, _ := http.NewRequest(method, messaging_uri, buf)
req.Header.Add("Content-Type", "application/json")
req.SetBasicAuth(messaging_token, messaging_secret)
client := &http.Client {}
res, e := client.Do(req)
if e != nil {
log.Fatal(e)
}
defer res.Body.Close()
io.Copy(os.Stdout, res.Body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment