def detect_json_array_to_new_message_array(fn): with open(fn, encoding='utf8') as f: jsonArray = json.load(f) returnArray = [] for jsonObject in jsonArray: message_type = jsonObject.get('type') if message_type == 'text': returnArray.append(TextSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'imagemap': returnArray.append(ImagemapSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'template': returnArray.append(TemplateSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'image': returnArray.append(ImageSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'sticker': returnArray.append(StickerSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'audio': returnArray.append(AudioSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'location': returnArray.append(LocationSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'flex': returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject)) return returnArray
def detect_json_array_to_new_message_array(fileName): ''' message_type 判斷器 讀取指定的 json檔,解析成不同格式的 SendMessage ''' with open(fileName, 'r', encoding='utf8') as f: jsonArray = json.load(f) newmessage_Array = [] for jsonObject in jsonArray: message_type = jsonObject.get('type') if message_type == 'text': newmessage_Array.append(TextSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'sticker': newmessage_Array.append(StickerSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'image': newmessage_Array.append(ImageSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'video': newmessage_Array.append(VideoSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'audio': newmessage_Array.append(AudioSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'location': newmessage_Array.append(LocationSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'imagemap': newmessage_Array.append(ImagemapSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'template': newmessage_Array.append(TemplateSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'flex': newmessage_Array.append(FlexSendMessage.new_from_json_dict(jsonObject)) return newmessage_Array
def detect_json_array_to_new_message_array(fileName): # 開啟檔案,轉成json with open(fileName, 'r', encoding="utf-8") as f: jsonArray = json.load(f) # 解析json returnArray = [] for jsonObject in jsonArray: # 讀取其用來判斷的元件 message_type = jsonObject.get('type') # 轉換 if message_type == 'text': returnArray.append(TextSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'imagemap': returnArray.append(ImagemapSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'template': returnArray.append(TemplateSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'image': returnArray.append(ImageSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'sticker': returnArray.append(StickerSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'audio': returnArray.append(AudioSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'location': returnArray.append(LocationSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'flex': returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject)) elif message_type == 'video': returnArray.append(FlexSendMessage.new_from_json_dict(jsonObject)) # 回傳 return returnArray
def handle_message(event): # 用json產生Imagemap物件 #with open('test1.json', 'r',encoding='utf8') as jsonfile: # json_object = json.load(jsonfile) json_object = { "type": "imagemap", "baseUrl": "https://i.imgur.com/lRAVpQ9.jpg", "altText": "This is an imagemap", "baseSize": { "width": 1040, "height": 868 }, "actions": [{ "type": "message", "area": { "x": 66, "y": 40, "width": 932, "height": 523 }, "text": "機車" }] } Imagemap_from_json = ImagemapSendMessage.new_from_json_dict(json_object) # 請line bot api回復文字訊息 line_bot_api.reply_message( event.reply_token, [TextSendMessage(text=event.message.text), Imagemap_from_json])
def handle_text_message(event): message_content: str = event.message.text if message_content in keywords_template_menu: with open(message_content + '/reply.json', 'r', encoding='utf8') as f: reply_json = json.load(f) reply_send_message = TemplateSendMessage.new_from_json_dict(reply_json) line_bot_api.reply_message(event.reply_token, reply_send_message) elif message_content in keywords_query_product_text: #之後建立好營養標示section要合併/刪除 with open("product_info_buttun/" + message_content + '/reply.json', 'r', encoding='utf8') as f: reply_json = json.load(f) reply_send_message = TemplateSendMessage.new_from_json_dict(reply_json) line_bot_api.reply_message(event.reply_token, reply_send_message) elif message_content in keywords_text: line_bot_api.reply_message( event.reply_token, TextSendMessage(keywords_text[message_content])) elif message_content in keywords_change_rich_menu: if message_content == '怎麼運送?': line_bot_api.reply_message( event.reply_token, TextSendMessage( '冷鏈(Cold Chain),是冷凍供應鏈,由冷凍加工、貯藏、物流運輸、銷售構成。\n\n冷鏈技術是消費者最終能否買到高品質產品重要的因素。' )) line_bot_api.link_rich_menu_to_user( event.source.user_id, keywords_change_rich_menu[message_content]) else: line_bot_api.link_rich_menu_to_user( event.source.user_id, keywords_change_rich_menu[message_content]) elif message_content in keywords_imagemap: if message_content == '產品選單': with open(message_content + '/reply.json', 'r', encoding='utf8') as f: reply_json = json.load(f) image_message = ImagemapSendMessage.new_from_json_dict(reply_json) line_bot_api.reply_message(event.reply_token, image_message) else: line_bot_api.reply_message(event.reply_token, TextSendMessage('看看選單其他功能吧'))
def handle_message(event): dest_langs = get_dest_langs(event.source.user_id) # 根據 根據對話判斷使用者活動時間,並更新使用時間 user_profile = line_bot_api.get_profile(event.source.user_id) user_dict = vars(user_profile) data_tuple = ( user_dict["display_name"], user_dict["user_id"], ) update_time(data_tuple) text = event.message.text if text[0] not in '@#:': # 主圖文選單開頭都為 @ trans = Translator() trans_text = trans.translate(event.message.text, src='zh-TW', dest=dest_langs).extra_data['translation'][0][0] tts = gTTS(trans_text, lang=dest_langs) tts_url = tts.get_urls()[0] line_bot_api.reply_message( event.reply_token, [ TextSendMessage(text=trans_text), AudioSendMessage( original_content_url=tts_url, duration=100000 ) ] ) elif text == "@作者": line_bot_api.reply_message( event.reply_token, TextSendMessage(text='不要認識的好') ) elif text == "@樂透": choose_image_map(event, 1), elif text == '#回主選單': choose_image_map(event, 0) else: if text == '@語言': flex_container = TextSendMessage(text='請選擇目標語言', quick_reply=quickReplyList) elif text == "@天氣": flex_container = ImagemapSendMessage.new_from_json_dict( json.load(open("./container/weather_map.json", 'r', encoding='utf8'))) elif text == "@油價": flex_container = flex_oil() elif text == "@發票": flex_container = flex_invoice() elif text == '#威力彩': flex_container = flex_lotto(0) elif text == '#大樂透': flex_container = flex_lotto(1) elif text == '#今彩539': flex_container = flex_lotto(2) elif text == '#雙贏彩': flex_container = flex_lotto(3) elif text == '#3星彩': flex_container = flex_lotto(4) elif text == '#4星彩': flex_container = flex_lotto(5) elif text == '#38樂合彩': flex_container = flex_lotto(6) elif text == '#49樂合彩': flex_container = flex_lotto(7) elif text == '#39樂合彩': flex_container = flex_lotto(8) elif text[:2] == '::': flex_container = flex_weather(text[2:]) try: line_bot_api.reply_message( event.reply_token, flex_container ) except: print('沒此功能')