def change_goal(user, goal, text, amt): goalobj = check_goal(goal, user) text = TextBlob(text) text = text.words if amt: goalobj.target = amt return get_response('change_goal_target', amt=amt, goal=goal) goalobj.name = text[-1] return get_response('change_goal_name', goal=goalobj.name)
def handle_message(update, context): text = str(update.message.text).lower() logging.info(f'User ({update.message.chat.id}) says: {text}') # Bot response response = responses.get_response(text) update.message.reply_text(response)
def change_money(user, goal, amt): goalobj = check_goal(goal, user) if not goalobj: return "{} isn't one of your goals".format(goal) goalobj.balance += amt if goalobj.balance >= goalobj.target: return get_response('congrats', amt=goalobj.target, goal=goalobj.name) + '\n' + get_twitter_link(goal) elif amt <= 0: return get_response('balancedown', amt=goalobj.balance, target=goalobj.target, goal=goalobj.name) else: return get_response('balanceup', amt=goalobj.balance, target=goalobj.target, goal=goalobj.name)
def generate_response(action, amt, goal, text, user): if action in ["save", "spend"]: if action == "spend": amt *= -1 return change_money(user, goal, amt) elif action == "new_goal": return set_goal(user, goal, amt) elif action == "del_goal": return unset_goal(user, goal) elif action == "change": return change_goal(user, goal, text, amt) elif action == "list" or goal == "goals": return get_response('list_goal', goal=', '.join(g.name for g in user.goals)) elif action == "greet": return get_response('hello', user=user.name) elif goal == None: return "I didn't understand your goal. Please send the message again, with a goal at the end." elif amt == None: return "How much are we talking here? Please send the message again, with a monetary amount" else: return "I didn't understand your message :("
def webhook(): # 'message' is an object that represents a single GroupMe message. message = request.get_json() translator = str.maketrans('', '', string.punctuation) text = message['text'] textNoPunc = text.translate(translator) words = textNoPunc.split() if sender_is_bot(message): return "ok", 200 else: words = get_response(text) for t in words: reply(t) return "ok", 200
def unset_goal(user, goal): goalobj = check_goal(goal, user) user.remove_goal(goalobj) print(user.goals) return get_response('del_goal', goal=goal)
def set_goal(user, goal, amt): user.add_goal(goal, amt) print(user.goals) return get_response('new_goal', goal=goal, amt=amt)