使用 Telegram bot 检索所有聊天 ID [英] Retrieve all chat ids using Telegram bot

查看:417
本文介绍了使用 Telegram bot 检索所有聊天 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要问题是我如何获得与机器人进行的所有对话的聊天 ID?

the main question is how do I get the chat ids for all the conversations ever held with the bot?

想象一下,在执行机器人的过程中,有一个与用户 A 的对话.

Imagine that during the execution of the bot there is a conversation with the user A.

现在我停止机器人进程并重新启动它.

Now I stop the bot process and start it again.

如何获取与用户 A 过去聊天的聊天 ID?

How do I get the chat id of that past chat with the user A?

我知道当用户向您发送消息时您会获得聊天 ID,并且您使用该 ID 进行回复,但是如果用户 A 在当前执行期间不再向机器人发送消息怎么办?如何获取过去的对话ID?

I understand you get the chat id when the user sends you a message, and you use that id to reply, but what if user A no longer sends messages to the bot during the current execution? how to get the past conversation id?

是唯一一种在第二次执行开始时存储 id 和检索它们的选项吗?

Is the only one option to store the ids and retrieve them when the second execution starts?

更新:

看起来当前的解决方案是将聊天 ID 存储在安全的地方,正如@Tick Tock 所回答的那样.

Looks like the current solution is to store the chat id somewhere safe, as answered by @Tick Tock.

推荐答案

我不清楚你的问题,但据我从你的问题中了解到,我写了一些东西给你,希望对你有所帮助.您可以检索 chat_id 并使用它向该 chat 发送内容.我会给出一个示例代码,但在我解释之前.

Your question is unclear to me but as I understand from your question I wrote something to you hope be helpful. You can retrieve chat_ids and use it to send something to that chat. I would give a sample code but before let me explain something.

在 Telegram Bot API 中有两个定义:chat_idfrom_id.

In Telegram Bot API there is two definitions: chat_id and from_id.

1-当我们在 private 聊天时,chat_idfrom_id 是相等的.

1-When we are in private chat with some one chat_id and from_id are equal.

2-当我们的 bot 是 group 成员时,chat_id 是该组的 id 并且不同于那个人的 id(from_id) 可能会向群组发送一些内容(并且可能我们的机器人也会收到它——当 privacy_mode 关闭时)

2-When our bot is a group member, then chat_id is id of that group and is different from that person id(from_id) may be send something to group(and maybe our bot receive it too-when privacy_mode is off)

我假设您的机器人正在私人聊天中:当用户向您的机器人发送任何内容时,Telegram 将该消息发送给您的 BOT(调用您的脚本),此示例代码发送Hello chat_id";给那个用户.(在 PHP 中)

I assume your bot is in private chat: when user sends anything to your bot, then Telegram gives that message to your BOT( calls your script), this sample code sends "Hello chat_id" to that user.(in PHP)

define('BOT_TOKEN','12345:abcde');//replace with your bot token
$command_prefix_url='https://api.telegram.org/bot' . BOT_TOKEN ;

$update = json_decode(file_get_contents('php://input')); //retrieves data sent by telegram

$chat_id=$update->message->chat->id; //retrives `chat_id`

$rep = json_decode(file_get_contents($command_prefix_url . '/SendMessage?chat_id=' .
    $chat_id    . '&text=' . urldecode('Hello '.(string)$chat_id))); //send something to that `chat_id` (sender)

更新:(由于版本问题)

首先,chat_id 对于该用户来说是唯一的并且永远是永久的(在私人聊天中)即使您的机器人的用户离开您的机器人并重新加入.到目前为止,我没有听到或读到任何关于 Telegram 已提供一种方法来告诉您的 bot 其用户 chat_id 的方法,因此这是了解您的用户的最佳方式是将他们的 chat_id 和其他信息(您在一段时间内从用户接收的消息中收集)保存在数据库中.

First, chat_id is unique and always permanent for that user(in private chats) even if your bot's user leaves your bot and rejoin again. I don't hear or read anything up to now that Telegram have been provided a method to tell your bot WHOLE its users chat_id , so the best way to know about your users is to save their chat_id and other info (that you gather along the time from user from messages reciceve from) in a database.

如果您至少将他们的 chat_id 保存在一个简单的数据库中,那么您就有了一个机器人订阅用户的列表.由于 chat_id 是永久性的,您可以向用户发送任何您想要的内容.

If you save at least their chat_id in a simple database then you have a list of your bot's subscribed users. And since chat_id is permanent you can send anything you want to your users.

而且,我从你的问题中了解到,如果你没有数据库,但用户 A 是你的机器人的订阅用户,据我所知,你应该等到她/他向你发送一条消息,然后你抓住她/him chat_id 并将其添加到您的数据库中.现在你可以随时发送给她/他.

AND, as I understand from your question, IF you do not have a database but user A is your bot's subscribed user, AS I KNOW, you should wait until she/he send a single message to you, then you grab her/him chat_id and add it to your database. NOW you can send her/him every time anything.

这篇关于使用 Telegram bot 检索所有聊天 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