def json_del_friend(recipient_id): log("sending json_del_friend to {recipient}".format( recipient=recipient_id)) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "attachment": { "type": "template", "payload": { "template_type": "button", "text": u"請問您要刪除明信片好友嗎?", "buttons": [{ "type": "postback", "title": u"是", "payload": "del_yes" }, { "type": "postback", "title": u"否", "payload": "del_no" }] } } } }) sendtofb(data)
def json_mainbutton(recipient_id): log("sending mainbutton to {recipient}".format(recipient=recipient_id)) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "text": "你要選擇哪個呢?", "quick_replies": [{ "content_type": "text", "title": "寵物顧問", "payload": "main_button1" }, { "content_type": "text", "title": "交換明信片", "payload": "main_button2" }, { "content_type": "text", "title": "可愛寵物影片推播", "payload": "main_button3" }, { "content_type": "text", "title": "領養資訊搜尋", "payload": "main_button4" }, { "content_type": "text", "title": "民間送養資訊搜尋", "payload": "main_button5" }] } }) sendtofb(data)
def json_mainbutton(recipient_id): # construct mainbutton json log("sending mainbutton to {recipient}".format(recipient=recipient_id)) data = json.dumps( {"recipient": { "id": recipient_id }, "message": { "text": "請選擇您想要的服務:", "quick_replies": [ { "content_type": "text", "title": "景點搜尋", "payload": "main_button1" }, { "content_type": "text", "title": "旅伴明信片", "payload": "main_button2" } ] } } ) sendtofb(data)
def json_subscription(recipient_id): log("sending json_subscription to {recipient}".format( recipient=recipient_id)) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "attachment": { "type": "template", "payload": { "template_type": "button", "text": u"請問您是否要訂閱呢? 訂閱可以在每天的固定時間接收新的可愛寵物影片呦", "buttons": [{ "type": "postback", "title": u"是", "payload": "subscription_yes" }, { "type": "postback", "title": u"否", "payload": "subscription_no" }] } } } }) sendtofb(data)
def typingon_json(recipient_id): # construct typing on json log("sending typingon to {recipient}".format(recipient=recipient_id)) data = json.dumps({ "recipient": { "id": recipient_id }, "sender_action": "typing_on" }) sendtofb(data)
def json_message(recipient_id, message_text): # construct message json log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text)) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "text": message_text } }) sendtofb(data)
def json_photo(recipient_id, url): log("sending photo to {recipient}".format(recipient=recipient_id)) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "attachment": { "type": "image", "payload": { "url": url } } } }) sendtofb(data)
def json_location(recipient_id): log("sending location to {recipient}".format(recipient=recipient_id)) data = json.dumps( { "recipient": { "id": recipient_id }, "message": { "text": "Please share your location:", "quick_replies": [ { "content_type": "location", } ] } } ) sendtofb(data)
def json_chat_quickreply(recipient_id): log("sending chat_quickreply to {recipient}".format( recipient=recipient_id)) titles = [ '領養寵物的條件?', '貓可以喝牛奶嗎?', '貓咪怎麼洗澡?', '狗狗怎麼洗澡', '貓咪吐了怎麼辦?', '要如何照顧剛出生的貓?', '貓咪半夜好吵怎麼辦?', '貓咪抓傢具怎麼辦', '貓咪要怎麼訓練', '貓咪拉肚子怎麼辦?', '貓咪一直咬我', '貓咪為什麼會翻肚子?', '貓咪為什麼會抖屁屁?', '貓咪屁股對著我代表什麼?', '貓咪斜眼看人代表什麼?', '狗狗要怎麼訓練', '我想知道貓咪喝水的知識', '我想了解貓砂盆的知識', '我想了解貓砂的知識' ] random.shuffle(titles) data = json.dumps({ "recipient": { "id": recipient_id }, "message": { "text": "不過我懂其他有關的寵物知識,例如", "quick_replies": [{ "content_type": "text", "title": titles[0], "payload": "2" }, { "content_type": "text", "title": titles[1], "payload": "2" }, { "content_type": "text", "title": titles[2], "payload": "2" }, { "content_type": "text", "title": titles[3], "payload": "2" }, { "content_type": "text", "title": titles[4], "payload": "2" }] } }) sendtofb(data)
def search_scene(sender_id, px, py, count2, mode, tag): log("sending search_scene") client = pymongo.MongoClient(uri) db = client.get_default_database() # First we'll add a few songs. Nothing is required to create the songs # collection; it is created automatically when we insert. scenes = db['travel'] template = new_template(sender_id) count = 0 if mode == 0: for doc in scenes.find(): if (pow(float(doc['Px']) - px, 2) + pow(float(doc['Py']) - py, 2) < 0.035): count = count + 1 if (count >= count2): name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) else: if tag == 0: # tainan for doc in scenes.find(): if u'tainan' in doc['Add'] or u'台南' in doc[ 'Add'] or u'Tainan' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 1: for doc in scenes.find(): if u'台北' in doc['Add'] or u'臺北' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 2: for doc in scenes.find(): if u'台中' in doc['Add'] or u'臺中' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 3: for doc in scenes.find(): if u'台東' in doc['Add'] or u'臺東' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 4: for doc in scenes.find(): if u'桃園' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 5: for doc in scenes.find(): if u'新竹' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 6: for doc in scenes.find(): if u'苗栗' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 7: for doc in scenes.find(): if u'彰化' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 8: for doc in scenes.find(): if u'南投' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 9: for doc in scenes.find(): if u'雲林' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 10: for doc in scenes.find(): if u'嘉義' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 11: for doc in scenes.find(): if u'高雄' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 12: for doc in scenes.find(): if u'屏東' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 13: for doc in scenes.find(): if u'宜蘭' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 elif tag == 14: for doc in scenes.find(): if u'花蓮' in doc['Add']: print "in find" if count >= count2: name = doc['Name'].encode('utf-8') discription = doc['Toldescribe'].encode('utf-8') image_url = doc['Picture1'] place_url = doc['place_url'] if (count >= count2 + 8): template = add_template(template, u"想看更多?", u"看更多", image_url, px, py, count, place_url, mode, tag) break else: template = add_template(template, name, discription, image_url, px, py, count, place_url, mode, tag) count = count + 1 if count == 0 or (count < count2): json_message(sender_id, "嗚嗚嗚不好意思,找不到相對應的結果") else: data = json.dumps(template) sendtofb(data)
def json_template(template, recipient_id): # construct template json log("sending template to {recipient}".format(recipient=recipient_id)) data = json.dumps(template) sendtofb(data)
def webhook(): # endpoint for processing incoming messaging events data = request.get_json() log( data ) # you may not want to log every incoming message in production, but it's good for testing if data["object"] == "page": for entry in data["entry"]: for messaging_event in entry["messaging"]: sender_id = messaging_event["sender"]["id"] if messaging_event.get("message"): # someone sent us a message if ("quick_reply" in messaging_event["message"]): payload = messaging_event["message"]["quick_reply"][ "payload"] if (payload == "mainbutton_1"): upload_flag(1, sender_id) json_message(sender_id, '你可以直接輸入地點詢問,例如') json_message(sender_id, '我想找台南的景點') json_message(sender_id, '或是分享你的地點,來獲得離你最近的景點資訊') json_location(sender_id) elif payload == "mainbutton_4": upload_flag(2, sender_id) json_message( sender_id, '旅行明信片是一個特別的社交方式,藉由互送明信片,分享旅途的美麗風景,認識其他熱愛旅行的旅人!' ) json_message(sender_id, "請先傳送一張旅途的美麗風景,作為明信片的封面吧~") elif ("text" in messaging_event["message"]): message_text = messaging_event["message"][ "text"] # the message's text print(sender_id) # test if (u'台南' in message_text or u'臺南' in message_text) and get_flag(sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 0) elif (u'台北' in message_text or u'臺北' in message_text) and get_flag(sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 1) elif (u'台中' in message_text or u'臺中' in message_text) and get_flag(sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 2) elif (u'台東' in message_text or u'臺東' in message_text) and get_flag(sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 3) elif u'桃園' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 4) elif u'新竹' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 5) elif u'苗栗' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 6) elif u'彰化' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 7) elif u'南投' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 8) elif u'雲林' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 9) elif u'嘉義' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 10) elif u'高雄' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 11) elif u'屏東' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 12) elif u'宜蘭' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 13) elif u'花蓮' in message_text and get_flag( sender_id) == 1: search_scene(sender_id, 0, 0, 0, 1, 14) elif get_flag(sender_id) == 2: upload_db_intro(message_text, sender_id) json_message(sender_id, "已完成,請耐心等待神秘的明信片") elif ("attachments" in messaging_event["message"]): attachment = messaging_event["message"]["attachments"] if (attachment[0]["type"] == u"location"): px = float(attachment[0]["payload"]["coordinates"] ["long"]) py = float( attachment[0]["payload"]["coordinates"]["lat"]) search_scene(sender_id, px, py, 0, 0, 0) elif (attachment[0]["type"] == u"image"): for attachment in messaging_event["message"][ "attachments"]: url = attachment["payload"]["url"] url = upload_photo(url) upload_db_photo_url(url, sender_id) json_message(sender_id, "已收到圖片") json_message(sender_id, "請輸入簡單的明信片內容,給未知的旅行愛好者吧\n 例如") json_message(sender_id, "嗨~這是美麗的西子灣秘境,分享給妳~") if messaging_event.get("delivery"): # delivery confirmation pass if messaging_event.get("optin"): # optin confirmation pass # user clicked/tapped "postback" button in earlier message if messaging_event.get("postback"): if messaging_event["postback"][ "payload"] == 'GET_STARTED_PAYLOAD': json_message(sender_id, "你好,我是旅行助理。專為愛旅行的你所打造!") json_mainbutton(sender_id) elif messaging_event["postback"][ "payload"] == 'main_button1': upload_flag(1, sender_id) json_message(sender_id, '你可以直接輸入地點詢問,例如') json_message(sender_id, '我想找台南的景點') json_message(sender_id, '或是分享你的地點,來獲得離你最近的景點資訊') json_location(sender_id) elif messaging_event["postback"][ "payload"] == 'main_button2': upload_flag(2, sender_id) json_message( sender_id, '旅行明信片是一個特別的社交方式,藉由互送明信片,分享旅途的美麗風景,認識其他熱愛旅行的旅人!') json_message(sender_id, "請先傳送一張旅途的美麗風景,作為明信片的封面吧~") elif 'http' in messaging_event["postback"]["payload"]: place_url = messaging_event["postback"]["payload"] json_message(sender_id, place_url) elif messaging_event['postback'][ 'payload'] == 'get_match_mail': mail = get_mail(sender_id) intro = mail['intro'] img_url = mail['url'] json_photo(sender_id, img_url) json_message(sender_id, intro.encode('utf-8')) else: wantwatch = messaging_event["postback"][ "payload"].split() if len(wantwatch) == 3: px = float(wantwatch[0]) py = float(wantwatch[1]) count = int(wantwatch[2]) search_scene(sender_id, px, py, count, 0, 0) else: tag = int(wantwatch[0]) count = int(wantwatch[1]) search_scene(sender_id, 0, 0, count, 1, tag) return "ok", 200