-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_vaccination_dates.rb
57 lines (50 loc) · 1.57 KB
/
display_vaccination_dates.rb
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
require 'facebook/messenger'
require 'httparty'
require 'json'
require_relative './bot.rb'
require_relative 'json_templates/template'
require_relative '../utils'
class MessengerBot
#Display upcoming and previous vaccination dates and their details
def display_vaccination_dates(id,vaccination_dates)
template = GENERIC_TEMPLATE_BODY
elements = []
@language = MessengerBot.new.get_language(id)
# if vaccination_dates.length == 0 then
# MessengerBot.say(id,"No vaccines available")
# end
for i in 0..9
break if i > vaccination_dates.length-1
vaccine_name = vaccination_dates[i][:vaccine_name]
vaccine_date = Date.parse(vaccination_dates[i][:due_date]).strftime("%d %b %Y")
vaccine_url = vaccination_dates[i][:url]
new_element = {
"title": "#{vaccine_name.upcase}",
"subtitle": "#{DUE_DATE["#{@language}"]}: #{vaccine_date}",
"buttons":[
{
"type": "web_url",
"title": DETAILS_BUTTON["#{@language}"],
"url": "#{vaccine_url}"
}
]
}
elements << new_element
end
template[:attachment][:payload][:elements] = elements
message_options = {
"messaging_type": "RESPONSE",
"recipient": { "id": "#{id}"},
"message": "#{template.to_json}"
}
res = HTTParty.post(FB_MESSAGE, headers: HEADER, body: message_options)
Bot.on :message do |message|
id = message.sender["id"]
MessengerBot.call_message(id,message.text)
end
Bot.on :postback do |postback|
id = postback.sender["id"]
MessengerBot.call_postback(id,postback.payload)
end
end
end