def on_request(self, data: MessagingEntry): data.sender = User(self, data.sender) print("Message: {}".format(data.message)) if self.always_typing_on: self.typing_on(data.sender) if self.always_mark_seen: self.mark_seen(data.sender) self.process_payloads(data) # Handlers may change user's state and want to immediately call next state's handler # That's why we make continue_processing False every time we call a handler, and if the next handler has to be # called, the called handler will have made continue_processing True. while data.continue_processing: state = data.sender.state if state in self.handlers: for f in self.handlers[state]: data.continue_processing = False f(self, data) else: print("Unregistered state: {}".format(state)) else: print("State handlers not processed (message already processed).")
def process_quick_reply(self, entry: MessagingEntry): p = entry.quick_reply_payload if not p: return if p.startswith("SetState:"): state = p.split(":")[1] print("Setting state to {}".format(state)) entry.sender.set_state(state) elif p.startswith("Execute:"): f_ident = p.split(":")[1] to_call = FunctionRegistry.get(f_ident) if to_call: print("Executing function {} ({})".format( to_call.__name__, f_ident)) # Set continue_processing before calling the function so the # user can change this behaviour entry.continue_processing = False to_call(entry) else: print( "Couldn't find function to execute in FunctionRegistry ({})." .format(f_ident)) else: print("Unsupported payload: {}".format(p))
def handler_custom_email(self, req: MessagingEntry): req.sender.send( Message(content="Thanks for signing up! You just got $250!")) self.balances[req.sender.id] = 250.0 self.emails[req.sender.id] = req.message req.sender.set_state("Home") req.continue_processing = True
def parse_raw_data(data): chunks = list() for entry in data["entry"]: if "messaging" in entry: for messaging_entry in entry.get("messaging"): chunks.append(MessagingEntry.from_dict(messaging_entry)) continue print(entry) return chunks
def handler_pay(self, req: MessagingEntry): user = req.sender message = req.message try: amount = float(message) except ValueError: user.send("Sorry, but this amount is invalid. Please try again.") return if amount > self.balances[user.id]: user.send("Your balance is insufficient.") return receiver_id = self.receivers[user.id] print("Sending {} from {} to {}".format(amount, self.emails[user.id], self.emails[receiver_id])) self.balances[receiver_id] += amount self.balances[user.id] -= amount user.send("Sending ${}".format(amount)) user.set_state("Home") req.continue_processing = True
def save_email(self, req: MessagingEntry): email = req.quick_reply_payload req.message = email self.handler_custom_email(req)
def delete_account(self, req: MessagingEntry): del self.balances[req.sender.id] req.sender.send("Okay, we deleted it.") req.sender.set_state("Home") req.continue_processing = True
def send_balance(self, req: MessagingEntry): user = req.sender user.send("Your balance is ${}".format(self.balances[user.id])) user.set_state("Home") req.continue_processing = True