简体   繁体   English

在telethon python电报中向联系人发送消息

[英]sending message to contacts in telethon python telegram

How can I see all my contacts and send them messages?如何查看我的所有联系人并向他们发送消息? i use Telethon (API telegram python).我使用 Telethon(API 电报 python)。

from telethon.tl.functions.contacts import ResolveUsernameRequest
from telethon.tl.types import InputChannelEmpty
from telethon import TelegramClient
from telethon.tl.types.messages import Messages
from telethon.tl.types.contacts import Contacts
api_id = 1****
api_hash = '5fbd2d************************'
client = TelegramClient('arta0', api_id, api_hash)
client.connect()

Just add this line to your code:只需将此行添加到您的代码中:

contacts = client.invoke(GetContactsRequest(""))
print(contacts)

And you should see the contacts in the result.您应该会在结果中看到联系人。

To send messages to contacts, you can use the send_message function defined in telegram_client.py and has an example in InteractiveTelegramClient.py .将消息发送到联系人,你可以使用send_message中定义的函数telegram_client.py并在一个例子InteractiveTelegramClient.py

for u in contacts.users: 
    client.send_message(InputPeerUser(u.id, u.access_hash), "hi")

If you need more details comment below and I will try to reply.如果您需要更多详细信息,请在下方评论,我会尽力回复。

Sending empty string didn't worked for me:发送空字符串对我不起作用:

contacts = client.invoke(GetContactsRequest(""))
*** struct.error: required argument is not an integer

So I think you should use '0' instead:所以我认为你应该使用 '0' 代替:

contacts = client.invoke(GetContactsRequest(0))
print(contacts)

I think in new update client.invoke() is not defined.我认为在新的更新client.invoke()中没有定义。 I used following code and it worked for me.我使用了以下代码,它对我有用。

from telethon.tl.functions.contacts import GetContactsRequest
contacts = client(GetContactsRequest(0))

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

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