import pymysql, time, asyncio, requests, sys, threading, os, socks
from telethon import TelegramClient, events
from telethon.tl.functions.channels import JoinChannelRequest
pid = os.getpid()
print(pid)
r = requests.post('http://ip:5000/api/select/account', json={"token": ""})
if r.json()['Status'] == 2:
print("Нет доступных аккаунтов")
sys.exit()
else:
x, Phone, Api_id, Api_hash = r.json()['Id'], r.json()['Phone'], r.json()['Api_id'], r.json()['Api_hash']
client = TelegramClient(session=str(f'C:\\Users\\Админ\\Desktop\\RG\\new\\session{x}'), api_id=Api_id, api_hash=Api_hash).start()
a = requests.post('http://ip:5000/api/set/status/pid', json={"token": "", "x": x, "pid": pid})
@client.on(events.NewMessage(func=lambda e: e.is_private))
async def handler(event):
try:
print(event.message.message)#текст
await client.get_dialogs()
id = event.message.peer_id.user_id#юзерка пользователя
info = await client.get_entity(event.message.peer_id.user_id)
print(info.to_dict()['username'])#юзерка
username = info.to_dict()['username']
if username is None:
username = "None"
z = requests.post('http://ip:5000/api/upload/message', json={"token": "", "x": x, "Text": event.message.message, "User_id": id, "Username": username, "Number": Phone, "id": x})
except Exception as e:
print(e)
async def join(Task, client):
print("1")
print(Task)
return await client(JoinChannelRequest(Task))
async def send(Task, Message, client):
print("2")
print(Task, Message)
return await client.send_message(f"{Task}", f"{Message}")
#/api/check/task/account
def t():
while True:
r = requests.post('http://ip:5000/api/check/task/account', json={"token": "", "Phone": Phone})
if r.json()['status'] == 2:
time.sleep(2)
else:
Type = r.json()['Type']
if Type == "join":
Task = r.json()['Task']
_thread = threading.Thread(target=asyncio.run, args=(join(Task, client),)).start()
if Type == "send":
Task = r.json()['Task']
Message = r.json()['Message']
_thread = threading.Thread(target=asyncio.run, args=(send(Task, Message, client),)).start()
threading.Thread(target=t, args=()).start()
client.run_until_disconnected()
当向函数发送任务(加入、发送)时,它们根本不会被执行,尽管如果它不是以异步方式启动,一切正常。