Asuncio: RuntimeError: Timeout context manager should be used inside a task
Делаю голосового помощника, который будет отправлять мне сообщения, если я говорю "надо купить...". Но когда говорю, вылезает ошибка.
import speech_recognition as sr
import pyttsx3
import logging
from threading import Thread
import asyncio
from aiogram import Bot, Dispatcher, executor, types
API_TOKEN = ''
CHAT_ID = ''
# Configure logging
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
mic = sr.Microphone(device_index=0)
rec = sr.Recognizer()
started = False
async def send_to_chat(text):
await bot.send_message(CHAT_ID, text)
def writer():
while True:
with sr.Microphone() as source:
audio = rec.listen(source)
try:
voice = rec.recognize_google(audio, language='ru-RU').lower()
print(voice)
if 'надо купить' in voice:
asyncio.run(send_to_chat(voice))
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
thread1 = Thread(target=writer)
if __name__ == '__main__':
thread1.start()
executor.start_polling(dp, skip_updates=True)
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\threading.py", line 888, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\d323i\Desktop\Даня\Голосовой ассистент\vw.py", line 36, in writer
asyncio.run(send_to_chat(voice))
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "C:\Users\d323i\Desktop\Даня\Голосовой ассистент\vw.py", line 24, in send_to_chat
await bot.send_message(1476545271, text)
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\bot.py", line 318, in send_message
result = await self.request(api.Methods.SEND_MESSAGE, payload)
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\base.py", line 208, in request
return await api.make_request(self.session, self.server, self.__token, method, data, files,
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiogram\bot\api.py", line 139, in make_request
async with session.post(url, data=req, **kwargs) as response:
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 1117, in __aenter__
self._resp = await self._coro
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\client.py", line 448, in _request
with timer:
File "C:\Users\d323i\AppData\Local\Programs\Python\Python39\lib\site-packages\aiohttp\helpers.py", line 635, in __enter__
raise RuntimeError(
RuntimeError: Timeout context manager should be used inside a task