vote.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:democracybot 作者: LiaungYip 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号