Skip to content

Commit ec1cbd6

Browse files
committed
Use ruby to create a line bot on AWS
1 parent 68dcd4c commit ec1cbd6

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
102102
| [Aws Scheduled Cron](https://github.com/serverless/examples/tree/master/aws-python-scheduled-cron) <br/> Example of creating a function that runs as a cron job using the serverless `schedule` event | python |
103103
| [Aws Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with python | python |
104104
| [Serverless Telegram Bot](https://github.com/serverless/examples/tree/master/aws-python-telegram-bot) <br/> This example demonstrates how to setup an echo Telegram Bot using the Serverless Framework ⚡🤖 | python |
105+
| [Aws Ruby Line Bot](https://github.com/serverless/examples/tree/master/aws-ruby-line-bot) <br/> Example demonstrates how to setup a simple Line echo bot on AWS | ruby |
105106
| [Aws Ruby Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-ruby-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint | ruby |
106107
| [Azure Nodejs](https://github.com/serverless/examples/tree/master/azure-node-line-bot) <br/> Azure Functions sample for the Serverless framework | nodeJS |
107108
| [Azure Node Simple Http Endpoint](https://github.com/serverless/examples/tree/master/azure-node-simple-http-endpoint) <br/> An example of making http endpoints with the Azure Functions Serverless Framework plugin | nodeJS |

aws-ruby-line-bot/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.serverless
2+
vendor
3+
node_modules
4+
package-lock.json
5+
.bundle/

aws-ruby-line-bot/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'line-bot-api'

aws-ruby-line-bot/Gemfile.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
line-bot-api (1.11.0)
5+
6+
PLATFORMS
7+
ruby
8+
9+
DEPENDENCIES
10+
line-bot-api
11+
12+
BUNDLED WITH
13+
2.0.2

aws-ruby-line-bot/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# AWS-ruby-line-echo-bot
2+
3+
Follow this [project](https://github.com/serverless/examples/tree/master/aws-python-line-echo-bot),
4+
5+
I use my first language(ruby) to build this on serverless,
6+
7+
so you can use this project in others case.
8+
9+
# Bebore you start
10+
11+
1. Line developer account
12+
2. [Line Message API](https://developers.line.biz/en/docs/messaging-api/getting-started/)
13+
14+
# Get Started
15+
16+
1. Install serverless via npm
17+
18+
```bash=
19+
$ npm install -g serverless
20+
```
21+
22+
2. Setup your **AWS** ceritficate
23+
24+
```bash=
25+
export AWS_ACCESS_KEY_ID=<your-key-here>
26+
export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
27+
```
28+
29+
3. Insert you line bot secret & key
30+
31+
```python=
32+
config.channel_secret = "YOUR_LINE_CHANNEL_SECRET"
33+
config.channel_token = "YOUR_LINE_CHANNEL_TOKEN"
34+
```
35+
36+
4. Deploy the webhhok function
37+
38+
```bash=
39+
npm install
40+
serverless deploy
41+
```
42+
43+
Now you can test you chatbot, have fun!
44+
![Echo bot](https://i.imgur.com/ekiLRHS.png)
45+
46+
# References
47+
48+
- [Plugin hook](https://github.com/serverless/serverless/issues/5567#issuecomment-444671106)

aws-ruby-line-bot/handler.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'line/bot'
2+
3+
def webhook(event:, context:)
4+
client ||= Line::Bot::Client.new { |config|
5+
config.channel_secret = "YOUR_LINE_CHANNEL_SECRET"
6+
config.channel_token = "YOUR_LINE_CHANNEL_TOKEN"
7+
}
8+
9+
event = JSON.parse(event["body"])
10+
reply_token = event["events"][0]["replyToken"]
11+
message = {
12+
type: 'text',
13+
text: event["events"][0]["message"]["text"]
14+
}
15+
16+
response = client.reply_message(reply_token, message)
17+
18+
{ statusCode: 200, body: JSON.generate({message: "OK"}) }
19+
end

aws-ruby-line-bot/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "aws-ruby-line-bot",
3+
"version": "1.0.0",
4+
"description": "Example demonstrates how to setup a simple Line echo bot on AWS",
5+
"author": "NiJia",
6+
"license": "MIT",
7+
"devDependencies": {
8+
"serverless-hooks-plugin": "^1.1.0"
9+
}
10+
}

aws-ruby-line-bot/serverless.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
service: aws-ruby-line-bot
2+
3+
frameworkVersion: ">=1.1.0 <2.0.0"
4+
5+
provider:
6+
name: aws
7+
runtime: ruby2.5
8+
9+
functions:
10+
current_time:
11+
handler: handler.webhook
12+
events:
13+
- http:
14+
path: webhook
15+
method: post
16+
17+
plugins:
18+
- serverless-hooks-plugin
19+
20+
custom:
21+
hooks:
22+
package:initialize:
23+
- bundle install --deployment

0 commit comments

Comments
 (0)