【问题标题】:sending message to contacts in telethon python telegram在 Telethon python 电报中向联系人发送消息
【发布时间】:2021-06-12 20:42:19
【问题描述】:

如何查看我的所有联系人并向他们发送消息? 我使用 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()

【问题讨论】:

  • 不要在此处粘贴您的 api_hash 和 id。它可以被滥用。

标签: python api telegram telethon


【解决方案1】:

只需将此行添加到您的代码中:

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

您应该会在结果中看到联系人。

要向联系人发送消息,您可以使用telegram_client.py 中定义的send_message 函数,并在InteractiveTelegramClient.py 中有一个示例。

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

如果您需要更多详细信息,请在下方评论,我会尽力回复。

【讨论】:

  • 坦克(apadana,我认为你是波斯人和我)是我的工作..现在我如何为我的联系人发送消息..
  • client.send_message(InputPeerUser(u.id, u.access_hash), "hi") NameError: name 'InputPeerUser' is not defined
  • 添加这个导入:from telethon.tl.types.input_peer_user import InputPeerUser
  • 拜托我的兄弟可以帮我添加联系人吗?
  • 对不起,我没听懂你的最后一个问题。你问如何添加联系人?如果是,请发布一个新问题并联系我。
【解决方案2】:

发送空字符串对我不起作用:

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

所以我认为你应该改用“0”:

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

【讨论】:

    【解决方案3】:

    我认为在新的更新中client.invoke() 没有定义。 我使用了以下代码,它对我有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2021-06-11
      • 2017-11-19
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      相关资源
      最近更新 更多