Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telethon, how to get an entity?

I started using Telethon to integrate a python app with telegram API. I was able to get it started and send a few messages.

The function for sending messages gets an entity as the first argument. So far I was getting this entity from the get_dialogs function which returns list of entities. I know which group I want to send messages to and don't want to go through get_dialogs every time to get the entity.

So which function can I use to give me an entity to pass it to send message? I am expecting there should be a function which gets a group id (or a similar unique feature from the group) as an input and passes me the entity in response. But so far I wasn't able to locate any function.

def send_message(self,
                     entity,# <--------------- how can I get this entity?
                     message,
                     markdown=False,
                     no_web_page=False):
like image 870
apadana Avatar asked Jun 08 '17 22:06

apadana


1 Answers

You could potentially save the group/chat/user or anything you want in a external file, if you don't want to query it each time. What send_message actually takes is an InputPeer, that can be, in your case, an InputChat.

Assuming you know what the ID of your chat is, you can do the following:

from telethon.tl.types import InputPeerChat

chat = InputPeerChat(desired_chat_id)
client.send_message(chat, 'your message')
like image 176
Lonami Avatar answered Oct 10 '22 23:10

Lonami