如何在Telegram机器人中设置推送通知

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

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot注册Telegram机器人,你需要遵循以下步骤: 1. 首先,你需要创建一个Telegram Bot账号,可以通过联系BotFather创建。 2. 在Spring Boot应用程序,你需要添加Telegram Bot库。可以使用TelegramBots库来实现这一点。 3. 在你的应用程序添加Telegram Bot的配置,包括Bot的token和Webhook URL。可以使用@BotConfiguration注释来实现这一点。 4. 创建一个WebhookController类并添加一个@PostMapping方法。该方法应该处理来自Telegram的所有请求。 5. 在你的应用程序,启动Webhook并将其链接到Telegram Bot API。可以使用TelegramBots库的WebhookUtils类来实现这一点。 6. 最后,你需要启动你的Spring Boot应用程序。 下面是一个简单的示例代码,演示如何在Spring Boot注册Telegram机器人: ``` @BotConfiguration public class TelegramBotConfig { @Value("${telegram.bot.token}") private String botToken; @Value("${telegram.bot.webhook-url}") private String webhookUrl; @Bean public TelegramBot telegramBot() { TelegramBotsApi telegramBotsApi = new TelegramBotsApi(); try { TelegramBot telegramBot = new MyTelegramBot(botToken); telegramBotsApi.registerBot(telegramBot); telegramBot.setWebhook(webhookUrl); return telegramBot; } catch (TelegramApiException e) { e.printStackTrace(); return null; } } } @RestController public class WebhookController { @PostMapping("${telegram.bot.webhook-url}") public ResponseEntity<Object> handleUpdate(@RequestBody Update update) { // 处理来自Telegram的请求 return ResponseEntity.ok().build(); } } @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在这个例子,我们创建了一个名为MyTelegramBot的TelegramBot类,并将它注册到Telegram Bot API。我们还创建了一个名为WebhookController的类,用于处理来自Telegram的请求。最后,我们在Spring Boot应用程序启动Webhook,并将它链接到Telegram Bot API
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值