Repository navigation
Expand file tree
/
Copy pathsimpleSMSAPIBridge_TWEDX.go
More file actions
145 lines (126 loc) · 4.03 KB
/
Copy pathsimpleSMSAPIBridge_TWEDX.go
File metadata and controls
145 lines (126 loc) · 4.03 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Simple SMS API bridge code
//
// Alternative to using SMS API Bridge service from Melrose Labs (melroselabs.com)
//
// Code enables applications supporting Twilio Messaging API to switch to using Esendex Conversations API without changing existing code.
// Intended as example code that can be used as a starting point for more capable bridging between APIs.
//
// 24 Feb 2020 Mark Hay / Melrose Labs
//
// Melrose Labs
// melroselabs.com
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
const targetURL = "https://conversations.esendex.com/v1/messages"
const targetAPIKey = "YOUR_API_KEY"
const targetAccountReference = "EX000000"
// Define structure for the Format T response
type FormatTResponse struct {
Sid string `json:"sid"`
}
// Define structures for the Format B request and response
type FormatBRequest struct {
Channel string `json:"channel"`
Metadata map[string]string `json:"metadata"`
RecipientVariables map[string]string `json:"recipientVariables"`
To string `json:"to"`
Body struct {
Text struct {
Value string `json:"value"`
} `json:"text"`
} `json:"body"`
From string `json:"from"`
CharacterSet string `json:"characterSet"`
Expiry int `json:"expiry"`
}
type FormatBResponse struct {
Id string `json:"id"`
}
func smsSendHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Parse form data
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Accessing the form values
to := r.FormValue("To")
from := r.FormValue("From")
body := r.FormValue("Body")
// Transform Format T request to Format B
formatB := FormatBRequest{
Channel: "sms",
To: to,
Body: struct {
Text struct {
Value string `json:"value"`
} `json:"text"`
}{
Text: struct {
Value string `json:"value"`
}{
Value: body,
},
},
From: from,
CharacterSet: "GSM",
Expiry: 30,
}
requestBody, err := json.Marshal(formatB)
if err != nil {
log.Print("Failed to marshal")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Send the Format B request to the external service
client := &http.Client{}
request, err := http.NewRequest("POST", targetURL, bytes.NewBuffer(requestBody))
if err != nil {
log.Print("Failed to call target URL")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
request.Header.Set("Content-Type", "application/json")
// Add authorization header if required
request.Header.Set("Account-Reference", targetAccountReference)
request.Header.Set("Api-Key", targetAPIKey)
response, err := client.Do(request)
if err != nil {
log.Print("Failed to send request")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer response.Body.Close()
bodyBytes, err := ioutil.ReadAll(response.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var respB FormatBResponse
if err := json.Unmarshal(bodyBytes, &respB); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Transform Format B response to Format A response
respT := FormatTResponse{
Sid: respB.Id,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(respT)
}
func main() {
http.HandleFunc("/2010-04-01/Accounts/", smsSendHandler)
log.Println("Server starting on port 8081...")
if err := http.ListenAndServe(":8081", nil); err != nil {
log.Fatal(err)
}
}