def webhook(): if request.method == 'POST': pp = pprint.PrettyPrinter(indent=3) ### dictionary from line data = request.json data_show = pp.pprint(data) ## extract text from line text_fromline = data['events'][0]['message']['text'] ## ค้นหาคำจาก wikipedia result = search_wiki(text_fromline) ### import function ในการส่งmessage reply.py from reply import ReplyMessage ReplyMessage(Reply_token=data['events'][0]['replyToken'], TextMessage=result, Line_Access_Token=access_token ) return 'OK' elif request.method == 'GET': return 'นี้คือลิงค์เว็บสำหรับรับ package'
def webhook(): if request.method == 'GET': # check method return 'Your aer in Method' elif request.method == 'POST': # check method data = request.get_json() # รับข้อมูลรูปแบบ Json text = data['events'][0]['message']['text'] reply_token = data['events'][0]['replyToken'] print('text :' + text) print('reply_token' + reply_token) from Resource.wolf import search_wiki reply_msg = search_wiki(keyword='ff') from Resource.reply import ReplyMessage ReplyMessage(Reply_token=reply_token, TextMessage=reply_msg, Line_Access_Token=line_access_token) print(data) return 'OK'
def message_text(event): print(event) Text_fromUser = event.message.text reply_Token = event.reply_token if 'เช็คราคา' in Text_fromUser: from Resource.bxAPI import GetBxPrice msg = GetBxPrice(Number_to_get=10) from Resource.FlexMessage import setCarousel flex = setCarousel(msg) ### setup carousel flex = SetMenuMessage_Object(flex) ### setup flex send_flex(reply_Token , flex , channel_access_token) elif 'เช็คข่าวสาร' in Text_fromUser: from Resource.GetNews import get_cnn_news data = get_cnn_news() from Resource.FlexMessage import news_setbubble msg = news_setbubble(data['title'],data['description'],data['url'],data['image_url']) flex = SetMenuMessage_Object(msg) send_flex(reply_Token , flex , channel_access_token) else : message = '' ### message ที่เราจะส่งกลับไปให้ยูสเสอ text = [] user_data = None from dialogflow_uncle import detect_intent_texts project_id = os.getenv('DIALOGFLOW_PROJECT_ID') session_id = event.source.user_id ## get user id message = detect_intent_texts(project_id,session_id,Text_fromUser,'th') for i in message['fulfillment_messages']: ### เพิ่มจากในคลิบ txt = TextSendMessage(text=i)### เพิ่มจากในคลิบ text.append(txt)### เพิ่มจากในคลิบ ## adding imagemap message เรียนอะไร if message['action'] == 'register': from MessageTemplate.Imgmap import selectCourse imagemap = Base.get_or_new_from_json_dict(selectCourse(),ImagemapSendMessage) text.append(imagemap) ### prepare imagemap message to send #### เรียนที่ไหน elif message['action'] == 'selectcourses': from MessageTemplate.Imgmap import selectWhere imagemap = Base.get_or_new_from_json_dict(selectWhere(),ImagemapSendMessage) text.append(imagemap) ### prepare Imagemap message to send ### ให้ยูสเซอลงเวลา elif message['action'] == 'selecttype': from MessageTemplate.Imgmap import selectTime msg = Base.get_or_new_from_json_dict(selectTime(),TemplateSendMessage) text.append(msg) #### confirm message elif message['action'] == 'selectmonth': from MessageTemplate.Imgmap import confirmRegis msg = Base.get_or_new_from_json_dict(confirmRegis(),TemplateSendMessage) text.append(msg) ### sumarize message elif message['action'] == 'confirm': from google.protobuf.json_format import MessageToDict data = message['parameters'] data = MessageToDict(data) from MessageTemplate.Imgmap import GetStudentCard ####เนื้องจากการส่งข้อความแบบ flex2019 SDK python ยังไม่รองรับทำให้เราต้องกลับมาส่งแบบ manual msg = [] for i in text: Dict = i.as_json_dict() msg.append(Dict) msg.append(GetStudentCard(data['courses'])) flex = SetMenuMessage_Object(msg) send_flex(reply_Token,file_data = flex,bot_access_key = channel_access_token) #### กรณีเข้าโหมดถามตอบ elif message['action'] == 'q&a': line_bot_api.link_rich_menu_to_user(session_id,richmenu['menu2']) #### กรณีกำลังถามตอบ elif message['action'] == 'qa.qa-fallback': from MessageTemplate.Imgmap import AnwserMsg from Resource.wolf import search_wiki result = search_wiki(Text_fromUser) ### set msg + answer Flex_Ans = AnwserMsg(Text_fromUser,result) ### set quick reply qbtn = QuickReplyButton(image_url='https://cdn0.iconfinder.com/data/icons/online-education-butterscotch-vol-2/512/Questions_And_Answers-512.png' ,action=MessageAction('หยุดถาม','หยุดถาม')) q = QuickReply(items=[qbtn]) ### set text as json new_text1 = TextSendMessage(text='ลุงขอหาแปรป...').as_json_dict() new_text2 = TextSendMessage(text='อยากถามต่อไหม ถ้าไม่... กดปุ่ม หยุดถามได้เลยจร้า',quick_reply=q).as_json_dict() ### send msg with quick reply flex = SetMenuMessage_Object(Message_data=[new_text1,Flex_Ans,new_text2]) r = send_flex(Reply_token,file_data = flex,bot_access_key = channel_access_token) #### กรณีหยุดถาม elif message['action'] == 'qa.qa-custom': line_bot_api.link_rich_menu_to_user(session_id,richmenu['menu1']) text = TextSendMessage(text='แล้วกลับมาถามใหม่น่าาาา...') line_bot_api.reply_message(reply_Token,text) print(message['action']) line_bot_api.reply_message(reply_Token,text) return 'OK'