def process_vote_response(bot: telegram.Bot, update: telegram.Update):
# Message text comes from a reply keyboard.
# The keyboard options look like: "abcdefgh: Title of Poll",
# where "abcdefgh" is the poll tag.
t = update.message.text
uid = update.message.from_user.id
# Check if user has typed something into the text reply, instead of
# clicking a reply keyboard button like they should have.
if not re.match("^[a-z]{8}:", t):
bot.sendMessage(update.message.chat_id, text_vote_not_understood,
reply_markup=ReplyKeyboardHide())
return ConversationHandler.END
# Check if the given poll tag 1) exists, 2) is active,
# and 3) if the user is allowed to vote in that poll.
poll_tag = t[0:8]
q = Query()
poll = polls_db.get(q.tag == poll_tag)
if (poll is None) or (poll['active'] != True) or (
not user_is_in_chat(uid, poll['target_chat'], bot)):
bot.sendMessage(update.message.chat_id, text_no_such_vote,
reply_markup=ReplyKeyboardHide())
return ConversationHandler.END
# Check if user was already given a token for this poll.
q2 = Query()
existing_token = tokens_db.get(
q2.user_id == uid and q2.poll_tag == poll_tag)
if existing_token is None:
# Give new token
token = make_token(uid, poll_tag)
msg = text_new_token.format(title=poll['title'], url=poll['url'],
token=token)
bot.sendMessage(update.message.chat_id, msg,
reply_markup=ReplyKeyboardHide())
return ConversationHandler.END
else:
# Give existing token again
token = existing_token['token']
msg = text_existing_token.format(title=poll['title'], url=poll['url'],
token=token)
bot.sendMessage(update.message.chat_id, msg,
reply_markup=ReplyKeyboardHide())
return ConversationHandler.END
assert False # Should never reach here
评论列表
文章目录