def hide_starting_button(token): data = to_json({ "setting_type": "call_to_actions", "thread_state": "new_thread", }) handle_thread_settings(token, data)
def typing_off(recipient, token): data = to_json({ "recipient": { "id": recipient.get_id() }, "sender_action": "typing_off" }) messagerequestpost(token, data)
def mark_seen(recipient, token): recipient_id = recipient.get_id() data = to_json({ "recipient": { "id": recipient_id }, "sender_action": "mark_seen" }) messagerequestpost(token, data)
def typing_on(recipient, token): recipient_id = recipient.get_id() print("Replying to {}".format(recipient_id)) data = to_json({ "recipient": { "id": recipient_id }, "sender_action": "typing_on" }) messagerequestpost(token, data)
def handle_location(token, user, location): user.setLocation(location) data = to_json({ "recipient": { "id": user.get_id() }, "message": { "text": "Updated Location. What is it you are looking for?", "quick_replies": qr.quick_replies_list_smaps } }) MessageHandler.messagerequestpost(token, data)
def ask_for_location(user, token): data = to_json({ "recipient": { "id": user.get_id() }, "message": { "text": "I'd love to help you look. Could you send me a location?", "quick_replies": [{ "content_type": "location" }] } }) MessageHandler.messagerequestpost(token, data)
def greetings(token): print("=============================") print("Handling Greetings") print("=============================") data = to_json({ 'setting_type': 'greeting', 'greeting': { 'text': "Hi friend. I am MemeBot", "locale": "default" } }) handle_thread_settings(token, data)
def handle_message_req(request, Users): payload = request.get_data() print(payload) for sender, message in messaging_events(payload): print("Incoming from %s: %s" % (sender, message)) user = next((u1 for u1 in Users if u1.get_id() == sender), None) if user is None: user = User(sender) Users.append(user) mark_seen(user, PAT) typing_on(user, PAT) if (isinstance(message, Location)): GoogleMapsHandler.handle_location(PAT, user, message) elif ("pic" in message.lower() or "send" in message.lower() or "get" in message.lower()): print("Initialized Reddit API Client") reddit = praw.Reddit(client_id=s.CLIENT_ID, client_secret=s.CLIENT_SECRET, password=s.PASSWORD, user_agent=s.USER_AGENT, username=s.USERNAME) send_message_reddit(PAT, user, message, reddit) elif ("look" in message.lower() and user.get_location() is not None): print("Initialized Google Maps API Client") google_places = GooglePlaces(s.GAPI) GoogleMapsHandler.handle_geosearch(PAT, user, message, google_places) elif ("look" in message.lower() and user.get_location() is None): GoogleMapsHandler.ask_for_location(user, PAT) else: print("Not Sure how to respond.") data = to_json({ "recipient": { "id": user.get_id() }, "message": { "attachment": { "type": "template", "payload": { "template_type": "button", "text": "Hi. Here are some of the things I can do for you!", "buttons": Buttons.button_list } } } }) messagerequestpost(PAT, data) return Users
def handle_geosearch(token, recipient, text, client, amttodisplay=4): search_words = " ".join([ tok for tok, pos in pos_tag(word_tokenize(text)) if pos.startswith('N') or pos.startswith('J') ]) query_result = client.nearby_search(location='{},{}'.format( recipient.get_location().get_longitude(), recipient.get_location().get_latitude()), keyword=search_words, rankby="distance") gmaps = googlemaps.Client(s.GAPI) distmat = gmaps.distance_matrix(origins=[ (recipient.get_location().get_longitude(), recipient.get_location().get_latitude()) ], destinations=[ (str(place.geo_location['lat']), str(place.geo_location['lng'])) for place in query_result.places ], mode="walking") timeanddur = distmat['rows'][0]['elements'] if len(query_result.places) > 0: elements = [] for indx, place in enumerate(query_result.places[:amttodisplay]): image_url = None timeaway, disaway = timeanddur[indx]['duration']['text'].decode( 'utf-8'), timeanddur[indx]['distance']['text'].decode('utf-8') if (len(place.photos) > 0): photo = place.photos[0] photo.get(maxheight=500, maxwidth=500) image_url = photo.url element = { "title": place.name.encode('utf-8'), "image_url": image_url, "subtitle": "{} by walking. {}.".format(timeaway, disaway), "buttons": [{ "title": "Open in Maps", "type": "web_url", "url": place.url, "messenger_extensions": 'true', "webview_height_ratio": "tall", }] } element = json.dumps(element) elements.append(element) data = to_json({ "recipient": { "id": recipient.get_id() }, "message": { "attachment": { "type": "template", "payload": { "template_type": "list", "top_element_style": "compact", "elements": elements } } } }) else: data = to_json({ "recipient": { "id": recipient.get_id() }, "message": { "text": "Couldn't find {}".format(search_words) } }) MessageHandler.messagerequestpost(token, data)