Пример работы с ePochta SMS на языке Go | ePochta SMS
image image

Отправка СМС:

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
  
import (
    "crypto/md5"
    "encoding/hex"
    "net/url"
    "bytes"
    "fmt"
    "sort"
   _ "log"
    "net/http"
    "strings"
    "encoding/json"
    "io/ioutil"
    "strconv"
    _ "os"
)
 
// config
const public_key = "your public key"
const private_key = "your private key"
 
/**
 *
 */
func main() {
 
    // send sms
    // log.Println(sendSMS("SmSender", "38093363795", "bla bla bla"))
 
    // var phones = [][]string{
    //    {"38093363795"},
    // }
 
    // send group sms
    // log.Println(sendSMSGroup("SmSender", phones, "bla bla bla", ""))
 
    // send group sms
    // log.Println(addAddressbook("test go", "goooooooo")) // {"result":{"addressbook_id":1482777}} <nil>
 
    // send group sms
    // log.Println(addPhoneToAddressBook(1482777, "380933637995", "")) // ok: {"result":{"phone_id":457180251}} <nil>; or error: {"error":"No addressbook","code":"213","result":"117"} <nil>
 
    // send group sms
    // log.Println(getUserBalance("USD")) // {"result":{"credits":6.113900599999999,"currency":"USD","balance_currency":0.3057}} <nil>
 
    // send group sms
    // log.Println(createCampaign("TeSender", "go campaign", 1482777, "", 0, 0, 0, "")) // {"result":{"id":118703412,"price":0.013300399011970358,"currency":"USD"}}
 
    // http.HandleFunc("/", handler)
    // http.ListenAndServe(":8082", nil)
}
 
func handler(w http.ResponseWriter, r *http.Request) {
}
 
/**
 *
 *
 */
func calcSum(params map[string]string, action string) string {
    action = strings.ToLower(action)
    params["key"] = public_key
    params["version"] = "3.0"
    params["action"] = action
    keys := make([]string, 0, len(params))
    for k := range params {
        keys = append(keys, k)
    }
    sort.Strings(keys)
    control_str := ""
    for _, k := range keys {
        control_str += params[k]
    }
    control_str += private_key
    // log.Printf("%s\n", control_str)
    hasher := md5.New()
    hasher.Write([]byte(control_str))
    control_sum := hex.EncodeToString(hasher.Sum(nil))
    return control_sum;
}
 
/**
 *
 *
 */
func sendRequest(form url.Values, action string) (string, error) {
    action = strings.ToLower(action)
    body := bytes.NewBufferString(form.Encode())
    url := fmt.Sprintf(api_url + "/" + action)
    result := ""
    response, err := http.Post(url, "application/x-www-form-urlencoded", body)
    if err != nil {
        return result, err
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            return result, err
        }
        return string(contents), nil
    }
}
 
/**
 *
 *
 *
 *
 */
func sendSMS(sender string, phone string, text string, datetime string) (string, error) {
    if datetime == "" {
        datetime = ""
    }
    action := "sendSMS"
    // datetime := ""
    sms_lifetime := "0"
    params := make(map[string]string)
    params["sender"] = sender
    params["text"] = text
    params["phone"] = phone
    params["datetime"] = datetime
    params["sms_lifetime"] = sms_lifetime
    control_sum := calcSum(params, action)
    form := url.Values{
        "key":          {public_key},
        "sum":          {control_sum},
        "sender":       {sender},
        "text":         {text},
        "phone":        {phone},
        "datetime":     {""},
        "sms_lifetime": {"0"},
    }
    return sendRequest(form, action)
}
 
/**
 *
 *
 *
 */
