-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
165 lines (148 loc) · 5.72 KB
/
bot.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from datetime import datetime
from flask import current_app
from linebot import LineBotApi
from linebot.models import TextSendMessage
import json
class HMIFLineBotApi(LineBotApi):
def __init__(self, channel_access_token):
super(HMIFLineBotApi, self).__init__(channel_access_token)
def reply_message_raw(self, reply_token, messages, timeout=None):
if not isinstance(messages, (list, tuple)):
messages = [messages]
data = {
'replyToken': reply_token,
'messages': [message for message in messages]
}
super(HMIFLineBotApi, self)._post(
'/v2/bot/message/reply', data=json.dumps(data), timeout=timeout
)
def __create_left_box_content(self, event):
startday = datetime.fromtimestamp(event.get('start')).strftime('%a')
startdate = datetime.fromtimestamp(event.get('start')).strftime('%d %b')
left_box_contents = []
left_box_contents.append({
'type': 'text',
'text': startday,
'gravity': 'top',
'align': 'end',
'weight': 'bold',
'size': 'xs'
})
left_box_contents.append({
'type': 'text',
'text': startdate,
'gravity': 'top',
'align': 'end',
'weight': 'bold',
'size': 'xs'
})
return left_box_contents
def __create_right_box_content(self, event):
right_box_contents = []
right_box_contents.append({
'type': 'text',
'text': event.get('name'),
'gravity': 'top',
'size': 'xs',
'color': '#101010',
'wrap': True
})
startdate = datetime.fromtimestamp(event.get('start')).strftime('%a %d %b')
enddate = datetime.fromtimestamp(event.get('end') - 1).strftime('%a %d %b')
if (not event.get('allDay', False) or startdate != enddate):
starttime = datetime.fromtimestamp(event.get('start')).strftime('%H:%M')
endtime = datetime.fromtimestamp(event.get('end')).strftime('%H:%M')
# event that last less than a day
duration = '{} - {}'.format(starttime, endtime)
if (startdate != enddate):
if (event.get('allDay', False)):
# all day event that last more than a day
duration = '{} - {}'.format(startdate, enddate)
else:
# event that last more than a day, but not all day
duration = '{} {} - {} {}'.format(startdate, starttime, enddate, endtime)
else:
if (event.get('end') - event.get('start') <= 10 * 60):
# event that exist only as a mark, not event
duration = '{}'.format(starttime)
right_box_contents.append({
'type': 'text',
'text': duration,
'gravity': 'bottom',
'size': 'xxs',
'color': '#999999'
})
if (len(event.get('desc', '')) > 0):
right_box_contents.append({
'type': 'text',
'text': event.get('desc', ''),
'gravity': 'bottom',
'size': 'xxs',
'color': '#999999',
'wrap': True
})
if (len(event.get('location', '')) > 0):
right_box_contents.append({
'type': 'text',
'text': event.get('location', ''),
'gravity': 'bottom',
'size': 'xxs',
'color': '#999999'
})
return right_box_contents
def __wrap_event_row(self, left_box_contents, right_box_contents):
return {
'type': 'box',
'layout': 'horizontal',
'spacing': 'md',
'contents': [
{
'type': 'box',
'layout': 'vertical',
'contents': left_box_contents,
'flex': 2,
},
{
'type': 'box',
'layout': 'vertical',
'contents': right_box_contents,
'flex': 8,
}
]
}
def __wrap_event_message(self, title, contents):
return {
'type': 'flex',
'altText': title,
'contents': {
'type': 'bubble',
'body': {
'type': 'box',
'layout': 'vertical',
'spacing': 'md',
'contents': contents,
}
}
}
def __split_list(self, data, n):
chunks = [data[x:x + n] for x in range(0, len(data), n)]
return chunks
def send_events(self, line_event, title, events):
if (len(events) > 0):
rows = []
for event in events:
left_box_contents = self.__create_left_box_content(event)
right_box_contents = self.__create_right_box_content(event)
content = self.__wrap_event_row(left_box_contents, right_box_contents)
rows.append(content)
messages = []
n_break = 12
chunks = self.__split_list(rows, n_break)
for chunk in chunks:
message = self.__wrap_event_message(title, chunk)
messages.append(message)
current_app.logger.info(f'Sending {len(events)} events in {len(messages)} messages..')
self.reply_message_raw(line_event.reply_token, messages[:5])
else:
response = TextSendMessage(text='Wah belum ada event nih!')
self.reply_message(line_event.reply_token, response)