Skip to content

Repository files navigation

LINE BotTemplate

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.

GoDoc Go goreportcard.com

What it does

  • Serves a /callback webhook 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.

Requirements

Go 1.25 or later
SDK github.com/line/line-bot-sdk-go/v8 v8.22.0

Environment variables

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.

Getting started

1. Create a LINE Messaging API channel

  1. Sign in to the LINE Developers Console.
  2. Create a new Messaging API channel.
  3. Copy the Channel Secret from the Basic settings tab.
  4. Issue a Channel Access Token on the Messaging API tab.
  5. 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).

2. Deploy

Pick whichever platform you prefer.

Render

Deploy to Render

Render reads render.yaml. Fill in ChannelSecret and ChannelAccessToken when prompted.

Heroku

Deploy

Heroku reads app.json and Procfile. Fill in the same two variables, and note the app name — you need it for the webhook URL.

3. Point LINE at your webhook

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.

Running locally

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 5000

Then set the webhook URL to https://<subdomain>.ngrok-free.app/callback.

Making it your own

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.TextMessage for StickerMessage, ImageMessage, FlexMessage, or TemplateMessage in the Messages slice.
  • Handle non-message eventswebhook.FollowEvent, webhook.JoinEvent, webhook.PostbackEvent, and webhook.BeaconEvent are cases on the outer switch, alongside webhook.MessageEvent.
  • Push instead of reply — a reply token is single-use and short-lived; use bot.PushMessage when you need to message a user outside of a reply window.

See the Messaging API reference for the full surface.

Tutorials

Inspired by

Project52

It is one of my project 52.

License

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.

About

A simple Golang LINE bot Template and tutorial how to setup on Heroku for LINE bot API

Topics

Resources

Stars

200 stars

Watchers

20 watching

Forks

Releases

Used by

Contributors

Languages