Last active
May 6, 2017 10:23
-
-
Save mujdatcicek/6c09c9b92adc08e5def64e12e188ae04 to your computer and use it in GitHub Desktop.
NETGSM SMS gönderimi için go örneği
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "io/ioutil" | |
| "net/http" | |
| "net/url" | |
| "strings" | |
| ) | |
| // NetGsm hesap bilgilerini içerir | |
| type NetGsm struct { | |
| UserCode, Password, Header string | |
| UseTurkishChar bool | |
| } | |
| func clearTurkishChars(message string) string { | |
| trChars := []string{"Ç", "Ğ", "İ", "Ö", "Ş", "Ü", "ç", "ğ", "ı", "ö", "ş", "ü"} | |
| trCharsReplaceWith := []string{"C", "G", "I", "O", "S", "U", "c", "g", "i", "o", "s", "u"} | |
| for index := 0; index < len(trChars); index++ { | |
| message = strings.Replace(message, trChars[index], trCharsReplaceWith[index], 1) | |
| } | |
| return message | |
| } | |
| func (netGsm *NetGsm) sendSMS(message, gsmno string) (bool, string, error) { | |
| if !netGsm.UseTurkishChar { | |
| message = clearTurkishChars(message) | |
| } | |
| message = url.QueryEscape(message) | |
| params := "usercode=" + netGsm.UserCode + "&password=" + netGsm.Password + "&gsmno=" + gsmno + "&message=" + message + "&msgheader=" + netGsm.Header | |
| req, err := http.NewRequest("GET", "https://api.netgsm.com.tr/bulkhttppost.asp?"+params, nil) | |
| if err != nil { | |
| return false, "", err | |
| } | |
| response, err := http.DefaultClient.Do(req) | |
| if err != nil { | |
| return false, "", err | |
| } | |
| smsID := "" | |
| responseBody, err := ioutil.ReadAll(response.Body) | |
| responseMessage := string(responseBody[:]) | |
| responseCode := responseMessage[0:2] | |
| fmt.Println(responseMessage) | |
| if responseCode == "00" { | |
| smsID = responseMessage[3 : len(responseMessage)-2] | |
| } | |
| return responseCode == "00", smsID, err | |
| } | |
| func main() { | |
| UserCode := "XXXXXXXX" | |
| Password := "XXXXXXXX" | |
| Header := "XXXXXX" | |
| netGsm := NetGsm{Header: Header, UserCode: UserCode, Password: Password, UseTurkishChar: false} | |
| isSuccess, smsID, err := netGsm.sendSMS("SMSTEXT", "NUMBER") | |
| if err != nil { | |
| return | |
| } | |
| fmt.Println(isSuccess) | |
| fmt.Println(smsID) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment