-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunction.rb
More file actions
38 lines (34 loc) · 965 Bytes
/
function.rb
File metadata and controls
38 lines (34 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require 'json'
require 'line/bot'
def client
@client ||= Line::Bot::Client.new { |config|
config.channel_id = ENV['LINE_CHANNEL_ID']
config.channel_secret = ENV['LINE_CHANNEL_SECRET']
config.channel_token = ENV['LINE_CHANNEL_TOKEN']
}
end
def webhook(event:, context:)
signature = event['headers']['X-Line-Signature']
body = event['body']
unless client.validate_signature(body, signature)
return {statusCode: 400, body: JSON.generate('signature_error')}
end
events = client.parse_events_from(body)
events.each do |event|
case event
when Line::Bot::Event::Message
case event.type
when Line::Bot::Event::MessageType::Text
bot_response = {
type: 'text',
text: "Your message is\n" + event.message['text']
}
client.reply_message(
event['replyToken'],
bot_response
)
end
end
end
{statusCode: 200, body: JSON.generate('done')}
end