func sendSMSGroup(sender string, phones [][]string, text string, send_type string) (string, error) {
    action := "sendSMSGroup"
    datetime := ""
    if (send_type == "now") {
        datetime = ""
    }
    sms_lifetime := "0"
    batch := "0"
    batchinterval := "0"
    phonesJ, err := json.Marshal(phones)
    if err != nil {
        panic(err)
    }
    params := make(map[string]string)
    params["sender"] = sender
    params["text"] = text
    params["phones"] = string(phonesJ)
    params["datetime"] = datetime
    params["sms_lifetime"] = sms_lifetime
    params["batch"] = batch
    params["batchinterval"] = batchinterval
    control_sum := calcSum(params, action)
    form := url.Values{
        "key":          {public_key},
        "sum":          {control_sum},
        "text":         {text},
        "phones":       {string(phonesJ)},
        "sender":       {sender},
        "batch":    {batch},
        "datetime":     {datetime},
        "sms_lifetime": {sms_lifetime},
        "batchinterval":{batchinterval},
    }
    return sendRequest(form, action)
}
 
 /**
  * Add addressbook
  *
  * @param [string] name
  * @param [string] description
  * @return [map]
  */
func addAddressbook(name string, description string) (string, error) {
    action := "addAddressbook"
    params := make(map[string]string)
    params["name"] = name
    params["description"] = description
    control_sum := calcSum(params, action)
    form := url.Values{
        "name":       {name},
        "description":{description},
        "key":        {public_key},
        "sum":        {control_sum},
    }
    return sendRequest(form, action)
}
 
 /**
  * Add phone to addressbook
  *
  * @param [int] id_addressbook
  * @param [string] phone
  * @param [string] variables
  * @return [map]
  */
func addPhoneToAddressBook(id_addressbook int, phone string, variables string) (string, error) {
    action := "addPhoneToAddressBook"
    params := make(map[string]string)
    params["idAddressBook"] = strconv.Itoa(id_addressbook)
    params["phone"] = phone
    params["variables"] = variables
    control_sum := calcSum(params, action)
    form := url.Values{
        "idAddressBook":   {strconv.Itoa(id_addressbook)},
        "phone":            {phone},
        "variables":        {variables},
        "key":              {public_key},
        "sum":              {control_sum},
    }
    return sendRequest(form, action)
}
 
 /**
  * Get user balance
  *
  * @param [string] currency
  * @return [map]
  */
func getUserBalance(currency string) (string, error) { /*= "USD"*/
    action := "getUserBalance"
    params := make(map[string]string)
    params["currency"] = currency
    control_sum := calcSum(params, action)
    form := url.Values{
        "currency":  {currency},
        "key":       {public_key},
        "sum":       {control_sum},
    }
    return sendRequest(form, action)
}
 
/**
  * Create campaign
  *
  * @param [string] sender
  * @param [string] text
  * @param [int] list_id
  * @param [string] datetime
  * @param [int] batch
  * @param [int] batchinterval
  * @param [int] sms_lifetime
  * @param [string] control_phone
  * @return [map]
  */
func createCampaign(sender string, text string, list_id int, datetime string, batch int, batchinterval int, sms_lifetime int, control_phone string) (string, error) {
    action := "createCampaign"
    params := make(map[string]string)
    params["sender"] = sender
    params["text"] = text
    params["list_id"] = strconv.Itoa(list_id)
    params["datetime"] = datetime
    params["batch"] = strconv.Itoa(batch)
    params["batchinterval"] = strconv.Itoa(batchinterval)
    params["sms_lifetime"] = strconv.Itoa(sms_lifetime)
    params["control_phone"] = control_phone
    control_sum := calcSum(params, action)
    form := url.Values{
        "sender":       {sender},
        "text":         {text},
        "list_id":      {strconv.Itoa(list_id)},
        "datetime":     {datetime},
        "batch":        {strconv.Itoa(batch)},
        "batchinterval":{strconv.Itoa(batchinterval)},
        "sms_lifetime": {strconv.Itoa(sms_lifetime)},
        "control_phone":{control_phone},
        "key":          {public_key},
        "sum":          {control_sum},
    }
    return sendRequest(form, action)
}
</nil></nil></nil></nil>
vector
image

Нужна помощь?

Задайте вопрос специалисту технической поддержки