机器人消息电报bot之后,如何从用户那里获取消息更新

发布于 2025-02-04 19:00:36 字数 1470 浏览 4 评论 0 原文

因此,我是新手制作电报机器人的,我想拥有 botfather

user: create bot

bot: provide name for your bot

user: foo bot

将来使用用户文本。

我有一个简单的示例机器人,该机器人定义了用户提供的单词

user:/define internet

bot: defines the term

,但我想将其重新创建为

user:/define

bot: please send the word you want to define

user: internet

bot: defines the term

user: ....

我使用

这是我的代码

@bot.message_handler(commands=['define'])
def dictionary(message):
user_message = message.text
msg = user_message.split(' ')
if len(msg) != 1:
    try:
        word = msg[1]
        botmsg = bot.send_message(message.chat.id, 'Finding the word in the dictionary')
        resp = requests.get(DICT_URL + word)
        respData = resp.json()
        pof = respData[0]['meanings'][0]['partOfSpeech']
        print(pof)
        defi = respData[0]['meanings'][0]['definitions'][0]['definition']
        print(defi)
        bot.send_message(message.chat.id, text=f'*Word:* {word} \n\n *Part Of Speech: *{pof} \n\n *Definition: *{defi}', parse_mode='Markdown')
        bot.delete_message(message.chat.id, botmsg.message_id)
    except:
        bot.send_message(message.chat.id, "Couldn't find in the dictionary.")
else:
    bot.send_message(message.chat.id, 'Please input a word to define.')

So I'm new at making a telegram bot, I want to have like the botfather:

user: create bot

bot: provide name for your bot

user: foo bot

then the user text is used in the future.

I have a simple sample bot which defines a word that the user provide

user:/define internet

bot: defines the term

but I want to recreate it to

user:/define

bot: please send the word you want to define

user: internet

bot: defines the term

user: ....

I'm using pyTelegramBotAPI

Here is my code

@bot.message_handler(commands=['define'])
def dictionary(message):
user_message = message.text
msg = user_message.split(' ')
if len(msg) != 1:
    try:
        word = msg[1]
        botmsg = bot.send_message(message.chat.id, 'Finding the word in the dictionary')
        resp = requests.get(DICT_URL + word)
        respData = resp.json()
        pof = respData[0]['meanings'][0]['partOfSpeech']
        print(pof)
        defi = respData[0]['meanings'][0]['definitions'][0]['definition']
        print(defi)
        bot.send_message(message.chat.id, text=f'*Word:* {word} \n\n *Part Of Speech: *{pof} \n\n *Definition: *{defi}', parse_mode='Markdown')
        bot.delete_message(message.chat.id, botmsg.message_id)
    except:
        bot.send_message(message.chat.id, "Couldn't find in the dictionary.")
else:
    bot.send_message(message.chat.id, 'Please input a word to define.')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2025-02-11 19:00:36

我找到了感谢@furas的答案,@furas帮助他的评论

无法与我同样的问题,这是代码。

@bot.message_handler(commands=['define'])
def send_welcome(message):
    msg = bot.reply_to(message, "What word you want to define?")
    bot.register_next_step_handler(msg, define)


def define(message):
    chat_id = message.chat.id
    word = message.text
    bot.send_message(chat_id, f'the word you want to define is {word}')

bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

bot.infinity_polling()

I've found the answer thanks to @furas who helped with his comments

incase someone has the same problem with me here's the code.

@bot.message_handler(commands=['define'])
def send_welcome(message):
    msg = bot.reply_to(message, "What word you want to define?")
    bot.register_next_step_handler(msg, define)


def define(message):
    chat_id = message.chat.id
    word = message.text
    bot.send_message(chat_id, f'the word you want to define is {word}')

bot.enable_save_next_step_handlers(delay=2)
bot.load_next_step_handlers()

bot.infinity_polling()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文