def cmd_token(self, msg, params): from wallebot.app import rds user_id = msg['chat']['id'] user_name = msg['from']['username'] token = rds.hget(API_TOKEN_KEY, user_id) if token is None: # generate token while True: token = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16)) if not rds.hexists(API_TOKEN_REV_KEY, token): break # save the token rds.hset(API_TOKEN_KEY, user_id, token) rds.hset(API_TOKEN_REV_KEY, token, '%s:%s' % (user_id, user_name)) self.bot.sendMessage(chat_id=user_id, text='Your API token is "%s".' % token)
def message(self, msg): if msg['text'].startswith('/') or '#' in msg['text']: return chat_id = msg['chat']['id'] from wallebot.app import rds, cfg key = __REPEAT_KEY__ % chat_id last_msg = rds.hget(key, 'text') or '' last_msg = last_msg.decode('utf-8') last_users = rds.hget(key, 'users') or '' last_users = filter(None, last_users.split(',')) text = msg['text'] username = msg['from']['username'] if cfg.DEBUG: self.send_repeat(chat_id, text) elif last_msg == text and username not in last_users: last_users.append(username) log.info('Found repeat, add user %s, user=%s ' % (username, last_users)) if len(last_users) == THRESHOLD: # use # to ensure msg is sent only once self.send_repeat(chat_id, text) rds.hset(key, 'users', ','.join(last_users)) else: rds.hset(key, 'text', text.encode('utf-8')) rds.hset(key, 'users', username)
def cmd_chatid(self, msg, params): from wallebot.app import rds chat_id = msg['chat']['id'] rds.hset(API_CHATID_PENDING, chat_id, int(time.time()) + 30) self.bot.sendMessage(chat_id=chat_id, text='Send any message in 30s in a chat to get its chat_id.')