简体   繁体   English

如何处理 Telegram Bot 中的消息?

[英]How to handle messages in a Telegram Bot?

My goal is to make a Telegram Bot using Python that does the following:我的目标是使用 Python 制作一个 Telegram Bot,它执行以下操作:

  1. The user types a command用户键入命令
  2. The bot explains what the user should type next.机器人会解释用户下一步应该输入什么。
  3. The user types certain information用户键入某些信息
  4. I use that info to fetch a value in a python dictionary.我使用该信息在 python 字典中获取一个值。
  5. The bot replies with that info机器人回复该信息

I have already created the bot and set the command.我已经创建了机器人并设置了命令。 The problem is that I don't know how to keep the bot "listening".问题是我不知道如何让机器人“倾听”。 I have looked up in the docs but I have only found getUpdates, which gets the user's responses but only when you hit run.我查阅了文档,但只找到了 getUpdates,它会获取用户的响应,但仅当您点击运行时。

res=requests.get(url=f"https://api.telegram.org/bot{bot_token}/getUpdates")

I would like to set a webhook, and I know there is a method for this but I'm not shure about how this works.我想设置一个 webhook,我知道有一个方法可以做到这一点,但我不确定它是如何工作的。

When you are a PHP programmer, setting the webhook through a URL and using setWebhook would do the trick and telegram will send the result to this link whenever a user sends an update to the bot.当你是 PHP 程序员时,通过 URL 设置 webhook 并使用setWebhook就可以做到这一点,只要用户向机器人发送更新,电报就会将结果发送到此链接。 But, Python programmers have to use a different approach, I think.但是,我认为 Python 程序员必须使用不同的方法。 The main, and while, the easiest approach to make the bot listen permanently to the request is to python-telegram-bot module.使机器人永久监听请求的主要且最简单的方法是使用python-telegram-bot模块。

There are a few links that can help to build your first pythonic bot that can respond to users' updates.:有几个链接可以帮助构建您的第一个可以响应用户更新的 pythonic 机器人:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM