-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_message_handler.rb
More file actions
64 lines (58 loc) · 1.6 KB
/
Copy pathtext_message_handler.rb
File metadata and controls
64 lines (58 loc) · 1.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require "line/bot"
require './cartridge_dao'
class TextMessageHandler
def initialize(client)
@client = client
end
def handle(event)
text = event.message['text']
case text
when 'latest'
cartridges = CartridgeDAO.latests(5)
if cartridges.length > 0
latest_cartridges(event, cartridges)
else
reply_text(event, 'No cartridges. Create one !')
end
else
if text.start_with?('line://')
reply_text(event, "⇈ Press the above link to start the game ⇈")
else
reply_text(event, "Unknown command: `#{text}`")
end
end
end
def reply_text(event, texts)
texts = [texts] if texts.is_a?(String)
@client.reply_message(
event['replyToken'],
texts.map { |text| {type: 'text', text: text} }
)
end
def reply_content(event, messages)
res = @client.reply_message(
event['replyToken'],
messages
)
puts res.read_body if res.code != 200
end
def latest_cartridges(event, cartridges)
columns = cartridges.map do |c|
{
title: c.name,
text: c.description,
actions: [
{ label: 'Download game ', type: 'message', text: c.url }
]
}
end
reply_content(event, {
type: 'template',
altText: 'List latest cartridges',
template: {
type: 'carousel',
columns: columns
}
})
end
end