-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
189 lines (165 loc) · 7.34 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import unicode_literals
import errno
import os
import sys
import tempfile
from argparse import ArgumentParser
from flask import Flask, request, abort, send_from_directory
from werkzeug.middleware.proxy_fix import ProxyFix
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
LineBotApiError, InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
SourceUser, SourceGroup, SourceRoom,
TemplateSendMessage, ConfirmTemplate, MessageAction,
ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
PostbackAction, DatetimePickerAction,
CameraAction, CameraRollAction, LocationAction,
CarouselTemplate, CarouselColumn, PostbackEvent,
StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
ImageMessage, VideoMessage, AudioMessage, FileMessage,
UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
TextComponent, SpacerComponent, IconComponent, ButtonComponent,
SeparatorComponent, QuickReply, QuickReplyButton,
ImageSendMessage)
import mydatabase
import datetime
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_host=1, x_proto=1)
# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
if channel_secret is None:
print('Specify LINE_CHANNEL_SECRET as environment variable.')
sys.exit(1)
if channel_access_token is None:
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)
line_bot_api = LineBotApi(channel_access_token)
handler = WebhookHandler(channel_secret)
static_tmp_path = os.path.join(os.path.dirname(__file__), 'static', 'tmp')
# function for create tmp dir for download content
def make_static_tmp_dir():
try:
os.makedirs(static_tmp_path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(static_tmp_path):
pass
else:
raise
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except LineBotApiError as e:
print("Got exception from LINE Messaging API: %s\n" % e.message)
for m in e.error.details:
print(" %s: %s" % (m.property, m.message))
print("\n")
except InvalidSignatureError:
abort(400)
return 'OK'
# 普通のメッセージを受け取った時の動作記述
@handler.add(MessageEvent, message=TextMessage)
def handle_text_message(event):
text = event.message.text
profile = line_bot_api.get_profile(event.source.user_id)
usrname = 'user_' + profile.display_name
if text.split()[0] == 'insert':
startTime = text.split()[1] + ' ' + text.split()[2]
finishTime = text.split()[3] + ' ' + text.split()[4]
mydatabase.InsertRow(usrname, startTime, finishTime)
line_bot_api.reply_message(event.reply_token, TextSendMessage(text='記録を挿入しました'))
elif text == 'help':
line_bot_api.reply_message(event.reply_token, TextSendMessage(text='insert format:\ninsert Y/m/d H:M Y/m/d H:M'))
else:
buttons_template = ButtonsTemplate(
title='動作を指定してください', text='「help」と送信すると説明文が出る(予定)', actions=[
# startというデータのpostbackeventを発行
PostbackAction(label='start', data='start'),
PostbackAction(label='finish', data='finish'),
DatetimePickerAction(label='show', data='show', mode='date'),
PostbackAction(label='del', data='del'),
])
template_message = TemplateSendMessage(
alt_text='Buttons alt text', template=buttons_template)
line_bot_api.reply_message(event.reply_token, template_message)
@handler.add(PostbackEvent)
def handle_postback(event):
profile = line_bot_api.get_profile(event.source.user_id)
usrname = 'user_' + profile.display_name
data = event.postback.data
if data == 'start':
messageStart = 'start timer' if mydatabase.Start(usrname) else 'すでにstartしています'
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=messageStart))
elif data == 'finish':
messageFinish = 'finish timer' if mydatabase.Finish(usrname) else 'まだstartしてないです'
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=messageFinish))
elif data == 'show':
date_str = event.postback.params['date']
year, month, day = date_str.split('-')
rowList = mydatabase.GetTableByMonth(usrname, year, month)
output = "{}-{} の記録\n".format(year, month)
for row in rowList:
start = datetime.datetime.strftime(row[2], "%d日 %H:%M")
finish = datetime.datetime.strftime(row[3], "%H:%M")
output += start + ' - ' + finish + '\n'
output += "------------\n"
h,m,_= str(mydatabase.GetSumOfMonth(usrname, year, month)).split(":")
output += "合計: " + h + "時間" + m + "分"
line_bot_api.reply_message(event.reply_token, TextSendMessage(text=output))
elif data == 'del':
mydatabase.DeleteRow(usrname)
line_bot_api.reply_message(event.reply_token, TextSendMessage(text='直近のデータを削除'))
# 友達登録された時の挙動(ここで新規テーブル作ると良い)
@handler.add(FollowEvent)
def handle_follow(event):
profile = line_bot_api.get_profile(event.source.user_id)
usrname = 'user_' + profile.display_name
mydatabase.CreateTable(usrname)
line_bot_api.reply_message(
event.reply_token, TextSendMessage(text='Got follow event'))
# ここでテーブル削除すると良い
@handler.add(UnfollowEvent)
def handle_unfollow():
profile = line_bot_api.get_profile(event.source.user_id)
usrname = 'user_' + profile.display_name
mydatabase.CreateTable(usrname)
app.logger.info("Got Unfollow event")
@handler.add(JoinEvent)
def handle_join(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text='Joined this ' + event.source.type))
@handler.add(LeaveEvent)
def handle_leave():
app.logger.info("Got leave event")
@app.route('/static/<path:path>')
def send_static_content(path):
return send_from_directory('static', path)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)