diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 096df563..d71245e2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -14,9 +14,9 @@ jobs: - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: - go-version: 1.21 + go-version: '1.25' - name: Build run: go build -v ./... diff --git a/README.md b/README.md index 99003f4c..8f3b24d7 100644 --- a/README.md +++ b/README.md @@ -1,66 +1,136 @@ -LINE BotTemplate: A simple Golang LINE Bot Template for LINE Bot API -============== +# LINE BotTemplate - [![GoDoc](https://godoc.org/github.com/kkdai/LineBotTemplate.svg?status.svg)](https://godoc.org/github.com/kkdai/LineBotTemplate) ![Go](https://github.com/kkdai/LineBotTemplate/workflows/Go/badge.svg) [![goreportcard.com](https://goreportcard.com/badge/github.com/kkdai/LineBotTemplate)](https://goreportcard.com/report/github.com/kkdai/LineBotTemplate) +A minimal Golang LINE Bot template built on the official [line-bot-sdk-go v8](https://github.com/line/line-bot-sdk-go). Clone it, deploy it, and you have a working LINE Bot in a few minutes. -Installation and Usage -============= +[![GoDoc](https://godoc.org/github.com/kkdai/LineBotTemplate.svg?status.svg)](https://godoc.org/github.com/kkdai/LineBotTemplate) +![Go](https://github.com/kkdai/LineBotTemplate/workflows/Go/badge.svg) +[![goreportcard.com](https://goreportcard.com/badge/github.com/kkdai/LineBotTemplate)](https://goreportcard.com/report/github.com/kkdai/LineBotTemplate) -### 1. Got A LINE Bot API devloper account +## What it does -- [Make sure you already registered on LINE developer console](https://developers.line.biz/console/), if you need use LINE Bot. +- 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 -- Create new Messaging Channel -- Get `Channel Secret` on "Basic Setting" tab. -- Issue `Channel Access Token` on "Messaging API" tab. -- Open LINE OA manager from "Basic Setting" tab. -- Go to Reply setting on OA manager, enable "webhook" +That is the whole app — one file, [`main.go`](main.go), roughly 100 lines. It is meant to be read and then rewritten into your own bot. -### 2. Deploy this on Web Platform +## Requirements -You can choose [Heroku](https://www.heroku.com/) or [Render](http://render.com/) +| | | +|---|---| +| Go | 1.25 or later | +| SDK | `github.com/line/line-bot-sdk-go/v8` v8.22.0 | -#### 2(A) Deploy this on Heroku +## Environment variables -[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) +Both are required. You get them from the [LINE Developers Console](https://developers.line.biz/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](https://developers.line.biz/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). -- Input `Channel Secret` and `Channel Access Token`. -- Remember your heroku, ID. +### 2. Deploy -#### 2(B) Deploy this on Rener +Pick whichever platform you prefer. + +#### Render [![Deploy to Render](http://render.com/images/deploy-to-render-button.svg)](https://render.com/deploy) -### 3. Go to LINE Bot Dashboard, setup basic API +Render reads [`render.yaml`](render.yaml). Fill in `ChannelSecret` and `ChannelAccessToken` when prompted. + +#### Heroku + +[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy) + +Heroku reads [`app.json`](app.json) and [`Procfile`](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://.onrender.com/callback +https://.herokuapp.com/callback +``` + +Hit **Verify**. If it returns success, add the bot as a friend and send it a message. + +## Running locally + +```bash +git clone https://github.com/kkdai/LineBotTemplate.git +cd LineBotTemplate + +export ChannelSecret= +export ChannelAccessToken= + +go run main.go +# http://localhost:5000/ +``` + +LINE only calls HTTPS webhooks, so expose the local port with a tunnel: + +```bash +ngrok http 5000 +``` + +Then set the webhook URL to `https://.ngrok-free.app/callback`. + +## Making it your own -- Setup your basic account information. Here is some info you will need to know. -- `Callback URL`: +All the interesting code lives in the event loop in `main.go`: -It all done. +```go +for _, event := range cb.Events { + switch e := event.(type) { + case webhook.MessageEvent: + switch message := e.Message.(type) { + case webhook.TextMessageContent: + // your logic here + } + } +} +``` -### Video Tutorial +- **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 events** — `webhook.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. -- [How to deploy LINE BotTemplate](https://www.youtube.com/watch?v=0BIknEz1f8k) -- [Hoe to modify your LINE BotTemplate code](https://www.youtube.com/watch?v=ckij73sIRik) +See the [Messaging API reference](https://developers.line.biz/en/reference/messaging-api/) for the full surface. -### Chinese Tutorial +## Tutorials -如果你看得懂繁體中文,這裡有[中文的介紹](http://www.evanlin.com/create-your-line-bot-golang/) +- Video: [How to deploy LINE BotTemplate](https://www.youtube.com/watch?v=0BIknEz1f8k) +- Video: [How to modify your LINE BotTemplate code](https://www.youtube.com/watch?v=ckij73sIRik) +- 中文教學:[用 Golang 打造你的 LINE Bot](http://www.evanlin.com/create-your-line-bot-golang/) -Inspired By -============= +## Inspired by - [Golang (heroku) で LINE Bot 作ってみる](http://qiita.com/dongri/items/ba150f04a98e96b160e7) - [LINE BOT をとりあえずタダで Heroku で動かす](http://qiita.com/yuya_takeyama/items/0660a59d13e2cd0b2516) - [阿美語萌典 BOT](https://github.com/miaoski/amis-linebot) -Project52 ---------------- +## Project52 It is one of my [project 52](https://github.com/kkdai/project52). -License ---------------- +## License Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/go.mod b/go.mod index accd1b3e..0237fae1 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/kkdai/LineBotTemplate -// +heroku goVersion go1.21 -go 1.23 +// +heroku goVersion go1.25 +go 1.25 -require github.com/line/line-bot-sdk-go/v8 v8.10.0 +require github.com/line/line-bot-sdk-go/v8 v8.22.0 diff --git a/go.sum b/go.sum index 41f4af7a..30303ed2 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/line/line-bot-sdk-go/v8 v8.10.0 h1:rdlb+Qp2UGPgAnt0CWTHfPDxmTtQ0taGuiPsqj6LCfU= -github.com/line/line-bot-sdk-go/v8 v8.10.0/go.mod h1:9U4mY4kLAFSCSwPl1YxtqmG0Db19DnclpuYS5VOkOZY= +github.com/line/line-bot-sdk-go/v8 v8.22.0 h1:jkxX5Ds8zlPSxUYil63P/Xg52SJ+kN/Xa7A4t/ItlDw= +github.com/line/line-bot-sdk-go/v8 v8.22.0/go.mod h1:QMXJwPka2ysSeVQKWXkBp8DzBFs+CFAXFNo75KJtWho=