def add_new_user(message): new_user = message.new_chat_participant balance = model.Balance( group=message.chat.id, user=new_user.username, amount=0) session.add(balance) cherrypy.engine.publish( 'add-payments', message.chat.id, {new_user.username: 0}) bot.send_message(message.chat.id, ('Welcome, %s! Sharing is caring.' % new_user.first_name))
def add_new_user(message): new_user = message.new_chat_participant balance = model.Balance(group=message.chat.id, user=new_user.username, amount=0) session.add(balance) cherrypy.engine.publish('add-payments', message.chat.id, {new_user.username: 0}) bot.send_message( message.chat.id, ('Welcome, %s! Sharing is caring.' % new_user.first_name))
def add_payments(message): usage_message = ( 'Please specify the payment amount after /pay, e.g. /pay 5.00') amount = None words = message.text.split() if not (len(words) == 2): bot.send_message(message.chat.id, usage_message) return try: amount = float(words[1]) except ValueError: bot.send_message(message.chat.id, usage_message) return session_key = _GetSessionKey(message) session_is_open = cherrypy.engine.publish( 'session-is-open', session_key)[0] if (session_is_open): open_session_message = ( 'Looks like you forgot to finish your previous session!') # TODO: Print out session info. bot.send_message(message.chat.id, open_session_message) return cherrypy.engine.publish('add-users-to-session', session_key, [message.from_user.username]) payments = {message.from_user.username: amount} balance = session.query(model.Balance).filter_by( group=message.chat.id, user=message.from_user.username).one_or_none() if balance is None: balance = model.Balance( group=message.chat.id, user=message.from_user.username, amount=amount) session.add(balance) else: balance.amount += amount cherrypy.engine.publish( 'add-payments', message.chat.id, payments) old_balance = cherrypy.engine.publish( 'get-user', message.chat.id, message.from_user.username)[0] bot.send_message(message.chat.id, ('Thank you for your payment of %.2f, generous friend! ' 'Your total balance is %.2f now (%.2f in DB).' % (amount, old_balance, balance.amount)))
def add_payments(message): usage_message = ( 'Please specify the payment amount after /pay, e.g. /pay 5.00') amount = None words = message.text.split() if not (len(words) == 2): bot.send_message(message.chat.id, usage_message) return try: amount = float(words[1]) except ValueError: bot.send_message(message.chat.id, usage_message) return session_key = _GetSessionKey(message) session_is_open = cherrypy.engine.publish('session-is-open', session_key)[0] if (session_is_open): open_session_message = ( 'Looks like you forgot to finish your previous session!') # TODO: Print out session info. bot.send_message(message.chat.id, open_session_message) return cherrypy.engine.publish('add-users-to-session', session_key, [message.from_user.username]) payments = {message.from_user.username: amount} balance = session.query(model.Balance).filter_by( group=message.chat.id, user=message.from_user.username).one_or_none() if balance is None: balance = model.Balance(group=message.chat.id, user=message.from_user.username, amount=amount) session.add(balance) else: balance.amount += amount cherrypy.engine.publish('add-payments', message.chat.id, payments) old_balance = cherrypy.engine.publish('get-user', message.chat.id, message.from_user.username)[0] bot.send_message( message.chat.id, ('Thank you for your payment of %.2f, generous friend! ' 'Your total balance is %.2f now (%.2f in DB).' % (amount, old_balance, balance.amount)))
def debug(self): group = session.query(model.Group).get('123') if group is None: group = model.Group(id='123') session.add(group) return group.id