python操作telegram
操作telegram需要⾹港的linux,然后python配置socket,反正我不会
telegram的注册需要⽤⼿机注册,挺⿇烦的,
配置官⽹的地址和配置key的地址,csdn好像不给发出来
⾃⼰去百度下
⼀开始我想通过其他,把⼈拉到⾃⼰的,但没成功,原因是因为,必须是⾃⼰的好友才能拉进⾃⼰的,
# Use your own values
⾃⼰根据上⾯地址注册的机器⼈
api_id =
api_hash =''
telethon直接pip安装最新的就好了
import builtins
from telethon import TelegramClient, events, sync
from telethon.tl.custom import button
from telethon.ssages import AddChatUserRequest
from telethon.tl.functions.users import GetFullUserRequest
phone_number ='+86'# ⽤户号码
client = TelegramClient(phone_number, api_id, api_hash)
号的地址,⾃⼰建个可以到号的地址
channel ='t.me/joinchat/AeSndeZfpeQxMTI1'
import time
from telethon import functions
all_id=[]
button=1
chat_id=None
监听事件,当你收到⼀条信息,⽆论是也好或者个⼈也好都会触发这个
@ister(events.NewMessage)
async def msg_event_handler(event):
global chat_id;global all_id
if(chat_id==event.chat_id):
message = ssage
print(888888)
# 判断是否是组或者频道发送的消息
# if event.is_channel or event.is_group:
## 获取消息的id,
print(event.sender_id)
print(event.chat_id)
all_id.append(event.sender_id)
print(888888)
# await client.send_message(event.sender_id, 'What are you sending?')
client.add_event_handler(callback=msg_event_handler,
event=events.NewMessage(incoming=True))
async def main():
global button;global chat_id;global all_id
发信息
# await client.send_message('键平键平', 'Hello, myself!')
python新手代码userid
# You can, of course, use markdown in your messages:
# You can, of course, use markdown in your messages:
# message = await client.send_message(
#    '键平键平',
#    '1111',
#    link_preview=False
# )
# # You can reply to messages directly if you have a message object
# ply('Cool!')
可以拿到发给个⼈的信息
# You can print the message history of any chat:
# async for message in client.iter_messages('键平'):
#    )
可以根据地址拿到⾥⾯的信息
messages = client.iter_messages(channel, limit=1)
async for message in messages:
)
这⾥可以拿到id
chat_id=message.chat_id
print(chat_id)
button=0
可以拿到⾃⼰所有好友或者号的id,这个id可以⽤来拉⽤
# async for dialog in client.iter_dialogs():
#    print(dialog.name, 'has ID', dialog.id)
# path = await client.download_profile_photo(channel)
# print(path)
# @(events.UserUpdate)
# async def handler(event):
#    # If someone is uploading, say something
#    if event.uploading:
#        await client.send_message(event.user_id, 'What are you sending?')
# result = await client(functions.photos.GetUserPhotosRequest(
#    user_id="",
#    offset=0,
#    max_id=0,
#    limit=100
# ))
这all_id是数组,看我上⾯的监听函数,只要有⼈发信息,就把他的id记下来,然后放到数组⾥⾯,下⼀次循环就会把这个⼈拉进,只是不是好友拉不进,while(len(all_id)>0):
id=all_id.pop()
try:
await client(AddChatUserRequest(
这个chat_id是拿到的id,id上⾯的中间部分代码可以拿到
chat_id,
这个是监听拿到的id
id,
fwd_limit=10# Allow the user to see the 10 last messages
))
except Exception as e:
print("eeee")
while1:
with client:
client.loop.run_until_complete(main())
time.sleep(10)
好好看我的中⽂⽂字,国内关于这个记载的不多,尽⼒了,

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。