From 2380a6b79d57a33bd8333b9132b5155719c411fe Mon Sep 17 00:00:00 2001 From: kazu634 Date: Sun, 9 Feb 2020 12:11:29 +0700 Subject: [PATCH] =?UTF-8?q?nexmo=20API=E3=81=AE=E4=BD=BF=E3=81=84=E6=96=B9?= =?UTF-8?q?=E8=A8=98=E4=BA=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../golang/2020-02-09-how-to-use-nexmo-api.md | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 content/labs/golang/2020-02-09-how-to-use-nexmo-api.md diff --git a/content/labs/golang/2020-02-09-how-to-use-nexmo-api.md b/content/labs/golang/2020-02-09-how-to-use-nexmo-api.md new file mode 100644 index 0000000..9fb5e7c --- /dev/null +++ b/content/labs/golang/2020-02-09-how-to-use-nexmo-api.md @@ -0,0 +1,87 @@ ++++ +title = "nexmo APIの使い方" +date = 2020-02-09T03:12:22+08:00 +Description = "nexmo APIの使い方のメモ書きです" +Tags = [] +Categories = ["golang", "programming"] ++++ + +SMS送信などができるWebサービス、nexmo APIの使い方をまとめます。基本は参考URLからのコピペです。。 + +## API利用例 +利用例はこんな感じです。 + +### Golangの場合 + +``` +package main + +import ( + "net/http" + "net/url" + "log" + "fmt" +) + +func main(){ + value := url.Values{} + value.Set("from", "Nexmo") + value.Add("text", "Hello from Nexmo dayoneeee") + value.Add("to", "6692346nnnn") + value.Add("api_key", "APIKEY") + value.Add("api_secret", "APISecret") + + resp, err := http.PostForm("https://rest.nexmo.com/sms/json",value) + if err != nil{ + log.Fatal(err) + } + + buffer := make([]byte,1024) + respLen,_ := resp.Body.Read(buffer) + + body := string(buffer[:respLen]) + + fmt.Println(body) + fmt.Println(resp.Status) + + defer resp.Body.Close() +} +``` + +#### 実行例 +実行すると以下のように表示されるはずです: + +``` +kazu634@bastion% go run main.go +{ + "message-count": "1", + "messages": [{ + "to": "6692346nnnn", + "message-id": "1B00000052CEFF69", + "status": "0", + "remaining-balance": "1.88400000", + "message-price": "0.02300000", + "network": "52001" + }] +} +200 OK +``` + +受信した様子がこちら: + +Nexmo + +## curlの場合 +`curl`コマンドで実行する場合は、こんな感じになります: + +``` +curl -X "POST" "https://rest.nexmo.com/sms/json" \ + -d "from=Nexmo" \ + -d "text=Hello from Nexmo" \ + -d "to=81906490nnnn" \ + -d "api_key=APIKEY" \ + -d "api_secret=APISECRET" +``` + +## 参考 +- [GolangでAPI使ってSMS送信 - Qiita](https://qiita.com/KokiAsano/items/fffa3c64a1599ffc53ed) \ No newline at end of file