6

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):
4

1 回答 1

5

如果您不想每次都查询它,您可以将组/聊天/用户或您想要的任何内容保存在外部文件中。send_message 实际上需要的是一个,InputPeer在你的情况下,它可以是一个InputChat

假设您知道聊天的 ID 是什么,您可以执行以下操作:

from telethon.tl.types import InputPeerChat

chat = InputPeerChat(desired_chat_id)
client.send_message(chat, 'your message')
于 2017-06-09T14:06:51.863 回答