def find_record(self, user_id): connection = pymysql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database) sql = ("SELECT * FROM tb_records WHERE user_id = '%s';" % (user_id)) cursor = connection.cursor() cursor.execute(sql) rows = cursor.fetchall() cursor.close() connection.close() record = Record() record.insurance_id = rows[0][3] record.image = rows[0][1] record.location = rows[0][4] record.create_time = rows[0][5] return record
def handle_TextMessage(event): if event.message.text.startswith('#'): flag = DatabaseManager().verify_insurance(event.source.user_id, event.message.text) if flag: # line_bot_api.push_message(event.source.user_id,TextSendMessage(text='Please choose the type of Insurance Claims.')) message = TemplateSendMessage( alt_text='Confirm template', template=ConfirmTemplate( text='Please choose the type of Insurance Claims.', actions=[ PostbackAction( label='Slight', display_text='Slight', data='slight' ), PostbackAction( label='Serious', display_text='Serious', data='serious' ) ] ) ) line_bot_api.reply_message(event.reply_token, message) global record record.insurance_id = event.message.text else: line_bot_api.push_message(event.source.user_id, TextSendMessage(text="Sorry, this insurance ID doesn't exist")) elif event.message.text == 'status': temp = DatabaseManager().find_record(event.source.user_id) receipt = { "type": "bubble", "hero": { "type": "image", "url": temp.image, "size": "full", "aspectRatio": "20:13", "aspectMode": "cover", "action": { "type": "uri", "uri": "http://linecorp.com/" } }, "body": { "type": "box", "layout": "vertical", "spacing": "md", "contents": [ { "type": "text", "text": "INSURANCE CLAIMS RECORD", "wrap": True, "weight": "bold", "gravity": "center", "size": "xl" }, { "type": "box", "layout": "vertical", "margin": "lg", "spacing": "sm", "contents": [ { "type": "box", "layout": "baseline", "spacing": "sm", "contents": [ { "type": "text", "text": "ID", "color": "#aaaaaa", "size": "sm", "flex": 1 }, { "type": "text", "text": temp.insurance_id, "wrap": True, "size": "sm", "color": "#666666", "flex": 4 } ] }, { "type": "box", "layout": "baseline", "spacing": "sm", "contents": [ { "type": "text", "text": "Place", "color": "#aaaaaa", "size": "sm", "flex": 1 }, { "type": "text", "text": temp.location, "wrap": True, "color": "#666666", "size": "sm", "flex": 4 } ] }, { "type": "box", "layout": "baseline", "spacing": "sm", "contents": [ { "type": "text", "text": "Time", "color": "#aaaaaa", "size": "sm", "flex": 1 }, { "type": "text", "text": str(temp.create_time), "wrap": True, "color": "#666666", "size": "sm", "flex": 4 } ] } ] }, { "type": "box", "layout": "vertical", "margin": "xxl", "contents": [ { "type": "spacer" }, { "type": "image", "url": "https://scdn.line-apps.com/n/channel_devcenter/img/fx/linecorp_code_withborder.png", "aspectMode": "cover", "size": "xl" }, { "type": "text", "text": "You can check the insurance claims by using this code", "color": "#aaaaaa", "wrap": True, "margin": "xxl", "size": "xs" } ] } ] } } flex_message = FlexSendMessage( alt_text='hello', contents=receipt ) line_bot_api.reply_message( event.reply_token, flex_message ) elif event.message.text == 'compensation': profile = line_bot_api.get_profile(event.source.user_id) msg = TemplateSendMessage( alt_text='Buttons template', template=ButtonsTemplate( thumbnail_image_url=profile.picture_url, title='Compensation', text='Please choose compensation way.', actions=[ PostbackAction( label='check', display_text='check', data='check' ), PostbackAction( label='e-check', display_text='E-check', data='e-check' ), PostbackAction( label='bank transfer', display_text='Bank Transfer', data='bank transfer' ) ] ) ) line_bot_api.reply_message(event.reply_token, msg) else: profile = line_bot_api.get_profile(event.source.user_id) greeting = 'Hi, ' greeting = greeting + profile.display_name line_bot_api.push_message(event.source.user_id, TextSendMessage(text=greeting)) line_bot_api.push_message(event.source.user_id, TextSendMessage(text='Welcome to Insurance Claims Chatbot!')) user_record = DatabaseManager().find_user(event.source.user_id) if not user_record: user = User() user.id = event.source.user_id user.avatar = profile.picture_url user.name = profile.display_name DatabaseManager().save_user(user) create_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") record = Record() record.user_id = event.source.user_id record.create_time = create_time msg = TemplateSendMessage( alt_text='Buttons template', template=ButtonsTemplate( thumbnail_image_url=profile.picture_url, title='Terms of Service', text='Please read the Terms of Service first.', actions=[ PostbackAction( label='Terms of Service', display_text='Terms of Service', data='terms' ), PostbackAction( label='Agree', display_text='Agree', data='agree' ) ] ) ) line_bot_api.reply_message( event.reply_token, msg )