Browse Source

Initial commit

Dmitry Yu Okunev 6 years ago
commit
b0c1dc84e0
3 changed files with 109 additions and 0 deletions
  1. 7 0
      Makefile
  2. 45 0
      main.go
  3. 57 0
      ssmsSuApi/api.go

+ 7 - 0
Makefile

@@ -0,0 +1,7 @@
+
+all:
+	go build -o sendsms-ssms_su main.go
+
+clean:
+	rm -f sendsms-ssms_su
+

+ 45 - 0
main.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	    "os"
+	    "fmt"
+	    "code.google.com/p/getopt"
+	api "devel.mephi.ru/dyokunev/go-ssms.su/ssmsSuApi"
+)
+
+
+func main() {
+	var err			error
+	var login		string
+	var password		string
+	var recipient		string
+	var message		string
+	var senderName		string
+
+	getopt.StringVar(&login,			'l',	"login"		)
+	getopt.StringVar(&password,			'p',	"password"	)
+	getopt.StringVar(&recipient,			't',	"recipient"	)
+	getopt.StringVar(&message,			'm',	"message"	)
+	getopt.StringVar(&senderName,			'f',	"sender-name"	)
+
+	getopt.Parse()
+	if (getopt.NArgs() > 0) {
+		getopt.Usage()
+		os.Exit(-2)
+	}
+
+	if (login == "" || password == "" || recipient == "" || message == "") {
+		getopt.Usage()
+		os.Exit(-2)
+	}
+
+	err, r := api.SendSms(login, password, recipient, message, senderName)
+	if (err != nil) {
+		fmt.Printf("Cannot send the SMS: %v\n", err.Error())
+		os.Exit(-1)
+	}
+
+	fmt.Printf("Error: %v; Response: %v\n", err, r);
+
+	os.Exit(0);
+}

+ 57 - 0
ssmsSuApi/api.go

@@ -0,0 +1,57 @@
+package ssmsSuApi
+
+import (
+	"fmt"
+//	"strings"
+	"net/url"
+	"net/http"
+	"encoding/json"
+)
+
+const (
+	API_SERVER_URL string = "http://api2.ssms.su/"
+)
+
+type ResponseResponseMsg struct {
+	Err_Code	string
+	Text		string
+	Type		string
+}
+
+type ResponseResponseData struct {
+	Id		int
+	Credits		float32
+	N_Raw_SMS	int
+	Sender_Name	string
+}
+
+type ResponseResponse struct {
+	Msg		ResponseResponseMsg
+	Data		ResponseResponseData
+}
+
+type Response struct {
+	Response ResponseResponse
+}
+
+func SendSms(login string, password string, recipient string, message string, senderName string) (error, Response) {
+	resp, err := http.PostForm(API_SERVER_URL, url.Values{"format":{"JSON"}, "method":{"push_msg"}, "email":{login}, "password":{password}, "sender_name":{senderName}, "phone":{recipient}, "text":{message}})
+
+	if (err != nil) {
+		return err, Response{};
+	}
+
+	defer resp.Body.Close()
+	decoder := json.NewDecoder(resp.Body)
+	//decoder := json.NewDecoder(strings.NewReader("{\"response\":{\"msg\":{\"err_code\":\"0\",\"text\":\"OK\",\"type\":\"message\"},\"data\":{\"id\":7021498,\"credits\":\"1.50\",\"n_raw_sms\":1,\"sender_name\":\"MEPHI\"}}}"))
+	var responseData Response;
+	decoder.Decode(&responseData)
+	// response example: {"response":{"msg":{"err_code":"0","text":"OK","type":"message"},"data":{"id":7021498,"credits":"1.50","n_raw_sms":1,"sender_name":"MEPHI"}}}
+
+	if (responseData.Response.Msg.Err_Code != "0") {
+		return fmt.Errorf("Incorrect request: %v: %v [%v]", responseData.Response.Msg.Err_Code, responseData.Response.Msg.Text, responseData.Response.Msg.Type), responseData
+	}
+
+	return nil, responseData
+}
+