webbhlin / linechatbot-on-lambda

This is a line chatbot running on AWS lambda

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

linechatbot-on-lambda

This is a line chatbot demo code running on AWS lambda which calling AWS translate service in background. You might be charged by AWS and please check AWS free-tier servides. You might be also charged by Line business account and please refer to Line Offical website for details.

#how-to - https://medium.com/@webbhlin/use-golang-and-serverless-framework-to-build-line-messenger-chatbot-on-aws-lambda-69226711aee8?sk=8ec278b1c1590bb38c470437c19d91f8

Line ChatBot on lambda

Cloud Architecture

alt text

Prerequesites

  1. need to enable your line developer account
  2. need to add your webhook url - this is your api gateway URL
  3. need to install serverless framework
  4. need to install aws cli and export your

Required Packages

  1. gin gonic web framework
  2. line-bot-sdk-go
  3. aws-go-sdk

go get

go get -u github.com/apex/gateway go get -u github.com/line/line-bot-sdk-go/linebot go get -u github.com/gin-gonic/gin go get -u github.com/webbhlin/awstranslate

Environment Variables

export your AWS environment variables in your bashrc or bashprofile

export AWS_ACCESS_KEY_ID= export AWS_SECRET_ACCESS_KEY=

Serverless framework command reference and code changes

Create a serverless go project

serverless create -t aws-go-dep -p go-linechatbotlambda

Copy the main.go to your serverless project.

if you use serverless command to create your project, your main.go should be under /hello. This is also what I use for this demo, and you are feel to change but require to update your serverless.yml file.

In your terminal:

  1. copy the main.go file to your project by typeing "cp main.go to_your_serverless_project/hello/"
  2. compile the go binary by typeing "make" - ensure your check your Makefile. Please check my Makefile sample.
  3. fix the errors if any. do you go get all the library you need? any typo?
  4. if there is no error, congrats~ you are free to go next and try "sls deploy" alt text

deploy your serverless project to your lambda function

sls deploy

fix the errors if any. alt text

add your enpoint url to your line developer management console

It might take 3-5 minutes to be ready after you implement your lambda function. In the line console, you have to update your webhook url by clicking "edit" button and test the webhook by clicking on "verify"

alt text

You will see a success message in a pop window of line console. If you don't see the success message, go to your cloudwatch log to see what happened. alt text

endpoints: POST - https://xxxxx.execute-api.us-east-2.amazonaws.com/dev/hello

Test your line chatbot in your line messager

Supported Commands

Translating English to Traditional Chinese by AWS translate service

'2tw hello, I like to say hello!'

Translating Tranditional Chinese to English by AWS translate service

'2en 你是在哈囉嗎?'

Command helper

'@@'

Demo Video Link

https://www.youtube.com/embed/nTyU3pXkN_Q

Reference links

serverless.yml guideline - https://www.serverless.com/framework/docs/providers/aws/guide/functions/

linechatbot sample code - https://github.com/kkdai/LineBotTemplate/blob/master/main.go

serverless golang examples - https://www.serverless.com/blog/framework-example-golang-lambda-support/

gin gonic github - https://github.com/gin-gonic

gin-lambda github - https://github.com/appleboy/gin-lambda

serverless examples [very useful] - https://github.com/serverless/examples

special thanks for @pahuddev :-)

About

This is a line chatbot running on AWS lambda


Languages

Language:Go 100.0%