Skip to content

Instantly share code, notes, and snippets.

@evalphobia
Created May 24, 2017 12:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evalphobia/f8428f7ee1f2b288dae7e4c9d8f1efd7 to your computer and use it in GitHub Desktop.
Save evalphobia/f8428f7ee1f2b288dae7e4c9d8f1efd7 to your computer and use it in GitHub Desktop.
Women Who Go Lineボットの話
// Copyright 2016 LINE Corporation
//
// LINE Corporation licenses this file to you under the Apache License,
// version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/line/line-bot-sdk-go/linebot"
)
var bot *linebot.Client
func main() {
var err error
bot, err = linebot.New(
"<ひみつ>",
"<秘密>",
)
if err != nil {
log.Fatal(err)
}
// Setup HTTP Server for receiving requests from LINE platform
http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
events, err := bot.ParseRequest(req)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
for _, event := range events {
if event.Type == linebot.EventTypeMessage {
switch message := event.Message.(type) {
case *linebot.TextMessage:
replyTextMessage(event.ReplyToken, message)
}
}
}
})
// This is just sample code.
// For actual use, you must support HTTPS by using `ListenAndServeTLS`, a reverse proxy or something else.
if err := http.ListenAndServe(":9999", nil); err != nil {
log.Fatal(err)
}
}
func replyTextMessage(replyToken string, message *linebot.TextMessage) {
var response string
switch {
case strings.Contains(message.Text, "天気"):
// 天気の処理
response = getWeatherInfo()
default:
// 同じ文言を返却
response = message.Text
}
if _, err := bot.ReplyMessage(replyToken, linebot.NewTextMessage(response)).Do(); err != nil {
log.Print(err)
}
}
func getWeatherInfo() string {
const url = "http://weather.livedoor.com/forecast/webservice/json/v1?city=130010"
resp, err := http.Get(url)
if err != nil {
fmt.Printf("error = %v", err)
return err.Error()
}
byt, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("ioutil error = %v", err)
return err.Error()
}
// var result map[string]interface // <- 簡易的にやる例。本当は↓みたいに構造体の方が良い
var result WeatherResponse
fmt.Printf("============= アンマーシャル前:\n %v\n\n", result)
err = json.Unmarshal(byt, &result)
if err != nil {
fmt.Printf("json error = %v", err)
return err.Error()
}
fmt.Printf("============= アンマーシャル後:\n %v\n\n", result)
return fmt.Sprintf("%v", result.Description.text)
}
type WeatherResponse struct {
Title string `json:"title"`
Description description `json:"description"`
}
type description struct {
text string `json:"text"` // TODO: データが入らないバグを直す
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment