简体   繁体   English

Telethon,如何获得实体?

[英]Telethon, how to get an entity?

I started using Telethon to integrate a python app with telegram API.我开始使用Telethon将 python 应用程序与电报 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.到目前为止,我是从返回实体列表的 get_dialogs 函数中获取这个实体的。 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.我知道我想向哪个组发送消息并且不想每次都通过 get_dialogs 来获取实体。

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.我期望应该有一个函数可以获取组 ID(或来自组的类似独特功能)作为输入并将实体传递给我作为响应。 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):

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 . send_message实际需要的是InputPeer ,在您的情况下可以是InputChat

Assuming you know what the ID of your chat is, you can do the following:假设您知道聊天的 ID 是什么,您可以执行以下操作:

from telethon.tl.types import InputPeerChat

chat = InputPeerChat(desired_chat_id)
client.send_message(chat, 'your message')

You might keep it empty array if you don't need it. 如果不需要,可以将其保留为空数组。

It is use to markup text with bold, italic, etc. 用于用粗体,斜体等标记文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM