Go SDK for the LINE Messaging API
See the official API documentation for more information.
English: https://devdocs.line.me/en/
Japanese: https://devdocs.line.me/ja/
$ go get github.com/line/line-bot-sdk-go/linebot
import (
"github.com/line/line-bot-sdk-go/linebot"
)
func main() {
bot, err := linebot.New("<channel secret>", "<channel access token>")
...
}
client := &http.Client{}
bot, err := linebot.New("<channel secret>", "<channel accsss token>", linebot.WithHTTPClient(client))
...
The LINE Messaging API uses the JSON data format.
ParseRequest()
will help you to parse the *http.Request
content and return a slice of Pointer point to Event Object.
events, err := bot.ParseRequest(req)
if err != nil {
// Do something when something bad happened.
}
The LINE Messaging API defines 7 types of event - EventTypeMessage
, EventTypeFollow
, EventTypeUnfollow
, EventTypeJoin
, EventTypeLeave
, EventTypePostback
, EventTypeBeacon
. You can check the event type by using event.Type
for _, event := range events {
if event.Type == linebot.EventTypeMessage {
// Do Something...
}
}
To send a message to a user, group, or room, you need either an ID
userID := event.Source.UserID
groupID := event.Source.GroupID
RoomID := event.Source.RoomID
or a reply token.
replyToken := event.ReplyToken
The LINE Messaging API provides various types of message. To create a message, use New<Type>Message()
.
leftBtn := linebot.NewMessageTemplateAction("left", "left clicked")
rightBtn := linebot.NewMessageTemplateAction("right", "right clicked")
template := linebot.NewConfirmTemplate("Hello World", leftBtn, rightBtn)
messgage := linebot.NewTemplateMessage("Sorry :(, please update your app.", template)
With an ID, you can send message using PushMessage()
var messages []linebot.Message
// append some message to messages
_, err := bot.PushMessage(ID, messages...).Do()
if err != nil {
// Do something when some bad happened
}
With a reply token, you can reply to messages using ReplyMessage()
var messages []linebot.Message
// append some message to messages
_, err := bot.ReplyMessage(replyToken, messages...).Do()
if err != nil {
// Do something when some bad happened
}
This library requires Go 1.6 or later.
See LICENSE.txt