A minimal Golang LINE Bot template built on the official line-bot-sdk-go v8. Clone it, deploy it, and you have a working LINE Bot in a few minutes.
- Serves a
/callbackwebhook endpoint and verifies the LINE signature - Echoes back any text message it receives
- Replies with the sticker ID / resource type when it receives a sticker
- Logs every incoming event so you can see the payload shape while developing
That is the whole app — one file, main.go, roughly 100 lines. It is meant to be read and then rewritten into your own bot.
| Go | 1.25 or later |
| SDK | github.com/line/line-bot-sdk-go/v8 v8.22.0 |
Both are required. You get them from the LINE Developers Console.
| Name | Where to find it |
|---|---|
ChannelSecret |
Messaging API channel → Basic settings tab |
ChannelAccessToken |
Messaging API channel → Messaging API tab → issue a long-lived token |
PORT is optional and defaults to 5000. Most PaaS providers set it for you.
- Sign in to the LINE Developers Console.
- Create a new Messaging API channel.
- Copy the
Channel Secretfrom the Basic settings tab. - Issue a
Channel Access Tokenon the Messaging API tab. - Open LINE Official Account Manager from the Basic settings tab, go to Response settings, and turn Webhook on (turn Auto-reply messages off, otherwise the OA replies before your bot does).
Pick whichever platform you prefer.
Render reads render.yaml. Fill in ChannelSecret and ChannelAccessToken when prompted.
Heroku reads app.json and Procfile. Fill in the same two variables, and note the app name — you need it for the webhook URL.
Back in the LINE Developers Console, set the Webhook URL to your deployment plus /callback:
https://<your-app>.onrender.com/callback
https://<your-app>.herokuapp.com/callback
Hit Verify. If it returns success, add the bot as a friend and send it a message.
git clone https://github.com/kkdai/LineBotTemplate.git
cd LineBotTemplate
export ChannelSecret=<your channel secret>
export ChannelAccessToken=<your channel access token>
go run main.go
# http://localhost:5000/LINE only calls HTTPS webhooks, so expose the local port with a tunnel:
ngrok http 5000Then set the webhook URL to https://<subdomain>.ngrok-free.app/callback.
All the interesting code lives in the event loop in main.go:
for _, event := range cb.Events {
switch e := event.(type) {
case webhook.MessageEvent:
switch message := e.Message.(type) {
case webhook.TextMessageContent:
// your logic here
}
}
}- Reply to different message types — add cases for
webhook.ImageMessageContent,webhook.LocationMessageContent,webhook.AudioMessageContent, and so on. - Reply with something richer than text — swap
messaging_api.TextMessageforStickerMessage,ImageMessage,FlexMessage, orTemplateMessagein theMessagesslice. - Handle non-message events —
webhook.FollowEvent,webhook.JoinEvent,webhook.PostbackEvent, andwebhook.BeaconEventare cases on the outer switch, alongsidewebhook.MessageEvent. - Push instead of reply — a reply token is single-use and short-lived; use
bot.PushMessagewhen you need to message a user outside of a reply window.
See the Messaging API reference for the full surface.
- Video: How to deploy LINE BotTemplate
- Video: How to modify your LINE BotTemplate code
- 中文教學:用 Golang 打造你的 LINE Bot
It is one of my project 52.
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.