Пытаюсь сделать отправление сообщений в телеграме от именя пользователя через потоки
(api пустое потому что не хочу показывать, sqlite там для красоты, потому что с ним в потоках тоже много ошибок)
Код:
import time
from telethon import TelegramClient, sync
import sys
import sqlite3
import threading
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
api_id =
api_hash =
client = TelegramClient('spamer', api_id, api_hash)
client.start()
def message_thread(username, mess, interval):
while True:
client.send_message(username, mess)
time.sleep(interval)
def spamer():
try:
username = str(input('username: '))
message = str(input('message: '))
interval = float(input('interval: '))
thread = (username, message, interval)
cursor.execute("""CREATE TABLE IF NOT EXISTS users(
username TEXT,
message TEXT,
interval REAL);
""")
connect.commit()
cursor.execute(f"SELECT * FROM users WHERE username = '{username}'")
if cursor.fetchone() is None:
cursor.execute("INSERT INTO users VALUES(?, ?, ?);", thread)
connect.commit()
else:
cursor.execute(f"UPDATE users SET message='{message}', interval={interval} WHERE username='{username}';")
connect.commit()
threading.Thread(target=message_thread, args=(username, message, interval)).start()
spamer()
except Exception as e:
print(f'\n{repr(e)}\n')
spamer()
spamer()
ошибка:
Traceback (most recent call last):
File "C:\Users\User\.virtualenvs\TelegramBotClient-GI09_Qiv\lib\site-packages\telethon\helpers.py", line 431, in get_running_loop
return asyncio.get_running_loop()
RuntimeError: no running event loop
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\game\python\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "d:\game\python\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:/Users/User/PycharmProjects/TelegramBotClient/main.py", line 21, in message_thread
client.send_message(username, mess)
File "C:\Users\User\.virtualenvs\TelegramBotClient-GI09_Qiv\lib\site-packages\telethon\sync.py", line 35, in syncified
loop = helpers.get_running_loop()
File "C:\Users\User\.virtualenvs\TelegramBotClient-GI09_Qiv\lib\site-packages\telethon\helpers.py", line 433, in get_running_loop
return asyncio.get_event_loop_policy().get_event_loop()
File "d:\game\python\lib\asyncio\events.py", line 639, in get_event_loop
raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.
d:\game\python\lib\threading.py:934: RuntimeWarning: coroutine 'MessageMethods.send_message' was never awaited
self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback