Skip to content

karenchuu/go-linebot

Repository files navigation

Line Bot

  • Create a simple Line Bot that can receive and push messages.
  • Messages received are stored in MongoDB.

Setup local development

  • Docker desktop

  • Go

  • Homebrew

  • Install ngrok (for macOS users)

    brew install --cask ngrok

    用途: 使用ngrok,把外界的請求轉發到你的local

Setup MongoDB

  • Start MongoDB container: make up

Line Bot related settings

Make sure you already registered on LINE developer console.

  1. Create new Messaging Channel
  2. Get Channel Secret on "Basic Setting" tab.
  3. Issue Channel Access Token on "Messaging API" tab.
  4. Open LINE Official Account Manager from "Basic Setting" tab.
  5. Go to Reply setting on LINE Official Account Manager, enable "webhook"

How to run

  • Clone this project

    # Move to your workspace
    cd your-workspace
    
    # Clone this project into your workspace
    git clone git@github.com:karenchuu/go-linebot.git
    
    # Move to the project root directory
    cd go-linebot
    
  • Modify the configuration config/config.yaml

    • linebot.channelSecret and linebot.channelAccessToken must be changed to the data obtained from the LINE Developer Console.
  • Run server:

    go run main.go
    ngrok http 8080
    
  • Go to Messaging API on LINE Official Account Manager, input webhookurl. for example: https://{{YOUR NGROK URL}}/linecallback

The Project Layout

.
├── config
│   ├── config.yaml
├── db
│   └── mongo.go
├── docker-entrypoint-initdb.d
│   └── mongo-init.js
├── internal
│   ├── api
│   └── models
├── docker-compose.yaml
├── go.mod
├── go.sum
├── main.go
├── Makefile
├── main.go

Test

Test receiving Line Bot messages and pushing messages.

  • 傳送任意 Line 訊息,webhook 收到訊息後,會將 user 資訊及訊息分別存入 MongoDB 的 usersmessages 資料表

    Screenshot 2023-01-21 at 12 16 18 PM
  • 取得 user 資訊 http://localhost:8080/v1/users Screenshot 2023-01-21 at 12 25 30 PM

  • 取得指定 user 傳送的訊息 http://localhost:8080/v1/messages?bot_user_id=<bot_user_id> Screenshot 2023-01-21 at 12 26 12 PM

  • 主動推播訊息給 user http://localhost:8080/v1/sendMessage Screenshot 2023-01-21 at 12 28 19 PM

    Screenshot 2023-01-21 at 12 29 10 PM
  • 影片版本(測試步驟跟上面一樣) demo

About

Build a Linebot using Go and MongoDB.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages