Alex Kerr
Alex Kerr

Reputation: 1080

How to forward (filter) Telegram messages from a private group to myself based on sender?

I'm in a private Telegram group that has a continual stream of many messages from a large number of people. I need to somehow pull out just those from a handful of specific people, and forward them to myself somehow - e.g. a new group I'm the only member of, or private chat, or some other way (email?). I just need to read them, not reply.

I'm a full stack developer (PHP back end but willing to consider others if works better) and know how to interface with APIs etc so can write some code to do this.

Can anyone advise on what API functions I'd use, or other approach here - and is it the Telegram or Bots API? Or is there a pre-existing solution out there?

Asking here as I've done my own research and not finding any solutions!

Upvotes: 2

Views: 4326

Answers (1)

GioIacca9
GioIacca9

Reputation: 436

You can create a bot and put it in the private group. Set the bot as administrator so it can read all messages from all users. For each message the users send in the group, the bot will recive an update containing a message object. The message object contains all the information about the message just sent in the group, including the user's data. To filter a message from a specific user you can compare the user_id in $update['message']['from']['id'] with an array containing all the user_ids of the users you want to forward their messages to you, with the in_array php function passing the user_id and the array. Then, if it match, use the forwardMessage method with these parameters:

{
  "chat_id": "YOUR_USER_ID",
  "from_chat_id": "PRIVATE_GROUP_CHAT_ID",
  "message_id": "THE_FILTERED_MESSAGE_ID"
}

If you use your user_id in the chat_id field, the bot will forward the message from the private group to your chat with it. The from_chat_id field is the chat_id of the private group (you can get it from $update['message']['chat']['id']). The message_id is the unique identifier for a message in that private group. It's contained in $update['message']['message_id'].

Instead of comparing the user_id to filter users, you can compare the first_name, last_name or username of the users but only the user_id is unique for each user and can not be changed.


You are using the Bot API.

Upvotes: 1

Related Questions