LINE Messaging API SDK for Go
Introduction
The LINE Messaging API SDK for Go makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes.
Documentation
See the official API documentation for more information.
- English: https://developers.line.biz/en/docs/messaging-api/overview/
- Japanese: https://developers.line.biz/ja/docs/messaging-api/overview/
Requirements
This library requires Go 1.20 or later.
Installation
$ go get -u github.com/line/line-bot-sdk-go/v7/linebot
Import all packages in your code
import (
"github.com/line/line-bot-sdk-go/v7/linebot"
"github.com/line/line-bot-sdk-go/v7/linebot/channel_access_token"
"github.com/line/line-bot-sdk-go/v7/linebot/httphandler"
"github.com/line/line-bot-sdk-go/v7/linebot/insight"
"github.com/line/line-bot-sdk-go/v7/linebot/liff"
"github.com/line/line-bot-sdk-go/v7/linebot/manage_audience"
"github.com/line/line-bot-sdk-go/v7/linebot/messaging_api"
"github.com/line/line-bot-sdk-go/v7/linebot/module"
"github.com/line/line-bot-sdk-go/v7/linebot/module_attach"
"github.com/line/line-bot-sdk-go/v7/linebot/shop"
"github.com/line/line-bot-sdk-go/v7/linebot/webhook"
)
Configuration
import (
"github.com/line/line-bot-sdk-go/v7/linebot/messaging_api"
)
func main() {
bot, err := messaging_api.NewMessagingApiAPI(
os.Getenv("LINE_CHANNEL_TOKEN"),
)
...
}
Configuration with http.Client
Every client application allows configuration with WithHTTPClient and WithEndpoint. (For Blob client, configurations WithBlobHTTPClient and WithBlobEndpoint are also available.)
client := &http.Client{}
bot, err := messaging_api.NewMessagingApiAPI(
os.Getenv("LINE_CHANNEL_TOKEN"),
messaging_api.WithHTTPClient(client),
)
...
Getting Started
The LINE Messaging API primarily utilizes the JSON data format. To parse the incoming HTTP requests, the webhook.ParseRequest()
method is provided. This method reads the *http.Request
content and returns a slice of pointers to Event Objects.
import (
"github.com/line/line-bot-sdk-go/v7/linebot/webhook"
)
cb, err := webhook.ParseRequest(os.Getenv("LINE_CHANNEL_SECRET"), req)
if err != nil {
// Handle any errors that occur.
}
The LINE Messaging API is capable of handling various event types. The Messaging API SDK automatically unmarshals these events into respective classes like webhook.MessageEvent
, webhook.FollowEvent
, and so on. You can easily check the type of the event and respond accordingly using a switch statement as shown below:
for _, event := range cb.Events {
switch e := event.(type) {
case webhook.MessageEvent:
// Do Something...
case webhook.StickerMessageContent:
// Do Something...
}
}
We provide code examples.
- EchoBot
- a simple echo bot
- KitchenSink
- a bot that handles many types of events
- EchoBotHandler
- DeliveryHelper
- InsightHelper
- RichmenuHelper
Receiver
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
Create message
The LINE Messaging API provides various types of message.
bot.ReplyMessage(
&messaging_api.ReplyMessageRequest{
ReplyToken: e.ReplyToken,
Messages: []messaging_api.MessageInterface{
messaging_api.TextMessage{
Text: replyMessage,
},
},
},
)
Send message
With an ID, you can send message using PushMessage()
bot.PushMessage(
&messaging_api.PushMessageRequest{
To: "U.......",
Messages: []messaging_api.MessageInterface{
messaging_api.TextMessage{
Text: replyMessage,
},
},
},
nil, // x-line-retry-key
)
With a reply token, you can reply to messages using ReplyMessage()
bot.ReplyMessage(
&messaging_api.ReplyMessageRequest{
ReplyToken: e.ReplyToken,
Messages: []messaging_api.MessageInterface{
messaging_api.TextMessage{
Text: replyMessage,
},
},
},
)
Help and media
FAQ: https://developers.line.biz/en/faq/
Community Q&A: https://www.line-community.me/questions
News: https://developers.line.biz/en/news/
X: @LINE_DEV
Versioning
This project respects semantic versioning.
Contributing
Please check CONTRIBUTING before making a contribution.
License
Copyright (C) 2016 LINE Corp.
Licensed 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.