by Nikita Kholin
通过尼基塔·霍林(Nikita Kholin)
如何在Telegram机器人中设置推送通知 (How to set up push notifications in your Telegram bot)
Telegram is a great platform with lots of great users (I’m a Telegram user myself). And what would be the best way for Telegram users to receive notifications? We can’t know for sure. Maybe they like email or something else. But we can guess that sending notifications to Telegram would be pretty convenient.
Telegram是一个很棒的平台,拥有很多很棒的用户(我自己也是Telegram用户)。 电报用户接收通知的最佳方式是什么? 我们不能确定。 也许他们喜欢电子邮件或其他东西。 但是我们可以猜测,将通知发送到Telegram将非常方便。
If you would like to send Telegram notifications from your application, you’ve come to the right place. I’ve added this feature to my application and I love it.
如果您想从应用程序发送电报通知,那么您来对地方了。 我已经将此功能添加到我的应用程序中,并且我喜欢它。
One quick note. In this article I provide examples of code in Python. But the ideas are not Python-specific and can be translated into another language without any hastle.
快速说明。 在本文中,我提供了Python代码示例。 但是这些想法不是特定于Python的,可以毫不费力地将其翻译成另一种语言。
So without further ado, let’s dive into how we can do it.
因此,事不宜迟,让我们深入研究如何做到这一点。
创建一个电报机器人 (Create a Telegram bot)
First of all, you need to create a Telegram bot. To do this you need to use another Telegram bot, BotFather. Just talk to him (press start).
首先,您需要创建一个Telegram机器人。 为此,您需要使用另一个Telegram机器人BotFather 。 只是和他说话(按开始)。
Now you see what it can do. But what interests us is creating a new bot, so that’s what we’re going to choose (/newbot
).
现在您知道它可以做什么。 但是,我们感兴趣的是创建一个新的bot,因此我们将选择( /newbot
)。
You’re quickly going to find out that bot’s name should end with “bot”. And since you’re like me and coming to the game too late, most bot names are already taken.
您很快就会发现bot的名称应以“ bot”结尾。 而且由于您和我一样,并且来不及参加游戏,因此大多数机器人名称已经被采用。
But eventually, you’re going to find a name for your bot and get an access token we’re going to need.
但是最终,您将为您的机器人找到一个名称,并获得我们需要的访问令牌。
Now that you have a bot, Telegram users can find and use it. But there is one problem — you can’t associate users that come from Telegram to the users in your application. Let me show you why.
现在您有了一个机器人,Telegram用户可以找到并使用它。 但是有一个问题-您无法将来自Telegram的用户与应用程序中的用户相关联。 让我告诉你为什么。
Once a user presses the “Start” button on your bot, you will receive an “update”. You can check all bot’s updates even in your browser by visiting the following URL https://api.telegram.org/bot{bot_token}/getUpdates
(don’t forget to use your access token in the URL). Here’s what I got:
用户按下机器人上的“开始”按钮后,您将收到“更新”。 您甚至可以通过访问以下URL https://api.telegram.org/bot{bot_token}/getUpdates
{bot_token} / https://api.telegram.org/bot{bot_token}/getUpdates
(甚至不要在URL中使用访问令牌)来检查所有bot的更新,甚至在浏览器中也是如此。 这是我得到的:
Can’t read anything? Don’t worry. You can fix it by installing some JSON prettifier extension in your browser. I use JSON Formatter for Chrome. It looks so much better.
看不到任何东西? 不用担心 您可以通过在浏览器中安装一些JSON前缀扩展程序来修复它。 我将JSON Formatter用于Chrome。 看起来好多了。
So as you can see we don’t get that much information about the person. From this information, we can get their full name. But it would be lucky if the user would provide their full name in your application, and doesn’t guarantee uniqueness of it. So we can’t use that to find a user in your applications.
因此,正如您所看到的,我们没有得到有关此人的太多信息。 从这些信息中,我们可以获得他们的全名。 但是,如果用户在您的应用程序中提供其全名,并且不保证其唯一性,那将是幸运的。 因此,我们不能使用它在您的应用程序中找到用户。
Another piece of information we get is the username. That is more useful as it is unique between all Telegram users. But most likely you don’t have that available in your applicat