def decision_time_no(bot, cuser, other_user): cuser.on_edge = False cuser.in_chat_with = "" utils.set_redis(cuser.id, cuser) other_user.on_edge = False other_user.in_chat_with = "" utils.set_redis(other_user.id, other_user) message_text = "Both random parties did not agree to share.\nConnection Disconnected" bot.send_text_message(cuser.id, message_text) bot.send_text_message(other_user.id, message_text) send_starting_gate(bot, cuser) send_starting_gate(bot, other_user)
def handle_postback(raw_event): bot = Bot(utils.config["page_access_token"]) cuser = messages.user(bot.get_user_info(raw_event["sender"]["id"]), raw_event["sender"]["id"]) payload = raw_event["postback"]["payload"] red_user = utils.get_redis(cuser.id) # welcome message, user has not signed up yet if red_user == None: messages.intro_message(bot, cuser) return # if something is different between the user and the database user if red_user.is_different(cuser): utils.set_redis(cuser.id, red_user) if payload == "starting_gate": # and not red_user.looking_for_chat: if not red_user.in_chat and not red_user.looking_for_chat: red_user.set_looking_for_chat() found_chat = red_user.search_for_chat() bot.send_text_message(red_user.id, "Searching...") if found_chat: messages.found_chat_reply(bot, red_user, found_chat) elif red_user.looking_for_chat: messages.send_in_limbo(bot, red_user) elif payload == "decision_time_yes" or payload == "decision_time_no": if not red_user.on_edge: return other_user = utils.get_redis(red_user.in_chat_with) if payload == "decision_time_no": messages.decision_time_no(bot, red_user, other_user) else: if red_user.showed_id: messages.waiting_for_decision(bot, red_user) return messages.decision_time_yes(bot, red_user, other_user) elif payload == "start_message": handle_message(raw_event)
def set_api_key(self, code): raw_data = requests.get( "https://graph.facebook.com/v2.8/oauth/access_token?client_id=" + utils.config["fb_client_id"] + "&redirect_uri=" + utils.config["website"] + "/fb_callback?id=" + self.id + "&client_secret=" + utils.config["fb_client_secret"] + "&code=" + code).json() access_token = raw_data["access_token"] self.api_key = access_token req_string = ("https://graph.facebook.com/v2.8/me?" + "fields=id,name&access_token=" + self.api_key) self.profile_id = requests.get(req_string).json()["id"] utils.set_redis("-" + str(self.profile_id), self.id)
def handle_auth_message(user_id, code): # get the info used for all messages bot = Bot(utils.config["page_access_token"]) cuser = utils.get_redis(user_id) new_user = False if cuser == None: cuser = messages.user(bot.get_user_info(user_id), user_id) new_user = True needs_update = cuser.needs_api_update cuser.needs_api_update = False cuser.set_api_key(code) utils.set_redis(user_id, cuser) if new_user: messages.after_registering(cuser) elif needs_update: messages.refresh_api_key()
def search_for_chat(self): friendlist = self.get_friendlist() for raw_friend in friendlist: cuser = get_user_from_profile_id(raw_friend["id"]) if cuser != None and cuser.looking_for_chat: cuser.looking_for_chat = False cuser.in_chat = True cuser.in_chat_with = self.id utils.set_redis(cuser.id, cuser) self.looking_for_chat = False self.in_chat = True self.in_chat_with = cuser.id utils.set_redis(self.id, self) return cuser.id return False
def decision_time_yes(bot, cuser, other_user): cuser.showed_id = True # if the other user has not decided yet if not other_user.showed_id: utils.set_redis(cuser.id, cuser) return cuser.showed_id = False other_user.showed_id = False cuser.on_edge = False other_user.on_edge = False cuser.in_chat_with = "" other_user.in_chat_with = "" utils.set_redis(cuser.id, cuser) utils.set_redis(other_user.id, other_user) bot.send_image_url(cuser.id, other_user.profile_pic) bot.send_image_url(other_user.id, cuser.profile_pic) output = "You were chatting with {0} {1}!" bot.send_text_message( cuser.id, output.format(other_user.first_name, other_user.last_name)) bot.send_text_message(other_user.id, output.format(cuser.first_name, cuser.last_name)) second_out = "Your conversation on 20messages with them is over\n" bot.send_text_message(cuser.id, second_out) bot.send_text_message(other_user.id, second_out) send_starting_gate(bot, cuser) send_starting_gate(bot, other_user)
def handle_message(raw_event): # use pymessenger to make an API call to FB to get the # info about the user who send the message bot = Bot(utils.config["page_access_token"]) user_id = raw_event["sender"]["id"] user_info = bot.get_user_info(user_id) cuser = messages.user(user_info, user_id) red_user = utils.get_redis(cuser.id) # welcome message, user has not signed up yet if red_user == None: messages.intro_message(bot, cuser) return # if something is different between the user and the database user if red_user.is_different(cuser): utils.set_redis(cuser.id, red_user) elif red_user.looking_for_chat: messages.send_in_limbo(bot, red_user) elif red_user.in_chat: if "message" in raw_event.keys(): raw_message = raw_event["message"] messages.handle_chat(bot, red_user, raw_message) else: bot.send_text_message(user_id, "You are in a chat currently") elif red_user.on_edge and not red_user.showed_id: messages.send_decision_message(bot, red_user) elif red_user.on_edge and red_user.showed_id: messages.waiting_for_decision(bot, red_user) elif not red_user.in_chat and not red_user.looking_for_chat and not red_user.on_edge: messages.send_starting_gate(bot, red_user)
def handle_chat(bot, cuser, raw_message): other_user = utils.get_redis(cuser.in_chat_with) messages_display = cuser.messages_left cuser.messages_left -= 1 other_user.messages_left -= 1 messages_left = cuser.messages_left if messages_left == 0: cuser.in_chat = False cuser.on_edge = True other_user.in_chat = False other_user.on_edge = True cuser.messages_left = 20 other_user.messages_left = 20 utils.set_redis(cuser.id, cuser) utils.set_redis(other_user.id, other_user) if "text" in raw_message.keys(): text_message = raw_message["text"] text = "{0}: '{1}'".format(messages_display, text_message) bot.send_text_message(other_user.id, text) elif "url" in raw_message["attachments"][0]["payload"].keys(): img_url = raw_message["attachments"][0]["payload"]["url"] text = "{0}:".format(messages_display) bot.send_text_message(other_user.id, text) bot.send_image_url(other_user.id, img_url) if messages_left == 0: send_decision_message(bot, cuser) send_decision_message(bot, other_user) elif messages_left == 1: bot.send_text_message(cuser.id, "You have one message left!") bot.send_text_message(other_user.id, "You have one message left!")
def set_looking_for_chat(self): self.looking_for_chat = True utils.set_redis(self.id, self)