Uvicorn running on http://0.0.0.0:8383 (Press CTRL+C to quit)
Bot is running...
Exception in thread Thread-2 (run_telegram_bot):
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "/Users/yosef/uni/Uni chat store/index.py", line 23, in run_telegram_bot
TelegramNotification().lunch_bot()
File "/Users/yosef/uni/Uni chat store/utils/telegram/send_notifictions.py", line 51, in lunch_bot
application.run_polling(1.0)
File "/Users/yosef/.local/share/virtualenvs/Uni_chat_store-eDiP6l1M/lib/python3.10/site-packages/telegram/ext/_application.py", line 836, in run_polling
return self.__run(
File "/Users/yosef/.local/share/virtualenvs/Uni_chat_store-eDiP6l1M/lib/python3.10/site-packages/telegram/ext/_application.py", line 1007, in __run
loop = asyncio.get_event_loop()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/events.py", line 656, 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-2 (run_telegram_bot)'.
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/threading.py
self._invoke_excepthook(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
это функция запуска Telegram из класса TelegramNotification
def lunch_bot(self):
application = Application.builder().token(self.BOT_TOKEN).build()
print('Bot is running...')
# Commands
application.add_handler(CommandHandler('start', self.start))
# Run bot
application.run_polling(1.0)
а это мой файл index.py
from fastapi import FastAPI
from routers import whatsapp_webhook, payment_method_form
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
from utils.telegram.send_notifictions import TelegramNotification
import threading
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all methods
allow_headers=["*"], # Allows all headers
)
app.include_router(whatsapp_webhook.router)
app.include_router(payment_method_form.router)
def run_telegram_bot():
TelegramNotification().lunch_bot()
# run the whatsapp webhook
def run_whatsapp_webhook():
uvicorn.run("index:app", host="0.0.0.0", port=8383)
if __name__ == '__main__':
threading.Thread(target=run_whatsapp_webhook).start()
threading.Thread(target=run_telegram_bot).start()
Подробнее здесь: https://stackoverflow.com/questions/781 ... -same-code