【问题标题】:Sending message with Telethon(Telegram API Client for Python)使用 Telethon 发送消息(Python 的 Telegram API 客户端)
【发布时间】:2018-09-16 14:07:46
【问题描述】:

我想使用电话号码发送带有 Telethon 的消息,但它给了我一个电话格式不正确的错误。这是我的代码:

from telethon import TelegramClient
from telethon.tl.types import PeerUser

api_id = 123456
api_hash = 'Something'

client = TelegramClient('Telethon', api_id, api_hash)
client.start()

contact = client.get_entity("+98XXXXXXXXXX")

注意:Python 3.6 版和 Telethon 的最新版本。

【问题讨论】:

  • 您之前是否通过 Telethon 登录过您的客户?实际上,client = TelegramClient('

标签: python client telegram telethon


【解决方案1】:

get_entity 仅适用于保存的电话号码。您必须先将电话号码保存在联系人中,然后才能获取用户实体。要保存联系人,您可以执行以下操作:

from telethon.tl.types import InputPhoneContact
from telethon.tl.functions.contacts import ImportContactsRequest

# Here you must connect to your client.
contact = InputPhoneContact(
        client_id=0,
        phone=phone_number,
        first_name="FN",
        last_name="LN"
    ) # For new contacts you should use client_id = 0
    result = client(ImportContactsRequest([contact]))
    try:
        client.get_entity(phone_number)
        print("There is an entity with the phone number")
    except:
        print("There is no such entity")

【讨论】:

  • 我试过你的例子并得到cannot get declaration to go to如果我想导航到ImportContactsRequest
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 2014-08-11
  • 1970-01-01
相关资源
最近更新 更多