Skip to content

Commit 4f8cfe5

Browse files
authored
Automate to set webhook url in example v2 (#559)
1 parent 4112021 commit 4f8cfe5

File tree

11 files changed

+43
-19
lines changed

11 files changed

+43
-19
lines changed

examples/v1/echobot/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ An example LINE bot just to echo messages
44

55
## Getting started
66

7-
```ruby
7+
```sh
88
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
99
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
1010
$ bundle install

examples/v1/kitchensink/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ A kitchen-sink LINE bot example
44

55
## Getting started
66

7-
```ruby
7+
```sh
88
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
99
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
1010
$ bundle install

examples/v1/rich_menu/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Getting started
66

7-
```ruby
7+
```sh
88
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
99
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
1010
$ bundle install

examples/v2/audience/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Getting started
66
Update User ID of `app.rb` and `audience.txt' to your own.
77

8-
```ruby
8+
```sh
99
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
1010
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
1111
$ bundle install

examples/v2/channel_access_token/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Getting started
66

7-
```ruby
7+
```sh
88
$ export LINE_CHANNEL_ID=YOUR_CHANNEL_ID
99
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
1010
$ bundle install

examples/v2/echobot/Gemfile.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ PATH
22
remote: ../../..
33
specs:
44
line-bot-api (0.0.1.pre.test)
5-
multipart-post (~> 2.4.1)
5+
base64 (~> 0.2)
6+
multipart-post (~> 2.4)
67

78
GEM
89
remote: https://rubygems.org/

examples/v2/echobot/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
An example LINE bot just to echo messages
33

44
## Getting started
5-
```ruby
5+
This projects tries to set `${APP_BASE_URL}/callback` as webhook URL automatically on starting app.
6+
7+
```sh
68
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
79
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
810
$ bundle install
11+
$ export APP_BASE_URL="https://your.base.url:4567"
912
$ bundle exec ruby app.rb
1013
```
1114

12-
```
13-
https://127.0.0.1:4567/callback
14-
```

examples/v2/echobot/app.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'line-bot-api'
33

44
set :environment, :production
5+
set :app_base_url, ENV.fetch('APP_BASE_URL')
56

67
def client
78
@client ||= Line::Bot::V2::MessagingApi::ApiClient.new(
@@ -13,6 +14,19 @@ def parser
1314
@parser ||= Line::Bot::V2::WebhookParser.new(channel_secret: ENV.fetch("LINE_CHANNEL_SECRET"))
1415
end
1516

17+
configure do
18+
webhook_endpoint = "#{settings.app_base_url}/callback"
19+
body, code, _ = client.set_webhook_endpoint_with_http_info(set_webhook_endpoint_request: Line::Bot::V2::MessagingApi::SetWebhookEndpointRequest.new(
20+
endpoint: webhook_endpoint
21+
))
22+
23+
if code == 200
24+
p "✅ LINE Webhook URL set to #{webhook_endpoint}"
25+
else
26+
p "❌ Failed to set LINE Webhook. code=#{code}, error body=#{body}"
27+
end
28+
end
29+
1630
post '/callback' do
1731
body = request.body.read
1832
signature = request.env['HTTP_X_LINE_SIGNATURE']

examples/v2/kitchensink/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
# Kitchen Sink Bot
22

3-
A kitchen-sink LINE bot example
3+
A kitchen-sink LINE bot example. This contains many examples of line-bot-api gem's features.
44

55
## Getting started
6+
This projects tries to set `${APP_BASE_URL}/callback` as webhook URL automatically on starting app.
67

7-
```ruby
8+
```sh
89
$ export LINE_CHANNEL_SECRET=YOUR_CHANNEL_SECRET
910
$ export LINE_CHANNEL_ACCESS_TOKEN=YOUR_CHANNEL_ACCESS_TOKEN
1011
$ bundle install
1112
$ export APP_BASE_URL="https://your.base.url:4567"
1213
$ bundle exec ruby app.rb
1314
```
14-
15-
Then set url as webhook url in https://developers.line.biz/console/
16-
```
17-
https://your.base.url:4567/callback
18-
```

examples/v2/kitchensink/app.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'line-bot-api'
44

55
set :environment, :production
6-
set :app_base_url, ENV['APP_BASE_URL']
6+
set :app_base_url, ENV.fetch('APP_BASE_URL')
77

88
THUMBNAIL_URL = 'https://via.placeholder.com/1024x1024'
99
HORIZONTAL_THUMBNAIL_URL = 'https://via.placeholder.com/1024x768'
@@ -39,6 +39,19 @@ def parser
3939
@parser ||= Line::Bot::V2::WebhookParser.new(channel_secret: ENV.fetch("LINE_CHANNEL_SECRET"))
4040
end
4141

42+
configure do
43+
webhook_endpoint = "#{settings.app_base_url}/callback"
44+
body, code, _ = client.set_webhook_endpoint_with_http_info(set_webhook_endpoint_request: Line::Bot::V2::MessagingApi::SetWebhookEndpointRequest.new(
45+
endpoint: webhook_endpoint
46+
))
47+
48+
if code == 200
49+
p "✅ LINE Webhook URL set to #{webhook_endpoint}"
50+
else
51+
p "❌ Failed to set LINE Webhook. code=#{code}, error body=#{body}"
52+
end
53+
end
54+
4255
post '/callback' do
4356
body = request.body.read
4457
signature = request.env['HTTP_X_LINE_SIGNATURE']

0 commit comments

Comments
 (0)