Golang Alibaba Cloud Send SMS

Source: Internet
Author: User
Tags hmac sendmsg sha1

write the relatively rough, there is the wrong place to correct, not English, some of the variables with pinyin name, disturb

Package Sendmsgimport ("bytes" "Crypto/hmac" "Crypto/sha1" "Encoding/base64" "Encoding/json" "FMT" "          Io/ioutil "" Net/http "" Net/url "" StrConv "" Strings "" Time ") const (AK =" xxxxxxx "SK = "xxxxxxx" Requesturl = "http://dysmsapi.aliyuncs.com/?" signature= "actionstr =" &action=sendsms&format=json&outid=123 "datestr =" &Version=2017-05-25 "Msgtemplete =" &signatureversion=1.0&templatecode= template id&templateparam= "singStr =" &SignatureMe    thod=hmac-sha1&signaturenonce= "singname =" &regionid=cn-hangzhou&signname= "lianJIe =" & " Signlinajie = "get&%2f&")//simplemessage ... type simplemessage struct {message string ' JSON: ' message ' ' Cod  E string ' JSON: ' Code ' '}//messagecontent ... type messagecontent struct {code string ' JSON: ' Code ' '}//sendmsg Send SMS Func Sendmsg (phone, msgjson string) bool {body: = "Send msg" content: = URL. QUeryescape (Msgjson) signstring: = Setquerystr (phone, content, "skw493jei3j34ij34i") Singstr: = Signlinajie + URL. Queryescape (signstring) Sign: = Hmac4go (SINGSTR, Sk+lianjie) Client: = &http. client{} var req *http. Request req, err = http. Newrequest ("GET", Requesturl+url. Queryescape (sign) +lianjie+signstring, strings. Newreader (body)) Var resp *http. Response resp, err = client. Do (req) if err! = Nil {fmt. Println ("HTTP Get error.", err) return false} FMT. Println (resp. Body) Defer resp. Body.close () var bodys []byte bodys, err = Ioutil. ReadAll (resp. Body) If err! = Nil {fmt. Println ("http ReadAll body Error", err) return false} FMT. Println (String (bodys)) var msg simplemessage if Len (bodys) > 0 {err = json. Unmarshal (Bodys, &msg) if err! = Nil {fmt. PRINTLN ("http JSON body error", err) return false} FMT. PRINTLN (msg)} if Msg. Code = = "OK" {        return true} return false}//setquerystr set query request func setquerystr (phone, content string, id Int64) string { var cstzone = time. Fixedzone ("GMT", 0) KeyTime: = time. Now (). In (Cstzone). Format ("2006-01-02t15:04:05z") singnames: = URL. Queryescape ("Signname") buf: = bytes. buffer{} buf. WriteString ("accesskeyid=") buf. WriteString (AK) buf. WriteString (ACTIONSTR) buf. WriteString ("&phonenumbers=") buf. WriteString (phone + singname + singnames + singstr) buf. WriteString (StrConv. Formatint (ID, ten) + Msgtemplete + content) buf. WriteString ("&timestamp=" + URL. Queryescape (keyTime) + datestr) signstring: = buf. String () return Signstring}//hmac4go encrypts the Func hmac4go (name, SK String) string {mac: = HMAC. New (SHA1. New, []byte (SK)) Mac. Write ([]byte (name)) Encodestring: = base64. Stdencoding.encodetostring (Mac. Sum (nil)) return encodestring}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.