BOT API Trial Account Overview - LINE Business Center ãå ¬éããã¦ãLINEã®botãä½ããããã«ãªã£ããã¨ãããã¨ã§ é ãã°ããªããèªåã 過去に作ったTwitter bot ã移æ¤ãã¦ã¿ãã
ããã¹ããåãåã£ã¦ã¡ãã£ã¨æ¹å¤ãã¦ãªã¦ã è¿ããããã¨ããã®ã¯ç·´ç¿ã¨ãã¦ã¯è¯ãé¡æã§ãããkagome ã使ãã°Pure Goã§å½¢æ ç´ è§£æã§ããããã¨ãããã¨ã§Goã§æ¸ãã¦ã¿ãã
- source code
- https://github.com/sugyan/line-zenra-bot
Callback URLã«SSLãå¿ é ãéä¿¡å´ã¯IP Whilelistã«ç»é²ãã¦ããå¿ è¦ããããã¨ãããã¨ã§ã¡ãã£ã¨å¶éãããããã©ããããããã®ãéã³ç¨åº¦ã®ãã®ãªãherokuã§åã㦠fixie addonã使ãã°IPåºå®ããã¦ä½¿ããã¨ãã§ããããã£ã¨æ¬æ ¼çã«ä½¿ããã¨æã£ããã¡ããã¨ããæ§æãèããå¿ è¦ãããã§ããããã©ãã
å®è£ ã®åèã«ãã¨å¹¾ã¤ãã®è¨äºãå ¬éã³ã¼ããèªãã§ã¿ãã®ã§ãã Signature validationãç¡è¦ãã¦ãããã®ãå¤ãã¦æ°ã«ãªãã¾ããã
ãéã³ç¨åº¦ã®ãã¢ãªãæ°ã«ãããã¨ããªãã®ãããããªããã©ãCallback URLã«å¯¾ãã¦èª°ã§ãåºé±ç®ãªJSONãPOSTã§ããç¶æ
㧠ãã®ã¾ã¾ä¿¡é ¼ãã¦ãã®ãã¼ã¿ã使ããªãã¦ã¨ãã§ããªãã
ãããªã«é£ãããã¨ã§ããªãããã¡ããã¨Signature validationã¯ãã¦ãããã
func (bot *Bot) handle(w http.ResponseWriter, req *http.Request) { defer req.Body.Close() body, err := ioutil.ReadAll(req.Body) if err != nil { ... } if !bot.checkSignature(req.Header.Get("X-Line-Channelsignature"), body) { return nil, errors.New("invalid signature") } ... } func (bot *Bot) checkSignature(signature string, body []byte) bool { hash := hmac.New(sha256.New, []byte(bot.ChannelSecret)) hash.Write(body) return signature == base64.StdEncoding.EncodeToString(hash.Sum(nil)) }
â»è¿½è¨ãã¾ãã