[英]How can I send messages to my private telegram channel with Telethon?
[英]how i can send message to my contact whit telethon API python telegram
當我使用此命令查看聯系人時:
result = client.invoke(GetContactsRequest(""))
print(result)
我看到這個結果:
(contacts.contacts (ID: 0x6f8b8cb2) = (contacts=['(contact (ID: 0xf911c994) = (user_id=334412783, mutual=False))'], users=['(user (ID: 0x2e13f4c3) = (is_self=None, contact=True, mutual_contact=None, deleted=None, bot=None, bot_chat_history=None, bot_nochats=None, verified=None, restricted=None, min=None, bot_inline_geo=None, id=334412783, access_hash=-8113372651091717470, first_name=khood, last_name=None, username=Mosafer575, phone=19132594548, photo=(userProfilePhoto (ID: 0xd559d8c8) = (photo_id=1436291966805583785, photo_small=(fileLocation (ID: 0x53d69076) = (dc_id=1, volume_id=803110857, local_id=86736, secret=1232685751818265379)), photo_big=(fileLocation (ID: 0x53d69076) = (dc_id=1, volume_id=803110857, local_id=86738, secret=3801220285627155105)))), status=(userStatusOffline (ID: 0x8c703f) = (was_online=2017-06-16 13:09:57)), bot_info_version=None, restriction_reason=None, bot_inline_placeholder=None, lang_code=None))']))
現在我如何向這位朋友發送一條消息,其中包含我的朋友的名字或用戶 ID 或電話或我的聯系人中的其他詳細信息??? 我看到這個頁面,但我沒有注意到。 請為此使用簡單的代碼
如果是您的聯系人,您可以像使用用戶名一樣使用電話號碼:
client.send_message('+xx123456789', 'hello')
舊答案:
users=['(user (ID: 0x2e13f4c3) ...`
users
列表包含您要與之交談的用戶。 所以你得到了那個用戶:
user = result.users[0]
然后你可以調用.send_message(user, 'your message')
。
https://stackoverflow.com/content/img/progress-dots.gif輕松顯示所有聯系人和發送消息
from telethon.tl.types import InputPhoneContact
from telethon.tl.functions.contacts import ImportContactsRequest
導入后
contacts = client(GetContactsRequest(0))
client.send_message(contact[<You can use indexing>],'<messages>')
您可以使用聯系人 ID 發送消息
如果發送消息所有合同號...
all_contacts = await client(GetContactsRequest(hash=0))
for i in range(len(all_contacts.users)):
if all_contacts.users[i].phone != None:
await client.send_message(all_contacts.users[i].phone, 'hello')
else:
await client.send_message(all_contacts.users[i].username, 'hello')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.