Пример работы с SMS шлюзом на языке Go

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"
const api_url = "http://api.atompark.com/api/sms/3.0/"

/**
 *
 */
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}} 

    // send group sms
    // log.Println(addPhoneToAddressBook(1482777, "380933637995", "")) // ok: {"result":{"phone_id":457180251}} ; or error: {"error":"No addressbook","code":"213","result":"117"} 

    // send group sms
    // log.Println(getUserBalance("USD")) // {"result":{"credits":6.113900599999999,"currency":"USD","balance_currency":0.3057}} 

    // 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)
}

Есть вопрос?

Дарья


0 (800) 21 01 91;
+38 (093) 672-52-57
+38 (095) 446-56-21
skype:dkepochta
support@epochta.com.ua