How to get a message from Telegram groups by API?
Asked Answered
A

2

17

I was looking for some way to listen and catch new messages provided by telegram groups.

I have not found libraries or API in order to do this in Python.

Someone having any suggestion?

Atonsah answered 24/5, 2019 at 15:40 Comment(0)
R
18

There are two ways to achieve your goal:

Method 1:

My suggested library for python: python-telegram-bot

  1. Create a bot.
  2. Add the bot to the desired group as administrator.
  3. Listen to messages as you normally listen in bots.

Method 2:

My suggested library for python: Telethon

  1. Join the desired group as a user (not a bot).
  2. Create a simple client that listens to new messages.
Retain answered 26/5, 2019 at 7:59 Comment(3)
Add the bot to the desired group as administrator. What if group isn't own by us?Untaught
@Untaught turn off privacy setting from the bot and it should listen to messages normally.Endoparasite
dude i have an issue to read the content protected channel and i can't grab the text of post , i don't know how to solve it ...Indictable
A
28

Using Telethon

Replace channel_name with your telegram channel.

from telethon import TelegramClient, events, sync

# Remember to use your own values from my.telegram.org!
api_id = ...
api_hash = '...'
client = TelegramClient('anon', api_id, api_hash)

@client.on(events.NewMessage(chats='channel_name'))
async def my_event_handler(event):
    print(event.raw_text)

client.start()
client.run_until_disconnected()
Able answered 9/3, 2020 at 3:34 Comment(5)
Can you please provide the specific question you are asking, or where you are getting stuck?Calculating
Sorry, but I don't understand your question. This is an example of how using Telethon I can listen and catch new message of a specific group.Able
How to bypass this - Please enter your phone (or bot token):Ralaigh
I am unable to use this approach to access multiple groups. Say if I want to receive messages from multiple groups or channels.Heeley
Add the bot to the desired group as administrator.Untaught
R
18

There are two ways to achieve your goal:

Method 1:

My suggested library for python: python-telegram-bot

  1. Create a bot.
  2. Add the bot to the desired group as administrator.
  3. Listen to messages as you normally listen in bots.

Method 2:

My suggested library for python: Telethon

  1. Join the desired group as a user (not a bot).
  2. Create a simple client that listens to new messages.
Retain answered 26/5, 2019 at 7:59 Comment(3)
Add the bot to the desired group as administrator. What if group isn't own by us?Untaught
@Untaught turn off privacy setting from the bot and it should listen to messages normally.Endoparasite
dude i have an issue to read the content protected channel and i can't grab the text of post , i don't know how to solve it ...Indictable

© 2022 - 2024 — McMap. All rights reserved.