def process_postback_event(event): result_message_array = [] replyJsonPath = r"./素材/{0}/reply.json".format(event.postback.data) print("replyJsonPath:", replyJsonPath) print("answer:", event.postback.data.find(":::Q")) user_profile = line_bot_api.get_profile(event.source.user_id) print("user_profile:", type(json.dumps(vars(user_profile), sort_keys=True))) userName = json.loads(json.dumps(vars(user_profile), sort_keys=True))["display_name"] if event.postback.data == "回答幾個問題讓我們更了解您的旅遊喜好吧": replyJsonPath = r"./素材/Q1/reply.json" result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find(":::Q") >= 1: idx = event.postback.data[5:6] print("idx:", idx) if idx == "5": print("return result") timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] key = "{0}-{1}".format(userName, timestamp) util.redisLPush(key, answer) allAnswer = util.redisLRange(key, 0, -1) print("allAnswer:", allAnswer) doc = {} score = 0 for an in allAnswer: doc[an] = score + 1 util.sendKafkaMsg("questionaire", doc, event.reply_token)\ elkDoc = {} elkDoc["key"] = key elkDoc["UserName"] = userName elkDoc["DateTime"] = datetime.datetime.now() for i, an in enumerate(allAnswer): elkDoc["item{0}".format(i + 1)] = an util.insertELK("questionaire", elkDoc) #把redis資料清空 util.redisLPopAll(key) else: timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] util.redisLPush("{0}-{1}".format(userName, timestamp), answer) replyJsonPath = r"./素材/Q{0}/reply.json".format(int(idx) + 1) result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find("Photo") > 1: ## 點擊後,切換至照片相簿選擇 cameraRollQRB = QuickReplyButton(action=CameraRollAction(label="上傳照片")) quickReplyList = QuickReply(items=[cameraRollQRB]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='上傳照片解鎖旅遊成就', quick_reply=quickReplyList)) elif event.postback.data.find("Camera") > 1: ## CameraAction cameraQuickReplyButton = QuickReplyButton(action=CameraAction( label="拍照解鎖")) quickReplyList = QuickReply(items=[cameraQuickReplyButton]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='拍照解鎖旅遊成就', quick_reply=quickReplyList)) else: result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array)
def process_postback_event(event): result_message_array = [] replyJsonPath = r"./static/material/{0}/reply.json".format( event.postback.data) #print("replyJsonPath:", replyJsonPath) #print("answer:", event.postback.data.find(":::Q")) user_profile = line_bot_api.get_profile(event.source.user_id) #print("user_profile:", type(json.dumps(vars(user_profile),sort_keys=True))) userName = json.loads(json.dumps(vars(user_profile), sort_keys=True))["display_name"] cityMapping = {"TPE": "雙北", "KL": "基隆市", "IL": "宜蘭縣", "TY": "桃園市"} if event.postback.data == "question": replyJsonPath = r"./static/material/Question1/reply.json" result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find(":::Q") >= 1: idx = event.postback.data[5:6] #print("idx:", idx) #if idx == "5": if idx == "4": #print("return result") timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] key = "{0}-{1}".format(userName, timestamp) try: util.redisLPush(key, answer) allAnswer = util.redisLRange(key, 0, -1) # print("allAnswer:", allAnswer) doc = {} score = 0 for an in allAnswer: score += 1 doc[util.answerMapping[score - 1][an]] = score util.sendKafkaMsg("questionaire", doc, key) elkDoc = {} elkDoc["key"] = key elkDoc["UserName"] = userName elkDoc["DateTime"] = datetime.datetime.now() mappingList = util.getMappingList() for i, an in enumerate(allAnswer): elkDoc["item{0}".format(i + 1)] = mappingList[i][an] util.insertELK("questionaire-2", elkDoc) except Exception as e: print(e) finally: #把redis資料清空 util.redisLPopAll(key) else: timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] util.redisLPush("{0}-{1}".format(userName, timestamp), answer) replyJsonPath = r"./static/material/Question{0}/reply.json".format( int(idx) + 1) result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find(":::R") >= 1: areaList = ["臺北市", "新北市", "基隆市", "宜蘭縣", "桃園市"] idx = int(event.postback.data[5:6]) #print("idx:", idx) siteList = util.getSiteListByArea(areaList[idx]) urlList = util.getUrlList() randomlist = random.sample([x for x in range(len(siteList))], 5) # print("randomlist:", randomlist) replyJsonPath = r"./static/material/recommedation/reply.json" with open(replyJsonPath, 'r', encoding="utf-8") as f: replyMsg = f.read() # print("replyMsg:", replyMsg) for i, r in enumerate(randomlist): replyMsg = replyMsg.replace("site{}".format(i + 1), siteList[r]) try: replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[r]) except: print("index out of range") # print("replyMsg:", replyMsg) line_bot_api.reply_message( event.reply_token, TemplateSendMessage.new_from_json_dict(json.loads(replyMsg))) elif event.postback.data.find("Photo") > 1: ## 點擊後,切換至照片相簿選擇 cameraRollQRB = QuickReplyButton(action=CameraRollAction(label="選擇照片")) quickReplyList = QuickReply(items=[cameraRollQRB]) key = "{0}-Photo".format(userName) util.redisDelKey(key) city = event.postback.data.split("_")[0] util.redisSetData(key, cityMapping[city]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='選擇照片', quick_reply=quickReplyList)) elif event.postback.data.find("Camera") > 1: ## CameraAction cameraQuickReplyButton = QuickReplyButton(action=CameraAction( label="拍照")) quickReplyList = QuickReply(items=[cameraQuickReplyButton]) key = "{0}-Photo".format(userName) util.redisDelKey(key) city = event.postback.data.split("_")[0] util.redisSetData(key, cityMapping[city]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='拍照上傳', quick_reply=quickReplyList)) elif event.postback.data == "recommedation": siteList = util.getSiteList() urlList = util.getUrlList() #randomlist = random.sample([x for x in range(1000)], 5) #print("randomlist:", randomlist) with open(replyJsonPath, 'r', encoding="utf-8") as f: replyMsg = f.read() timestamp = datetime.datetime.now().strftime("%Y%m%d") key = "{0}-{1}-re".format(userName, timestamp) if util.getRedis(True).exists(key): reSite = util.redisLRange(key, 0, -1) # print("reSite:", reSite) # for i, r in enumerate(randomlist): for i, r in enumerate(reSite): # replyMsg = replyMsg.replace("site{}".format(i + 1), siteList[r]) # replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[r]) replyMsg = replyMsg.replace("site{}".format(i + 1), r) replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[siteList.index(r)]) # print("replyMsg:", replyMsg) line_bot_api.reply_message( event.reply_token, TemplateSendMessage.new_from_json_dict(json.loads(replyMsg))) else: replyJsonPath = r"./static/material/Question1/reply.json" result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) else: result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array)
def process_postback_event(event): result_message_array = [] replyJsonPath = r"./static/material/{0}/reply.json".format( event.postback.data) #print("replyJsonPath:", replyJsonPath) #print("answer:", event.postback.data.find(":::Q")) user_profile = line_bot_api.get_profile(event.source.user_id) #print("user_profile:", type(json.dumps(vars(user_profile),sort_keys=True))) userName = json.loads(json.dumps(vars(user_profile), sort_keys=True))["display_name"] cityMapping = {"TPE": "雙北", "KL": "基隆市", "IL": "宜蘭縣", "TY": "桃園市"} if event.postback.data == "question": replyJsonPath = r"./static/material/Question1/reply.json" result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find(":::Q") >= 1: idx = event.postback.data[5:6] #print("idx:", idx) #if idx == "5": if idx == "4": #print("return result") timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] key = "{0}-{1}".format(userName, timestamp) try: util.redisLPush(key, answer) allAnswer = util.redisLRange(key, 0, -1) # print("allAnswer:", allAnswer) doc = {} score = 0 for an in allAnswer: score += 1 doc[util.answerMapping[score - 1][an]] = score util.sendKafkaMsg("questionaire", doc, key) elkDoc = {} elkDoc["key"] = key elkDoc["UserName"] = userName elkDoc["DateTime"] = datetime.datetime.now() mappingList = util.getMappingList() for i, an in enumerate(allAnswer): elkDoc["item{0}".format(i + 1)] = mappingList[i][an] util.insertELK("questionaire-2", elkDoc) except Exception as e: print(e) finally: #把redis資料清空 util.redisLPopAll(key) else: timestamp = datetime.datetime.now().strftime("%Y%m%d") answer = event.postback.data[9:] util.redisLPush("{0}-{1}".format(userName, timestamp), answer) replyJsonPath = r"./static/material/Question{0}/reply.json".format( int(idx) + 1) result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data.find(":::R") >= 1: areaList = ["臺北市", "新北市", "基隆市", "宜蘭縣", "桃園市"] idx = int(event.postback.data[5:6]) #print("idx:", idx) siteList = util.getSiteListByArea(areaList[idx]) urlList = util.getUrlList() randomlist = random.sample([x for x in range(len(siteList))], 5) # print("randomlist:", randomlist) replyJsonPath = r"./static/material/recommedation/reply.json" with open(replyJsonPath, 'r', encoding="utf-8") as f: replyMsg = f.read() # print("replyMsg:", replyMsg) for i, r in enumerate(randomlist): replyMsg = replyMsg.replace("site{}".format(i + 1), siteList[r]) try: replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[r]) except: print("index out of range") # print("replyMsg:", replyMsg) line_bot_api.reply_message( event.reply_token, TemplateSendMessage.new_from_json_dict(json.loads(replyMsg))) elif event.postback.data.find("Photo") > 1: ## 點擊後,切換至照片相簿選擇 cameraRollQRB = QuickReplyButton(action=CameraRollAction(label="選擇照片")) quickReplyList = QuickReply(items=[cameraRollQRB]) key = "{0}-Photo".format(userName) util.redisDelKey(key) city = event.postback.data.split("_")[0] util.redisSetData(key, cityMapping[city]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='選擇照片', quick_reply=quickReplyList)) elif event.postback.data.find("Camera") > 1: ## CameraAction cameraQuickReplyButton = QuickReplyButton(action=CameraAction( label="拍照")) quickReplyList = QuickReply(items=[cameraQuickReplyButton]) key = "{0}-Photo".format(userName) util.redisDelKey(key) city = event.postback.data.split("_")[0] util.redisSetData(key, cityMapping[city]) line_bot_api.reply_message( event.reply_token, TextSendMessage(text='拍照上傳', quick_reply=quickReplyList)) elif event.postback.data == "recommedation": siteList = util.getSiteList() urlList = util.getUrlList() #randomlist = random.sample([x for x in range(1000)], 5) #print("randomlist:", randomlist) with open(replyJsonPath, 'r', encoding="utf-8") as f: replyMsg = f.read() timestamp = datetime.datetime.now().strftime("%Y%m%d") key = "{0}-{1}-re".format(userName, timestamp) if util.getRedis(True).exists(key): reSite = util.redisLRange(key, 0, -1) # print("reSite:", reSite) # for i, r in enumerate(randomlist): for i, r in enumerate(reSite): # replyMsg = replyMsg.replace("site{}".format(i + 1), siteList[r]) # replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[r]) replyMsg = replyMsg.replace("site{}".format(i + 1), r) try: if urlList[siteList.index(r)] == "": assert ("url is empty") replyMsg = replyMsg.replace("http://{}".format(i + 1), urlList[siteList.index(r)]) except: print("index out of range") print("replyMsg:", replyMsg) line_bot_api.reply_message( event.reply_token, TemplateSendMessage.new_from_json_dict(json.loads(replyMsg))) else: replyJsonPath = r"./static/material/Question1/reply.json" result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array) elif event.postback.data == "hotsite": areaList = ["臺北市", "新北市", "基隆市", "宜蘭縣", "桃園市"] placeDic = { "新北市": [ "九份老街", "野柳地質公園", "淡水老街", "猴硐車站", "紅毛城", "鶯歌老街", "金瓜寮魚蕨步道", "朱銘美術館", "烏來風景區", "福隆海水浴場" ], "臺北市": [ "士林官邸", "饒河街觀光夜市", "北投圖書館", "國立故宮博物院", "中正紀念堂", "台北101", "軍艦岩親山步道", "陽明山夜景", "四四南村", "關渡碼頭", "士林觀光夜市" ], "基隆市": [ "和平島", "基隆廟口", "情人湖濱海大道", "阿根納造船廠", "白米甕砲台", "正濱漁港", "潮境公園", "劉銘傳隧道", "八斗子公園", "大武崙砲台" ], "桃園市": [ "小烏來天空步道", "石門水庫", "大溪老街", "拉拉山巨木群", "角板山行館", "東眼山森林遊樂區", "巴陵古道生態園區", "中原夜市", "小人國", "馬祖新村眷村文創園區" ], "宜蘭縣": [ "金車咖啡城堡", "外澳沙灘", "蘭陽博物館", "礁溪溫泉", "望龍埤", "福山植物園", "羅東夜市", "太平山森林遊樂區", "東澳灣‧粉鳥林漁港", "幾米主題廣場" ] } siteList = util.getSiteList() urlList = util.getUrlList() with open(replyJsonPath, 'r', encoding="utf-8") as f: replyMsg = f.read() idx = 1 for area in areaList: for i in range(3): r = placeDic[area][i] print(r) if idx == 1: replyMsg = replyMsg.replace("siteA", r) replyMsg = replyMsg.replace("https://A", urlList[siteList.index(r)]) elif idx == 2: replyMsg = replyMsg.replace("siteB", r) replyMsg = replyMsg.replace("https://B", urlList[siteList.index(r)]) else: replyMsg = replyMsg.replace("site{}".format(idx), r) replyMsg = replyMsg.replace("https://{}".format(idx), urlList[siteList.index(r)]) idx += 1 print("replyMsg:", replyMsg) line_bot_api.reply_message( event.reply_token, TemplateSendMessage.new_from_json_dict(json.loads(replyMsg))) else: result_message_array = detect_json_array_to_new_message_array( replyJsonPath) line_bot_api.reply_message(event.reply_token, result_message_array)
k2.append(k) else: k1.append(k) # k1 = ['宜蘭'] # k2 = ['戶外玩樂', '人文', '親子'] # print("k1:k2", k1, k2) reList = mt.do_compare(k1, k2) # mysqltest.sqlcon(k1) # ranking = pandas_main.pandascon(k2) # print("reList:", reList) rekey = "{}-re".format(msgKey) rc = util.getRedis(True) if (rc.exists(rekey)): rc.delete(rekey) for site in reList: util.redisLPush(rekey, site) elif topicName[t] == "recommendation": print("recommendation", msgKey) recommend_list = sr.spotRecommend(msgValue) for site in recommend_list: util.redisLPush(msgKey, site) print(recommend_list) elif topicName[t] == "picsimilarity": file = util.getRedisImg(msgKey) city = msgValue print("file path:", city, os.path.abspath(file)) ans = ps.pic_compare(city, os.path.abspath(file)) print(ans) util.redisSetData("{0}-pic".format(msgKey), str(ans))