gpt4 book ai didi

Python Telegram Bot : Access to contact information

转载 作者:行者123 更新时间:2023-11-28 21:39:40 26 4
gpt4 key购买 nike

以下代码请求用户的位置和联系方式:

def contact(bot, update):
dp = DjangoTelegramBot.dispatcher

con_keyboard = KeyboardButton(text="send_contact", request_contact=True)
loc_keyboard = KeyboardButton(text="send_location", request_location=True)
custom_keyboard = [[ con_keyboard ,loc_keyboard]]
reply_markup = ReplyKeyboardMarkup(custom_keyboard)

bot.sendMessage(update.message.chat_id,
text="Would you mind sharing your location and contact with me",
reply_markup=reply_markup)

我的问题是如何在用户点击发送联系人按钮后获取电话号码?
PS:我用的是python-telegram-botdjango-telegrambot

最佳答案

我不确定 django-telegrambot。但如果我没记错的话,你的 reply_markup 就是 python-telegram-bot 下的那个。按下按钮时,用户的电话号码将作为 Contact 发送到您的机器人。目的。你可以用 MessageHandler 捕捉它和一个 Filters.contact过滤器。

from telegram.ext import MessageHander, Filters

def contact_callback(bot, update):
contact = update.effective_message.contact
phone = contact.phone_number

dispatcher.add_handler(MessageHandler(Filters.contact, contact_callback))

关于Python Telegram Bot : Access to contact information,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com