def handle_image(sender, url): try: partner = activechatsdb.get_partner(sender) alias = activechatsdb.get_alias(sender) message = TextTemplate(text=alias + " has sent you an image.") send_message(message.get_message(), id=partner) message = AttachmentTemplate(url=url, type='image') send_message(message.get_message(), id=partner) #dummy1 except Exception as e: print("IMAGE ERROR", str(e))
def upgrade_level(self, id): print("game upgrade") user = usersdb.get(id) if user.level == None: user.level = 0 user.level = user.level + 1 self.db.session.commit() message = TextTemplate(text="Congrats! You have guessed the correct word. You are now at " + \ "level "+str(user.level)+".") send_message(message.get_message(), id) if user.level != 5: message = TextTemplate( text="Here is your hint for the next level:\n\n" + self.hints[user.level]) send_message(message.get_message(), id)
def send_help(sender): helptext = "BlindChat allows you to chat with people without revealing your identity. "+\ "The bot will match you with strangers all over the world. You can choose to share your profile with the other person after ending the chat.\n"+\ "\nAvailable commands:\n1. quit/exit: quits from the active chat or from the waitlist"+\ "\n2. help: view the help menu\n4. start: starts to look for a new chat\n5. restart: restart the bot"+\ "\n5. profile: modify your chat profile" message = TextTemplate(text=helptext) send_message(message.get_message(), sender)
def execute_exit(id): show_typing(id=id, duration=1) print("EXECUTING EXIT") if activechatsdb.isActive(id): endChat(id) elif waitlistdb.isWaiting(id): waitlistdb.delist(id) message = TextTemplate(text="You have been removed from the waitlist") send_message(message=message.get_message(), id=id) show_typing(id=id, duration=1) send_newchat_prompt(id=id) else: message = TextTemplate( text= "You are not in an active chat or in the waitlist. Type \"start\" to start a new chat" ) send_message(message=message.get_message(), id=id)
def restart_bot(id): show_typing(id=id, duration=1) print("here") if activechatsdb.isActive(id): print("ACTIVE", id) partner = activechatsdb.get_partner(id) activechatsdb.delete_chat_entries(id) message = TextTemplate(text="Your active chat has been ended.") send_message(message=message.get_message(), id=id) message = TextTemplate( text="Your active chat has been ended from the other side.") send_message(message=message.get_message(), id=partner) show_typing(id=partner, duration=1) send_newchat_prompt(id=partner) if waitlistdb.isWaiting(id): waitlistdb.delist(id) message = TextTemplate(text="You have been removed from the waitlist") send_message(message=message.get_message(), id=id) show_typing(id=id, duration=1) send_newchat_prompt(id=id)
def send_help(sender): helptext = """BlindChat allows you to chat with people without revealing your identity. The bot will match you with strangers all over the world. You can choose to share your profile with the other person after ending the chat. Available commands: quit - quits from the active chat or from the waitlist help - view the help menu start - starts to look for a new chatrestart - restart the bot profile - modify your chat profile """ message = TextTemplate(text=helptext) send_message(message.get_message(), sender)
def handle_postback(payload, sender): if payload not in valid_payloads and json.loads( payload)["keyword"] not in valid_payloads: message = TextTemplate(text="Not sure if this is a valid command") send_message(message.get_message(), id=sender) elif payload == "restart": interrupts.handleCommand(command="restart", sender=sender) elif payload == "help": send_help(sender=sender) elif payload == "quit": interrupts.handleCommand(command="quit", sender=sender) elif payload == "getstarted": print("GET STARTED DETECTED") message = TextTemplate( "Hello there, a big welcome to BlindChat. Chat with people all over the " + "world anonymously. Share your profile only when you want to.\n\n" + "We are adding cool new features every single day, so keep " + "on exploring. Cheers!\n\nAnd by the way, when in need, type" + " \"help\" to see the list of available commands") send_message(message.get_message(), id=sender) send_newchat_prompt(id=sender)
def webhook(): if request.method == 'POST': data = request.get_json(force=True) # analytics api post metrics.record(entry=data["entry"]) messaging_events = data['entry'][0]['messaging'] for event in messaging_events: sender = event['sender']['id'] print("EVENT", event) try: if sender != PAGE_ID and usersdb.hasDataOf(sender) is False: usersdb.add(sender) except Exception, e: print("ERROR", str(e)) try: if 'postback' in event and 'payload' in event['postback']: postback_payload = event['postback']['payload'] print("postback payload", postback_payload) handle_postback(payload=postback_payload, sender=sender) print("postback handled") continue elif 'message' in event and 'text' in event['message']: if Int.isValidCommand(event['message']['text']): print("interrupt detected", event['message']['text']) Int.handleCommand(command=event['message']['text'], sender=sender) print("interrupt handled") continue else: print("NOT POSTBACK OR INTERRUPT") except Exception, e: print("POSTBACK/INTERRUPT ERROR", str(e)) db.session.rollback() return '' if game.isGame(event) == True: x = game.gamify(event['message']['text'], id=sender) if x == True: continue if activechatsdb.isActive(sender): alias = activechatsdb.get_alias(sender) if 'message' in event and 'text' in event['message']: text = event['message']['text'] if 'quick_reply' in event['message'] and 'payload' in event[ 'message']['quick_reply']: quick_reply_payload = event['message']['quick_reply'][ 'payload'] handle_quick_reply(sender=sender, payload=quick_reply_payload) else: message = TextTemplate(text=alias + ": " + text) recipient = activechatsdb.get_partner(sender) send_message(message=message.get_message(), id=recipient) elif 'message' in event and 'attachments' in event[ 'message'] and 'type' in event['message'][ 'attachments'][0]: if event['message']['attachments'][0]['type'] == "image": handle_image(sender=sender, url=event['message']['attachments'][0] ['payload']['url']) else: recipient = sender if 'message' in event and 'text' in event['message']: text = event['message']['text'] try: if text[:3] == ":::": handle_debug(text, id=sender) message = TextTemplate( text="Debug command executed") send_message(message.get_message(), id=recipient) continue except Exception, e: print("DEBUG ERROR", str(e)) if 'quick_reply' in event['message'] and 'payload' in event[ 'message']['quick_reply']: quick_reply_payload = event['message']['quick_reply'][ 'payload'] handle_quick_reply(sender=sender, payload=quick_reply_payload) else: if (isGreeting(text)): handle_greetings(text, sender, usersdb.get(sender).first_name) continue message = TextTemplate( text= "I didn't understand what you intended. Type \"help\" to" + " get the set of available commands. Use those commands or" + " the menu options to interact with the bot") send_message(message.get_message(), id=recipient)
def send_emoticon(id): happy = u'\u2B50' print("EMOTICON") message = TextTemplate(text="Hi " + happy) send_message(message.get_message(), id=id) print("EMOTICON 1")
def handle_greetings(text, id, name): message = TextTemplate( text="Hi " + name + ". Nice to meet you. To get a list of available commands, type \"help\"" ) send_message(message.get_message(), id)
match = waitlistdb.get_match(gender, interest) print("START2", match) except Exception, e: print("ERROR #0002", str(e)) if match is None: try: # delist because there's no guarantee that it already isn't there waitlistdb.delist(id=sender) waitlistdb.enlist(id=sender, gender=gender, interest=interest) except Exception, e: print("ERROR #0003", str(e)) message = TextTemplate(text="No match found right now. You are in" + " the wait list. We will match you as" + " soon as someone becomes available") send_message(message.get_message(), id=sender) else: match_gender = usersdb.get(match).gender alias1 = generate_alias(gender=gender) alias2 = generate_alias(gender=match_gender) try: activechatsdb.clear_data(user=sender) activechatsdb.clear_data(user=match) activechatsdb.create_new_chat(user1=sender, user2=match) activechatsdb.set_alias(user=sender, alias=alias1) activechatsdb.set_alias(user=match, alias=alias2) except Exception, e: print("ERROR #0004", str(e)) imurl = APP_URL + "static/startchat.jpg/"
def send_hint(self, level, id): print("game 1.5", level) message = TextTemplate(text=self.hints[level]) send_message(message=message.get_message(), id=id)
def startChat(sender, interest): # handles the initiation of a new chat after the user selects the interest print("START1", log_waitlisted_users()) try: gender = usersdb.get(sender).gender # gets the gender from the except Exception as e: gender = "male" print("ERROR #0001", str(e)) try: # returns the PSID of the match match = waitlistdb.get_match(gender, interest) print("START2", match) except Exception as e: print("ERROR #0002", str(e)) if match == None: try: waitlistdb.delist(id=sender) # delist because there's no guarantee that it already isn't there waitlistdb.enlist(id=sender, gender=gender, interest=interest) except Exception as e: print("ERROR #0003", str(e)) message = TextTemplate(text="No match found right now. You are in the wait list. We will match you as soon"+\ " as someone becomes available") send_message(message.get_message(), id=sender) else: match_gender = usersdb.get(match).gender alias1 = generate_alias(gender=gender) alias2 = generate_alias(gender=match_gender) try: activechatsdb.clear_data(user=sender) activechatsdb.clear_data(user=match) activechatsdb.create_new_chat(user1=sender, user2=match) activechatsdb.set_alias(user=sender, alias=alias1) activechatsdb.set_alias(user=match, alias=alias2) except Exception as e: print("ERROR #0004", str(e)) imurl = APP_URL+"static/startchat.jpg/" # ------------------------------------ MATCH ---------------------------------------- # #message = AttachmentTemplate(url=get_start_hi(gender=gender),type="image") #send_message(message.get_message(), id=match) sender_bio = usersdb.get(sender).bio if sender_bio is None: bio = "No bio" else: bio = "Bio: " + sender_bio sender_interests = usersdb.get(sender).interests if sender_interests is None: intr = "No interests." else: intr = "Interests: " + sender_interests sender_level = usersdb.get(sender).level if sender_level == None: usersdb.setLevel(sender, 0) sender_level = usersdb.get(sender).level level_str = u'\u2B50' for i in range(sender_level): level_str = level_str + u'\u2B50' message = GenericTemplate() message.add_element(title="You are matched with "+alias1, subtitle=level_str, image_url=imurl) send_message(message=message.get_message(), id=match) message = TextTemplate(text=bio + " | "+ intr) send_message(message.get_message(), id=match) # ------------------------------------- SENDER -------------------------------------------- # #message = AttachmentTemplate(url=get_start_hi(gender=match_gender), type="image") #send_message(message.get_message(), id=sender) match_bio = usersdb.get(match).bio if match_bio is None: bio = "No bio" else: bio = "Bio: " + match_bio match_interests = usersdb.get(match).interests if match_interests is None: intr = "No interests." else: intr = "Interests: " + match_interests match_level = usersdb.get(match).level if match_level == None: usersdb.setLevel(match, 0) match_level = usersdb.get(match).level level_str = u'\u2B50' for i in range(match_level): level_str = level_str + u'\u2B50' message = GenericTemplate() message.add_element(title="You are matched with " + alias2, subtitle=level_str, image_url=imurl) send_message(message=message.get_message(), id=sender) message = TextTemplate(text=bio + " | " + intr) send_message(message.get_message(), id=sender)