def handle_text_message(event): text = event.message.text userid = event.source.user_id user_data = data.get_user(userid) print(f"\n\n\n{user_data}\n\n\n") """ Other msg """ if text == '我要設定帳號': ret = [ TextSendMessage(text="此功能暫時關閉。"), StickerSendMessage(package_id=11537, sticker_id=52002770), ] line_bot_api.reply_message(event.reply_token, ret) elif text == '請給我使用須知': ret1 = TextSendMessage( text="歡迎使用本平台😁\n本平台是作為學校校安機關的安全檢核系統\n目前功能僅有表單檢核功能") ret2 = TextSendMessage( text= "【填寫表單須知】:\n您可以透過點選選單中的問卷按鈕,或是輸入「問卷」來呼叫問卷。\n本問卷提供兩種填答方式:\n\n1.快速檢核:若情況緊急,請使用此捷徑\n2.常規問卷:共分成四類選單,可交叉填答\n【注意】:兩種填寫方式不可交叉填寫" ) ret3 = TextSendMessage(text="本次填寫的事件為:" + str(DB.get_latest_assessment_id_db()[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2] + [ret3]) elif text == '我要統計資料' or text == 'I need the summary of inspection results.': ret = TextSendMessage( text="https://pmdiana.hcilab.katrina.tw/report/index.html") line_bot_api.reply_message(event.reply_token, ret) """ Questionnaire Process """ if userid not in data.get_all_users() and userid not in DB.get_yitianda_db( ): data.add_user(userid) print("新用戶!") if userid in DB.get_yitianda_db(): ret1 = TextSendMessage(text="您已為本次事件提供災情回覆咯~") ret2 = TextSendMessage(text="本次填寫的事件為:" + str(DB.get_latest_assessment_id_db()[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2]) else: ### Module 1 ### if data.get_user_status(userid) == "pre": print("第一模組") signal = toCheck(event, line_bot_api) data.set_user_status(userid, signal[0]) if signal[1] != "": # Record building name # building_id = DB.get_building_id(signal[1]) # print(building_id) data.set_user_building(userid, signal[1]) # 紀錄建築名字 ### Module 2 ### elif data.get_user_status(userid) == "pre-class": # module 1 跟 2 的街口 if data.get_user_current(userid) == (): # 第一次填寫的時候 line_bot_api.unlink_rich_menu_from_user(userid) ct_container = ct_push(data.get_all_users(), userid, 0, 0, DB) # 前面ㄉ0代表推出【快速/標準carousel】 # 後面ㄉ0代表非(類別不修改) carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='災情回覆問卷', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) data.set_user_status(userid, "00") data.set_user_building(userid, text) # 紀錄教室名字 elif user_data['Answered']['Quick'] != []: #選擇快速檢核後,阻止其跳回標準檢核 line_bot_api.reply_message( event.reply_token, TextSendMessage(text="您已選擇快速檢核!請填頁面上的最後一題")) else: ct_container = ct_push(data.get_all_users(), userid, 1, 0, DB) #1代表推出【四大題組carousel】 carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='問卷選單', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_data["status"] == "01" and "題已回覆" not in text: #首次填答問卷選擇【待改進】 print('進入【首次填答待改進】') cat, Q = user_data["current"] ret = None data.add_user_answered(userid, cat, Q) _ = data.get_user_current(userid) + (text, ) data.set_user_current(userid, _) data.add_user_feedback(userid, (cat, Q, text)) ## 請使用者點選問題所在位置 floor = floor_plan() line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + [TextSendMessage(text="請點選問題所在位置(九宮格)")] + [floor]) data.set_user_status(userid, "02") # 等待使用者戳點 # 使用者要戳 平面圖 elif user_data["status"] == "02": if text in ['上左', '上中', '上右', '中左', '中中', '中右', '下左', '下中', '下右']: data.set_user_spec_feedback_pos(userid, text) _ = data.get_user_current(userid) + (text, ) data.set_user_current(userid, _) print(f"\n\n--\n{event}\n\--n\n\n") data.set_user_status(userid, "03") # 等他傳照片 line_bot_api.reply_message( event.reply_token, [TextSendMessage(text="請傳照片,並稍後照片上傳時間(約數秒)🙏")]) else: line_bot_api.reply_message(event.reply_token, [TextSendMessage(text="請點選上方平面圖!")]) #類別/最終修改答案,告訴系統要改的題目(EG, C8) elif user_data["status"] in ["10", "20"] and "題已回覆" not in text: try: if revise_able( revise_extract(text)[0], revise_extract(text)[1]) is True: cat = revise_extract(text)[0] i = revise_extract(text)[1] #相對題號 # 禁止跨類別條件: 同類別 or 最終檢查 if cat == user_data["current"][0] or user_data[ "status"] == "20": data.set_user_current(userid, (cat, i)) data.del_user_answered(userid, cat, i) newlist = [] for j in range(len( user_data["feedback"])): #從feedback拿掉要改的題的資料 if not (cat == user_data["feedback"][j][0] and i == user_data["feedback"][j][1]): newlist.append(user_data["feedback"][j]) data.set_user_feedback(userid, newlist) #丟confirm if user_data["status"] == "10": #各類別要改答案 print('\n進入【類別修改答案】') ret = [cat_revise_confirm(cat, i, DB)] elif user_data["status"] == "20": # 最終要改答案 print('\n進入【最終修改答案】') ret = [revise_confirm(cat, i, DB)] data.add_user_answered(userid, cat, i) else: ret = TextSendMessage(text="請修改當前類別:%s." % user_data["current"][0]) line_bot_api.reply_message(event.reply_token, ret) else: ret = revise_idiot(text, revise_extract(text)[0], revise_extract(text)[1]) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ret)) except: print("類別修改 題號判斷 失敗了喔喔喔喔喔喔喔") # 處理類別/最終改答案的時候,輸入的待改進的內容(EG, 哈囉MO) elif user_data["status"] in ["11", "21"] and "題已回覆" not in text: cat, Q = user_data["current"] data.add_user_feedback(userid, (cat, Q, text, "N/A", "N/A")) data.add_user_answered(userid, cat, Q) output = data.get_user_feedback(userid) if user_data["status"] == "21": #最終修改答案選待改進,revise_result會是false print('進入【最終修改待改進】') data.set_user_status(userid, "20") ret = tempview_confirm(output, DB) elif user_data[ "status"] == "11": #類別修改答案選待改進,cat_revise_result會是false print('進入【類別修改待改進】') data.set_user_status(userid, "10") ret = cat_tempview_confirm(cat, output, DB) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + ret)
def postback_process(userid, data, DB, event): """ input: 1. userid 2. data 3. DB 4. event return: 1. [message] """ ret = "NA" user_data = data.get_user(userid) # 填問卷的過程 # QC丟問題,相對題號 if event.postback.data == 'Quick': ret = [ confirm_push(data.get_all_users(), userid, event.postback.data, DB) ] elif event.postback.data == 'Standard': ct_container = ct_push(data.get_all_users(), userid, 1, 0, DB) #把4類別加進來 carousel_template = CarouselTemplate(columns=ct_container) ret = [ TemplateSendMessage(alt_text='詳細災情回覆問卷', template=carousel_template) ] # 四類丟問題,相對題號 elif event.postback.data in ['Normal', 'Indoors', 'Corridor', 'Outdoors']: ret = [ confirm_push(data.get_all_users(), userid, event.postback.data, DB) ] # 戳題目的confirm template的時候 try: parse = extract(event.postback.data) # [0]是類別;[1]是相對題號;[2]是沒問題/待改進 cat, Q, ans = parse data.set_user_current(userid, (cat, Q)) last = len(DB.get_category(cat)) # 處理carousel template # 填完該類別最後一題且最後一題是沒問題 if Q == last and ans == 'OK': data.set_user_status(userid, "00") data.add_user_answered(userid, cat, Q) output = data.get_user_feedback(userid) print('進入【(第一次)類別TEMPVIEW】') print(f"\n\n====\ncat: |{cat}\nOutput: |{output}\n") ret = cat_tempview_confirm(cat, output, DB) # 推第一次類別修改tempview confirm template # ct_container = ct_push(data.get_all_users(), userid, 1, 0, DB) # QC填完 if cat == "Quick" and Q == last: print('進入【最終TEMPVIEW】──QC的路,不要怕上一句話,因為她是必經之路') output = data.get_user_feedback(userid) ret = tempview_confirm(output, DB) # 推第一次最終修改tempview confirm template # 處理題目的confirm template # 待改進的話,或是非該類別的最後一題 else: ret = [next(data.get_all_users(), userid, parse, DB)[0]] status = next(data.get_all_users(), userid, parse, DB)[1] data.set_user_status(userid, status) except Exception as e: # exc_type, exc_obj, exc_tb = sys.exc_info() # fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] # print('ERROR:', exc_type, exc_obj, fname, exc_tb.tb_lineno) # print(event.postback.data) if event.postback.data == 'edit=NO': data.set_user_status(userid, "00") output = data.get_user_feedback(userid) print(f"\n\n\nAARRR!!!{output}\n\n\n") build, room = data.get_user_building(userid) print(f"\n\n\nBBRRR!!!{(build, room)}\n\n\n") for feed in range(len(output)): output[feed] += (build, ) output[feed] += (room, ) DB.get_feedback(output, userid, data.get_user_building(userid)[0]) #寫進資料庫 print('進入【最終修改答案不修改】,結束問卷') print( f"\n========================\nThis is feedback:\n{output}\n========================\n" ) ret = [ TextSendMessage(text="已收到您的回覆~謝謝您的貢獻!"), StickerSendMessage(package_id=11537, sticker_id=52002739), ] data.remove_user(userid) if event.postback.data == 'edit=OK': print('進入【最終修改答案要修改】,要求輸入修改題號') data.set_user_status(userid, "20") ret = [ TextSendMessage(text="請問您要修改哪一題呢?"), TextSendMessage(text="【注意】:當您填寫快速檢核時,不能修改其他四類問題;反之亦然。"), TextSendMessage( text= "請按照下列格式填寫:\n一般檢查(Normal)簡寫為N\n室內(Indoors)簡寫為I\n走廊(Corridor)簡寫為C\n室外(Outdoors)簡寫為O\n再加上題號,例如:\nN7(一般檢查的第七題)" ), ] if event.postback.data == 'cat_edit=NO': print('進入【類別修改答案不修改】,丟出類別選單') ct_container = ct_push(data.get_all_users(), userid, 1, 1, DB) if ct_container == "All cats have already checked!": # 類別全部修改過後,進入最終環節 print('進入【最終TEMPVIEW】──標準填完了唷,不要怕上一句話,因為她是必經之路') output = data.get_user_feedback(userid) ret = tempview_confirm(output, DB) else: carousel_template = CarouselTemplate(columns=ct_container) ret = [ TemplateSendMessage(alt_text='問卷選單', template=carousel_template) ] if 'cat_edit=OK' in event.postback.data: print('進入【類別修改答案要修改】,要求輸入修改題號') data.set_user_status(userid, "10") ## 避免戳 Normal1, Indoors1, Corridor1, Outdoors1 的時候類別被鎖住 cate = str(event.postback.data).split( ';')[1] if ';' in event.postback.data else '' print('\n===<%s>===\n' % cate) relq = user_data["current"][1] _ = (cate, relq) if cate != '' else user_data["current"] data.set_user_current(userid, _) ret = [ TextSendMessage(text="請問您要修改哪一題呢?"), TextSendMessage(text="【注意】:只能修改當前題組,欲修改其他題組,請於所有問題答畢後修改"), TextSendMessage( text= "請按照下列格式填寫:\n一般檢查(Normal)簡寫為N\n室內(Indoors)簡寫為I\n走廊(Corridor)簡寫為C\n室外(Outdoors)簡寫為O\n再加上題號,例如:\nN7(一般檢查的第七題)" ), ] # 類別修改答案的過程 if 'cat_revise=' in event.postback.data and 'OK' in event.postback.data: # 沒問題 print('進入【類別修改答案沒問題】,丟出cat_tempview') output = data.get_user_feedback(userid) ret = cat_tempview_confirm(user_data["current"][0], output, DB) # 把它目前的回答推個confirm template給他看看 elif 'cat_revise=' in event.postback.data and 'NO' in event.postback.data: # 待改進 print('進入【類別修改答案待改進】,請簡述災情') ret = [TextSendMessage(text="請簡述災情")] data.set_user_status(userid, "11") # 最終修改答案的過程 if 'all_revise=' in event.postback.data and 'OK' in event.postback.data: # 沒問題 print('進入【最後修改答案沒問題】,丟出tempview') output = data.get_user_feedback(userid) ret = tempview_confirm(output, DB) # 把它目前的回答推個confirm template給他看看 elif 'all_revise=' in event.postback.data and 'NO' in event.postback.data: # 待改進 print('進入【最後修改答案待改進】,請簡述災情') ret = [TextSendMessage(text="請簡述災情")] data.set_user_status(userid, "21") # 重設帳號或不設 if event.postback.data == 'account_reset': delete_userid_db(userid, DB) account[userid] = { 'userid': userid, 'name': 0, 'county': 0, 'school': 0, 'phone': 0 } ret = [TextSendMessage(text="請問您尊姓大名?")] account_q = 1 elif event.postback.data == 'account_remain': ret = [TextSendMessage(text="好的,謝謝😁")] return ret
def handle_postback(event): userid = event.source.user_id #取得Userid user_data = data.get_user(userid) global account global account_q ################################## ########## 填問卷的過程 ########### ################################## #QC丟問題,相對題號 if event.postback.data == 'Quick': line_bot_api.reply_message( event.reply_token, confirm_push(data.get_all_users(), userid, event.postback.data, db)) elif event.postback.data == 'Standard': ct_container = ct_push(data.get_all_users(), userid, 1, 0, db) #把4類別加進來 carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage(alt_text='詳細災情回覆問卷', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) #四類丟問題,相對題號 elif event.postback.data in ['Normal', 'Indoors', 'Corridor', 'Outdoors']: line_bot_api.reply_message( event.reply_token, confirm_push(data.get_all_users(), userid, event.postback.data, db)) #戳題目的confirm template的時候 try: parse = extract(event.postback.data) #[0]是類別;[1]是相對題號;[2]是沒問題/待改進 cat, Q, ans = parse # data[userid]["current"] = (cat, Q) data.set_user_current(userid, (cat, Q)) ret = None last = len(get_category(cat, db)) #處理carousel template #填完該類別最後一題且最後一題是沒問題 if Q == last and ans == 'OK': # data[userid]["status"] = "00" data.set_user_status(userid, "00") # data[userid]["Answered"][cat].append(Q) data.add_user_answered(userid, cat, Q) # output = data[userid]["feedback"] output = data.get_user_feedback(userid) print('進入【(第一次)類別TEMPVIEW】') print(f"\n\n====\ncat: |{cat}\nOutput: |{output}\n") ret = cat_tempview_confirm(cat, output, db) #推第一次類別修改tempview confirm template ct_container = ct_push(data.get_all_users(), userid, 1, 0, db) #QC填完 if cat == "Quick" and Q == last: print('進入【最終TEMPVIEW】──QC的路,不要怕上一句話,因為她是必經之路') # output = data[userid]["feedback"] output = data.get_user_feedback(userid) ret = tempview_confirm(output, db) #推第一次最終修改tempview confirm template #處理題目的confirm template #待改進的話,或是非該類別的最後一題 else: # ret, data[userid]["status"] = next(data, userid, parse, db) ret, _ = next(data.get_all_users(), userid, parse, db) data.set_user_status(userid, _) line_bot_api.reply_message(event.reply_token, ret) except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print('ERROR:', exc_type, exc_obj, fname, exc_tb.tb_lineno) print(event.postback.data) if event.postback.data == 'edit=NO': # data[userid]["status"] = "00" data.set_user_status(userid, "00") print('進入【最終修改答案不修改】,結束問卷') # output = data[userid]["feedback"] #填完了消滅它 output = data.get_user_feedback(userid) print(f"\nThis is feedback:\n{output}\n") # data.pop(userid) data.remove_user(userid) get_feedback(output, userid, db) #寫進資料庫 ret = [ TextSendMessage(text="已收到您的回覆~謝謝您的貢獻!"), StickerSendMessage(package_id=11537, sticker_id=52002739), ] line_bot_api.reply_message(event.reply_token, ret) if event.postback.data == 'edit=OK': print('進入【最終修改答案要修改】,要求輸入修改題號') # data[userid]["status"] = "20" #表示【最終修改答案要修改】 data.set_user_status(userid, "20") ret = [ TextSendMessage(text="請問您要修改哪一題呢?"), TextSendMessage(text="【注意】:當您填寫快速檢核時,不能修改其他四類問題;反之亦然。"), TextSendMessage( text= "請按照下列格式填寫:\n一般檢查(Normal)簡寫為N\n室內(Indoors)簡寫為I\n走廊(Corridor)簡寫為C\n室外(Outdoors)簡寫為O\n再加上題號,例如:\nN7(一般檢查的第七題)" ), ] line_bot_api.reply_message(event.reply_token, ret) if event.postback.data == 'cat_edit=NO': print('進入【類別修改答案不修改】,丟出類別選單') ct_container = ct_push(data.get_all_users(), userid, 1, 1, db) if ct_container == "All cats have already checked!": # 類別全部修改過後,進入最終環節 print('進入【最終TEMPVIEW】──標準填完了唷,不要怕上一句話,因為她是必經之路') # output = data[userid]["feedback"] output = data.get_user_feedback(userid) ret = tempview_confirm(output, db) else: carousel_template = CarouselTemplate(columns=ct_container) ret = TemplateSendMessage(alt_text='問卷選單', template=carousel_template) line_bot_api.reply_message(event.reply_token, ret) if 'cat_edit=OK' in event.postback.data: print('進入【類別修改答案要修改】,要求輸入修改題號') # data[userid]["status"] = "10" #表示【類別修改答案要修改】 data.set_user_status(userid, "10") ## 避免戳 Normal1, Indoors1, Corridor1, Outdoors1 的時候類別被鎖住 cate = str(event.postback.data).split( ';')[1] if ';' in event.postback.data else '' print('\n===<%s>===\n' % cate) relq = user_data["current"][1] # data[userid]["current"] = (cate, relq) if cate != '' else data[userid]["current"] _ = (cate, relq) if cate != '' else user_data["current"] data.set_user_current(userid, _) ret = [ TextSendMessage(text="請問您要修改哪一題呢?"), TextSendMessage(text="【注意】:只能修改當前題組,欲修改其他題組,請於所有問題答畢後修改"), TextSendMessage( text= "請按照下列格式填寫:\n一般檢查(Normal)簡寫為N\n室內(Indoors)簡寫為I\n走廊(Corridor)簡寫為C\n室外(Outdoors)簡寫為O\n再加上題號,例如:\nN7(一般檢查的第七題)" ), ] line_bot_api.reply_message(event.reply_token, ret) ################################## ####### 類別修改答案的過程 ######### ################################## if 'cat_revise=' in event.postback.data and 'OK' in event.postback.data: #沒問題 print('進入【類別修改答案沒問題】,丟出cat_tempview') # output = data[userid]["feedback"] output = data.get_user_feedback(userid) ret = cat_tempview_confirm(user_data["current"][0], output, db) #把它目前的回答推個confirm template給他看看 line_bot_api.reply_message(event.reply_token, ret) elif 'cat_revise=' in event.postback.data and 'NO' in event.postback.data: #待改進 print('進入【類別修改答案待改進】,請簡述災情') line_bot_api.reply_message(event.reply_token, TextSendMessage(text="請簡述災情")) # data[userid]["status"] = "11" data.set_user_status(userid, "11") ################################## ####### 最終修改答案的過程 ######### ################################## if 'all_revise=' in event.postback.data and 'OK' in event.postback.data: #沒問題 print('進入【最後修改答案沒問題】,丟出tempview') # output = data[userid]["feedback"] output = data.get_user_feedback(userid) ret = tempview_confirm(output, db) #把它目前的回答推個confirm template給他看看 line_bot_api.reply_message(event.reply_token, ret) elif 'all_revise=' in event.postback.data and 'NO' in event.postback.data: #待改進 print('進入【最後修改答案待改進】,請簡述災情') line_bot_api.reply_message(event.reply_token, TextSendMessage(text="請簡述災情")) # data[userid]["status"] = "21" data.set_user_status(userid, "21") ################################## ########## 重設帳號或不設 ######### ################################## if event.postback.data == 'account_reset': delete_userid_db(userid, db) account[userid] = { 'userid': userid, 'name': 0, 'county': 0, 'school': 0, 'phone': 0 } ret = TextSendMessage(text="請問您尊姓大名?") line_bot_api.reply_message(event.reply_token, ret) account_q = 1 elif event.postback.data == 'account_remain': line_bot_api.reply_message(event.reply_token, TextSendMessage(text="好的,謝謝😁"))
def handle_text_message(event): text = event.message.text userid = event.source.user_id user_data = data.get_user(userid) # if text == 'IU': # floor = floor_plan() # line_bot_api.reply_message( # event.reply_token, [floor]) if text == '請給我使用須知': ret1 = TextSendMessage( text="歡迎使用本平台😁\n本平台是作為學校校安機關的安全檢核系統\n目前功能僅有表單檢核功能") ret2 = TextSendMessage( text= "【填寫表單須知】:\n您可以透過點選選單中的問卷按鈕,或是輸入「問卷」來呼叫問卷。\n本問卷提供兩種填答方式:\n\n1.快速檢核:若情況緊急,請使用此捷徑\n2.常規問卷:共分成四類選單,可交叉填答\n【注意】:兩種填寫方式不可交叉填寫" ) ret3 = TextSendMessage(text="本次填寫的事件為:" + str(get_latest_assessment_id_db(db)[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2] + [ret3]) elif text == '我要統計資料' or text == 'I need the summary of inspection results.': ret = TextSendMessage( text="https://pmdiana.hcilab.katrina.tw/report/index.html") line_bot_api.reply_message(event.reply_token, ret) elif text == '請給我表單填寫' or text == 'I need the questionnaire.': if userid not in get_yitianda_db( get_latest_assessment_id_db(db)[0], db): #確認是否填答過最新事件的問卷 if userid not in data.get_all_users( ): #沒有USERID的話,add key(第一次填寫的時候) data.add_user(userid) ct_container = ct_push(data.get_all_users(), userid, 0, 0, db) # 前面ㄉ0代表推出【快速/標準carousel】 # 後面ㄉ0代表非(類別不修改) carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='災情回覆問卷', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif user_data['Answered']['Quick'] != []: #選擇快速檢核後,阻止其跳回標準檢核 line_bot_api.reply_message( event.reply_token, TextSendMessage(text="您已選擇快速檢核!請填頁面上的最後一題")) else: ct_container = ct_push(data.get_all_users(), userid, 1, 0, db) #1代表推出【四大題組carousel】 carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='問卷選單', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) else: ret1 = TextSendMessage(text="您已為本次事件提供災情回覆咯~") ret2 = TextSendMessage(text="本次填寫的事件為:" + str(get_latest_assessment_id_db(db)[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2]) elif user_data["status"] == "01" and "題已回覆" not in text: #首次填答問卷選擇【待改進】 print('進入【首次填答待改進】') cat, Q = user_data["current"] ret = None # user_data["Answered"][cat].append(Q) # user_data["current"] += (text,) # user_data["feedback"].append((cat, Q, text)) #紀錄(題號, 待改進內容) data.add_user_answered(userid, cat, Q) _ = data.get_user_current(userid) + (text, ) data.set_user_current(userid, _) data.add_user_feedback(userid, (cat, Q, text)) ## 請使用者點選問題所在位置 floor = floor_plan() line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + [TextSendMessage(text="請點選問題所在位置(九宮格)")] + [floor]) data.set_user_status(userid, "02") # 等待使用者戳點 # 使用者要戳 平面圖 elif user_data["status"] == "02": if text in ['上左', '上中', '上右', '中左', '中中', '中右', '下左', '下中', '下右']: # idx = user_data["feedback"].index(user_data["current"]) # find the corresponding idx in feedback # user_data["feedback"][idx] += (text,) # user_data["current"] += (text,) data.set_user_spec_feedback_pos(userid, text) _ = data.get_user_current(userid) + (text, ) data.set_user_current(userid, _) print(f"\n\n--\n{event}\n\--n\n\n") data.set_user_status(userid, "03") # 等他傳照片 line_bot_api.reply_message( event.reply_token, [TextSendMessage(text="請傳照片,並稍後照片上傳時間(約數秒)🙏")]) else: line_bot_api.reply_message(event.reply_token, [TextSendMessage(text="請點選上方平面圖!")]) #類別/最終修改答案,告訴系統要改的題目(EG, C8) elif user_data["status"] in ["10", "20"] and "題已回覆" not in text: try: if revise_able(revise_extract(text)[0], revise_extract(text)[1]) is True: cat = revise_extract(text)[0] i = revise_extract(text)[1] #相對題號 # 禁止跨類別條件: 同類別 or 最終檢查 if cat == user_data["current"][0] or user_data[ "status"] == "20": # user_data["current"] = (cat, i) data.set_user_current(userid, (cat, i)) # user_data['Answered'][cat].remove(i) #從已填答拿掉 data.del_user_answered(userid, cat, i) newlist = [] for j in range(len( user_data["feedback"])): #從feedback拿掉要改的題的資料 if not (cat == user_data["feedback"][j][0] and i == user_data["feedback"][j][1]): newlist.append(user_data["feedback"][j]) # user_data["feedback"] = newlist data.set_user_feedback(userid, newlist) #丟confirm if user_data["status"] == "10": #各類別要改答案 print('\n進入【類別修改答案】') ret = [cat_revise_confirm(cat, i, db)] elif user_data["status"] == "20": # 最終要改答案 print('\n進入【最終修改答案】') ret = [revise_confirm(cat, i, db)] # user_data["Answered"][cat].append(i)#加入已填答 data.add_user_answered(userid, cat, i) else: ret = TextSendMessage(text="請修改當前類別:%s." % user_data["current"][0]) line_bot_api.reply_message(event.reply_token, ret) else: ret = revise_idiot(text, revise_extract(text)[0], revise_extract(text)[1]) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ret)) except: print("類別修改 題號判斷 失敗了喔喔喔喔喔喔喔") # 處理類別/最終改答案的時候,輸入的待改進的內容(EG, 哈囉MO) elif user_data["status"] in ["11", "21"] and "題已回覆" not in text: cat, Q = user_data["current"] # user_data["feedback"].append((cat, Q, text, "N/A", "N/A")) # 紀錄(題號, 待改進內容) # user_data["Answered"][cat].append(Q) data.add_user_feedback(userid, (cat, Q, text, "N/A", "N/A")) data.add_user_answered(userid, cat, Q) # output = user_data["feedback"] output = data.get_user_feedback(userid) if user_data["status"] == "21": #最終修改答案選待改進,revise_result會是false print('進入【最終修改待改進】') # user_data["status"] = "20" data.set_user_status(userid, "20") ret = tempview_confirm(output, db) elif user_data["status"] == "11": #類別修改答案選待改進,cat_revise_result會是false print('進入【類別修改待改進】') # user_data["status"] = "10" data.set_user_status(userid, "10") ret = cat_tempview_confirm(cat, output, db) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + ret) elif text == '我要設定帳號': ret = [ TextSendMessage(text="此功能暫時關閉,若您有緊急事務,請洽詢水利署。"), StickerSendMessage(package_id=11537, sticker_id=52002770), ] line_bot_api.reply_message(event.reply_token, ret)
def text_process(text, userid, data, DB, event): """ input: 1. text: request 2. userid 3. data: user's status 4. DB: database 5. event: just for liangyuan return: 1. [message] """ user_data = data.get_user(userid) ret = "NA" if text == '我要設定帳號': ret = [ TextSendMessage(text="此功能暫時關閉。"), StickerSendMessage(package_id=11537, sticker_id=52002770), ] elif text == '請給我使用須知': ret1 = TextSendMessage( text="歡迎使用本平台😁\n本平台是作為學校校安機關的安全檢核系統\n目前功能僅有表單檢核功能") ret2 = TextSendMessage( text= "【填寫表單須知】:\n您可以透過點選選單中的問卷按鈕,或是輸入「問卷」來呼叫問卷。\n本問卷提供兩種填答方式:\n\n1.快速檢核:若情況緊急,請使用此捷徑\n2.常規問卷:共分成四類選單,可交叉填答\n【注意】:兩種填寫方式不可交叉填寫" ) ret3 = TextSendMessage(text="本次填寫的事件為:" + str(DB.get_latest_assessment_id_db()[1])) ret = ([ret1] + [ret2] + [ret3]) elif text == '我要統計資料' or text == 'I need the summary of inspection results.': ret = [ TextSendMessage( text="https://pmdiana.hcilab.katrina.tw/report/index.html") ] # Questionnaire Process if userid not in data.get_all_users() and userid not in DB.get_yitianda_db( ): data.add_user(userid) # print("新用戶!") if userid in DB.get_yitianda_db(): ret1 = TextSendMessage(text="您已為本次事件提供災情回覆咯~") ret2 = TextSendMessage(text="本次填寫的事件為:" + str(DB.get_latest_assessment_id_db()[1])) ret = [ret1] + [ret2] else: ### Module 1: liangyuan ### if data.get_user_status(userid) == "pre": print("第一模組") signal = toCheck(event, line_bot_api) data.set_user_status(userid, signal[0]) if signal[1] != "": # Record building name # building_id = DB.get_building_id(signal[1]) # print(building_id) data.set_user_building(userid, signal[1]) # 紀錄建築名字 ret = "NOREPLY" ### Module 2 ### elif data.get_user_status(userid) == "pre-class": # module 1 跟 2 的街口 if data.get_user_current(userid) == (): # 第一次填寫的時候 line_bot_api.unlink_rich_menu_from_user(userid) ct_container = ct_push(data.get_all_users(), userid, 0, 0, DB) # 前面ㄉ0代表推出【快速/標準carousel】 # 後面ㄉ0代表非(類別不修改) data.set_user_status(userid, "00") data.set_user_building(userid, text) # 紀錄教室名字 carousel_template = CarouselTemplate(columns=ct_container) ret = [ TemplateSendMessage(alt_text='災情回覆問卷', template=carousel_template) ] elif user_data['Answered']['Quick'] != []: #選擇快速檢核後,阻止其跳回標準檢核 ret = [TextSendMessage(text="您已選擇快速檢核!請填頁面上的最後一題")] else: ct_container = ct_push(data.get_all_users(), userid, 1, 0, DB) #1代表推出【四大題組carousel】 carousel_template = CarouselTemplate(columns=ct_container) ret = [ TemplateSendMessage(alt_text='問卷選單', template=carousel_template) ] elif user_data["status"] == "01" and ("題已回覆" not in text) and ('請簡述' not in text): #首次填答問卷選擇【待改進】 print('進入【首次填答待改進】') cat = user_data["current"][0] Q = user_data["current"][1] ret = None data.add_user_answered(userid, cat, Q) _ = data.get_user_current(userid) if len(_) == 2: _ += (text, ) else: _ = ( _[0], _[1], text, ) data.set_user_current(userid, _) ent = ner_sent(text) if not (len(ent["event"]) > 0 and len(ent["location"]) > 0 and len(ent["product_name"]) > 0): complement = [] if len(ent["event"]) == 0: complement.append("事件") if len(ent["location"]) == 0: complement.append("地點") if len(ent["product_name"]) == 0: complement.append("物品名稱") ret = ([TextSendMessage(text=f"請提供{complement}!")]) else: # 請使用者點選問題所在位置 data.add_user_feedback(userid, (cat, Q, text)) floor = floor_plan() ret = ([TextSendMessage(text='『' + text + '』已收到回覆')] + [TextSendMessage(text="請點選問題所在位置(九宮格)")] + [floor]) data.set_user_status(userid, "02") # 等待使用者戳點 # 使用者要戳 平面圖 elif user_data["status"] == "02": if text in ['上左', '上中', '上右', '中左', '中中', '中右', '下左', '下中', '下右']: data.set_user_spec_feedback_pos(userid, text) _ = data.get_user_current(userid) + (text, ) data.set_user_current(userid, _) data.set_user_status(userid, "03") # 等他傳照片 ret = [TextSendMessage(text="請傳照片,並稍後照片上傳時間(約數秒)🙏")] else: ret = [TextSendMessage(text="請點選上方平面圖!")] #類別/最終修改答案,告訴系統要改的題目(EG, C8) elif user_data["status"] in ["10", "20"] and "題已回覆" not in text: try: if revise_able( revise_extract(text)[0], revise_extract(text)[1]) is True: cat = revise_extract(text)[0] i = revise_extract(text)[1] #相對題號 # 禁止跨類別條件: 同類別 or 最終檢查 if cat == user_data["current"][0] or user_data[ "status"] == "20": data.set_user_current(userid, (cat, i)) data.del_user_answered(userid, cat, i) newlist = [] for j in range(len( user_data["feedback"])): #從feedback拿掉要改的題的資料 if not (cat == user_data["feedback"][j][0] and i == user_data["feedback"][j][1]): newlist.append(user_data["feedback"][j]) data.set_user_feedback(userid, newlist) #丟confirm if user_data["status"] == "10": #各類別要改答案 print('\n進入【類別修改答案】') ret = [cat_revise_confirm(cat, i, DB)] elif user_data["status"] == "20": # 最終要改答案 print('\n進入【最終修改答案】') ret = [revise_confirm(cat, i, DB)] else: print( f"\n\n\n\n\nAARRRRRRRRRRRRRRRRRRR\n待改進不是類別也不是最終\n\n\n\n" ) data.add_user_answered(userid, cat, i) else: ret = [ TextSendMessage(text="請修改當前類別:%s." % user_data["current"][0]) ] else: ret = [ TextSendMessage( text=revise_idiot(text, revise_extract(text)[0], revise_extract(text)[1])) ] except: print("類別修改 題號判斷 失敗了喔喔喔喔喔喔喔") # 處理類別/最終改答案的時候,輸入的待改進的內容(EG, 哈囉MO) elif user_data["status"] in ["11", "21"] and "題已回覆" not in text: cat, Q = user_data["current"] data.add_user_feedback(userid, (cat, Q, text, "N/A", "N/A")) data.add_user_answered(userid, cat, Q) output = data.get_user_feedback(userid) if user_data["status"] == "21": #最終修改答案選待改進,revise_result會是false print('進入【最終修改待改進】') data.set_user_status(userid, "20") ret = tempview_confirm(output, DB) elif user_data[ "status"] == "11": #類別修改答案選待改進,cat_revise_result會是false print('進入【類別修改待改進】') data.set_user_status(userid, "10") ret = cat_tempview_confirm(cat, output, DB) return ret
def handle_postback(event): userid = event.source.user_id #取得Userid global parse_no global result global revise_result global EPD global account global account_q ################################## ########## 填問卷的過程 ########## ################################## #QC丟問題,相對題號 if event.postback.data == 'Quick': line_bot_api.reply_message( event.reply_token, confirm_push(data, userid, event.postback.data, db)) #四類丟問題,相對題號 elif event.postback.data in ['Normal', 'Indoors', 'Corridor', 'Outdoors']: line_bot_api.reply_message( event.reply_token, confirm_push(data, userid, event.postback.data, db)) #戳題目的confirm template的時候 try: parse = extract(event.postback.data) #[0]是絕對題號;[1]是OK/NO ret = None cat = '' last = 0 parse_no = parse[0] #給定各類別的最後一題 if parse[0] in list(range(65, 78)): last = 77 cat = 'Quick' elif parse[0] in list(range(1, 13)): last = 12 cat = 'Normal' elif parse[0] in list(range(13, 33)): last = 32 cat = 'Indoors' elif parse[0] in list(range(33, 46)): last = 45 cat = 'Corridor' elif parse[0] in list(range(46, 65)): last = 64 cat = 'Outdoors' #處理carousel template #填完該類別最後一題且最後一題是沒問題 if parse[0] == last and parse[1] == 'OK': data[userid][cat] += 1 ct_container = ct_push(data, userid) data[userid]["Answered"].append(parse[0]) #QC填完 or 全部都填過了 if parse[0] == 77 or ct_container == [ Normal1, Indoors1, Corridor1, Outdoors1 ]: output = feedback[userid] ret = tempview_confirm(output, db) #把它目前的回答推個confirm templatea給他看看 #有類別沒填完 else: carousel_template = CarouselTemplate(columns=ct_container) ret = TemplateSendMessage(alt_text='問卷選單', template=carousel_template) #處理題目的confirm template #待改進的話,或是非該類別的最後一題 else: ret, result = next(data, userid, cat, parse, db) EPD = parse[0] if result is False else EPD line_bot_api.reply_message(event.reply_token, ret) except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print('ERROR:', exc_type, exc_obj, fname, exc_tb.tb_lineno) print(event.postback.data) if event.postback.data == 'edit=NO': print('AARRR') output = feedback.pop(userid) #填完了消滅它 data.pop(userid) get_feedback(output, userid, db) #寫進資料庫 ret = [ TextSendMessage(text="已收到您的回覆~謝謝您的貢獻!"), StickerSendMessage(package_id=11537, sticker_id=52002739), ] line_bot_api.reply_message(event.reply_token, ret) if event.postback.data == 'edit=OK': print('AARRR') ret = [ TextSendMessage(text="請問您要修改哪一題呢?"), TextSendMessage( text= "【注意】:當您填寫快速檢核時,不能修改其他四類問題;反之亦然。\n\n請按照下列格式填寫,例如:\nNormal Q7" ), ] line_bot_api.reply_message(event.reply_token, ret) ################################## ########## 修改答案的過程 ######### ################################## if 'revise=' in event.postback.data and 'OK' in event.postback.data: #沒問題 output = feedback[userid] ret = tempview_confirm(output, db) #把它目前的回答推個confirm template給他看看 line_bot_api.reply_message(event.reply_token, ret) elif 'revise=' in event.postback.data and 'NO' in event.postback.data: #待改進 line_bot_api.reply_message(event.reply_token, TextSendMessage(text="請簡述災情")) revise_result = False ################################## ########## 重設帳號或不設 ######### ################################## if event.postback.data == 'account_reset': delete_userid_db(userid, db) account[userid] = { 'userid': userid, 'name': 0, 'county': 0, 'school': 0, 'phone': 0 } ret = TextSendMessage(text="請問您尊姓大名?") line_bot_api.reply_message(event.reply_token, ret) account_q = 1 elif event.postback.data == 'account_remain': line_bot_api.reply_message(event.reply_token, TextSendMessage(text="好的,謝謝😁"))
def handle_text_message(event): text = event.message.text userid = event.source.user_id global data global feedback global revise_result global revise_EPD global account global account_q if text == '請給我使用須知': ret1 = TextSendMessage( text="歡迎使用本平台😁\n本平台是作為學校校安機關的安全檢核系統\n目前功能僅有表單檢核功能") ret2 = TextSendMessage( text= "【填寫表單須知】:\n您可以透過點選選單中的問卷按鈕,或是輸入「問卷」來呼叫問卷。\n本問卷提供兩種填答方式:\n\n1.快速檢核:若情況緊急,請使用此捷徑\n2.常規問卷:共分成四類選單,可交叉填答\n【注意】:兩種填寫方式不可交叉填寫" ) ret3 = TextSendMessage(text="本次填寫的事件為:" + str(get_latest_assessment_id_db(db)[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2] + [ret3]) if text == '我要統計資料': ret = TextSendMessage( text="https://pmdiana.hcilab.katrina.tw/report/index.html") line_bot_api.reply_message(event.reply_token, ret) if text == '請給我表單填寫': if userid not in get_yitianda_db( get_latest_assessment_id_db(db)[0], db): #確認他有沒有填過這次問卷 if userid not in data: #沒有USERID的話,add key(第一次填寫的時候) 然後推處死carousel data[userid] = { "Quick": 0, "Normal": 0, "Indoors": 0, "Corridor": 0, "Outdoors": 0, "Answered": [] } feedback[userid] = [] ct_container = ct_push(data, userid) #把4類別加進來 carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='災情回覆問卷', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) elif data[userid]['Quick'] != 0: #QC填到一半智障又打一次carousel line_bot_api.reply_message( event.reply_token, TextSendMessage(text="您已選擇快速檢核!請填頁面上的最後一題")) else: ct_container = ct_push(data, userid) carousel_template = CarouselTemplate(columns=ct_container) template_message = TemplateSendMessage( alt_text='問卷選單', template=carousel_template) line_bot_api.reply_message(event.reply_token, template_message) else: ret1 = TextSendMessage(text="您已為本次事件提供災情回覆咯~") ret2 = TextSendMessage(text="本次填寫的事件為:" + str(get_latest_assessment_id_db(db)[1])) line_bot_api.reply_message(event.reply_token, [ret1] + [ret2]) elif '已回覆待改進' not in text and '已回覆沒問題' not in text and 'Normal' not in text and 'Indoors' not in text and 'Corridor' not in text and 'Outdoors' not in text: global result #就是要 global EPD if result is False: #如果confirm templates 填待改進的話,他就會是 False cat = '' last = 0 ret = None #下一題的confirm result = True #把值改回來 feedback[userid].append((EPD, text)) #紀錄(題號, 廢話) data[userid]["Answered"].append(EPD) if EPD in list(range(65, 78)): last = 77 cat = 'Quick' elif EPD in list(range(1, 13)): last = 12 cat = 'Normal' elif EPD in list(range(13, 33)): last = 32 cat = 'Indoors' elif EPD in list(range(33, 46)): last = 45 cat = 'Corridor' elif EPD in list(range(46, 65)): last = 64 cat = 'Outdoors' if EPD == last: data[userid][cat] += 1 #待改進填到最後一題+1 ct_container = ct_push(data, userid) if EPD == 77 or ct_container == [ Normal1, Indoors1, Corridor1, Outdoors1 ]: output = feedback[userid] ret = tempview_confirm(output, db) else: carousel_template = CarouselTemplate(columns=ct_container) ret = [ TemplateSendMessage( alt_text='問卷選單', template=carousel_template, ) ] else: data[userid][cat] += 1 #待改進沒填到最後一題+1 ret = [confirm(cat, data[userid][cat], db)] line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + ret) #要改答案 try: if revise_able(revise_extract(text)[0], revise_extract(text)[1]) is True: cat = revise_extract(text)[0] i = revise_extract(text)[1] #相對題號 no = converter(cat, i) #絕對題號 revise_EPD = no data[userid]['Answered'].remove(no) #從已填答拿掉 newlist = [] for j in range(len(feedback[userid])): #從feedback拿掉 if no != feedback[userid][j][0]: newlist.append(feedback[userid][j]) feedback[userid] = newlist #丟confirm ret = [revise_confirm(cat, i, db)] data[userid]["Answered"].append(no) #加入已填答 line_bot_api.reply_message(event.reply_token, ret) else: ret = revise_idiot(text, revise_extract(text)[0], revise_extract(text)[1]) line_bot_api.reply_message(event.reply_token, TextSendMessage(text=ret)) except: pass #處理改答案得時候,他要待改進 if '已回覆待改進' not in text and '已回覆沒問題' not in text and 'Normal' not in text and 'Indoors' not in text and 'Corridor' not in text and 'Outdoors' not in text: if revise_result is False: revise_result = True feedback[userid].append((revise_EPD, text)) #紀錄(題號, 廢話) data[userid]["Answered"].append(revise_EPD) output = feedback[userid] ret = tempview_confirm(output, db) line_bot_api.reply_message( event.reply_token, [TextSendMessage(text='『' + text + '』已收到回覆')] + ret) if text == '我要設定帳號': if userid in get_userid_db(db): #已經填過了,問她要不要再改 line_bot_api.reply_message(event.reply_token, account_confirm()) elif userid not in get_userid_db(db): #第一次設 account[userid] = { 'userid': userid, 'name': 0, 'county': 0, 'school': 0, 'phone': 0 } ret1 = TextSendMessage(text="【注意】:請一次設定完成") ret2 = TextSendMessage(text="請問您尊姓大名?") line_bot_api.reply_message(event.reply_token, [ret1] + [ret2]) account_q = 1 elif account_q == 1: account[userid]['name'] = text ret = TextSendMessage(text="請問您的所在縣市?") line_bot_api.reply_message(event.reply_token, ret) account_q += 1 elif account_q == 2: text = '臺北市' if text == '台北市' else text account[userid]['county'] = text if text not in get_county_db(db): ret = TextSendMessage( text="不好意思,您所輸入的縣市不在我國疆域。提醒您中華民國採用繁體中文😁\n【請重新設定帳戶】") line_bot_api.reply_message(event.reply_token, ret) account.pop(userid) account_q = 0 else: ret = TextSendMessage(text="請問您所在學校名稱為何?") line_bot_api.reply_message(event.reply_token, ret) account_q += 1 elif account_q == 3: account[userid]['school'] = text if text not in get_school_db(account[userid]['county'], db): ret = TextSendMessage(text="您的學校尚未與本平台合作,請聯絡我們") line_bot_api.reply_message(event.reply_token, ret) account.pop(userid) account_q = 0 else: if text in no_repeat_school_db(account[userid]['county'], db): ret = TextSendMessage(text="您的學校已有負責人,請洽詢主管") line_bot_api.reply_message(event.reply_token, ret) account.pop(userid) account_q = 0 else: account_q += 1 ret = TextSendMessage(text="請問您的連絡電話?") line_bot_api.reply_message(event.reply_token, ret) elif account_q == 4: account_q = 0 account[userid]['phone'] = text ret = TextSendMessage(text="謝謝您的填答,您的身分已確認😁😁") line_bot_api.reply_message(event.reply_token, ret) get_account_db(account[userid], db) print(account[userid])