I made a LINE BOT that returns parrots by referring to the sample program below. https://github.com/line/line-bot-sdk-go/blob/master/examples/echo_bot/server.go
Deploying to AWS or Heroku is a hassle, so I used ngrok to publish the service from my local device to the outside world. After that, it is not preferable to publish the channel token etc. when publishing the program on GitHub, so I got it from the ini file.
main.go
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/line/line-bot-sdk-go/linebot"
"gopkg.in/ini.v1"
)
type Config struct {
channelSecrt string
channelToken string
}
var Cnf Config
func init() {
c, _ := ini.Load("config.ini")
Cnf = Config{
channelSecrt: c.Section("lineBotApi").Key("secret").String(),
channelToken: c.Section("lineBotApi").Key("token").String(),
}
}
func main() {
http.HandleFunc("/callback", lineHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func lineHandler(w http.ResponseWriter, r *http.Request) {
bot, err := linebot.New(
Cnf.channelSecrt,
Cnf.channelToken,
)
if err != nil {
log.Fatal(err)
}
events, err := bot.ParseRequest(r)
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:
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(message.Text)).Do(); err != nil {
log.Print(err)
}
case *linebot.StickerMessage:
replyMessage := fmt.Sprintf(
"sticker id is %s, stickerResourceType is %s", message.StickerID, message.StickerResourceType)
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(replyMessage)).Do(); err != nil {
log.Print(err)
}
}
}
}
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
}
}
config.ini
[lineBotApi]
secret = <Secret token>
token = <Channel access token>
This time, the following command is executed.
ngrok http 8080
The URL in the red frame is registered in the Webhook URL of the LINE BOT setting.
Recommended Posts