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_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 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): # 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 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)