Reputation: 133
I write a bot, that work with the telethont and the telebot. The telebot need to listen a bot in the telegram, he sends commands. The telethon need to listen alien channels, and to parse posts. I want to add a opportunity, that will be to add many listens. But appear error:
/home/home/.local/lib/python3.6/site-packages/telebot/util.py:60: RuntimeWarning: coroutine 'take_message' was never awaited
task(*args, **kwargs)
How to fix this error?
Main code:
# -*- coding: utf-8 -*-
import telebot
import asyncio
from telegram_client import TelegramSession as ts
api_id = ********************** # этот id вы получаете при создание приложения
api_hash = '***********************' # и этот тоже
username = '***************' # ваше имя в тг
id_channel = -***************** # id вашего канала
token = '********************' # токен вашего бота
bot = telebot.TeleBot(token)
client = ts(api_id,api_hash,username)
@bot.message_handler(content_types=['text'])
async def take_message(message):
if '@' in message.text and ',' in message.text:
channels = message.text.split(',')
my_channel = channels[-1]
parse_channel = channels[0:-1]
await client.add_task(parse_channel,my_channel)
else:
return bot.send_message(message.chat.id,'<b>Проверьте правильность ввода!</b>',parse_mode='html')
if __name__ == '__main__':
bot.polling(none_stop=True)
Code the module telegram_client:
from telethon import TelegramClient,sync,events,utils
import asyncio
class TelegramSession:
def __init__(self,api_id,api_hash,username):
self.__loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.__loop)
self.client = TelegramClient(username, api_id, api_hash,loop=self.__loop)
self.client.start()
async def handler_channel(self,parse_channel,my_channel):
#print(parse_channel)
@self.client.on(events.NewMessage(chats=parse_channel))
async def normal_handler(event):
await self.client.send_message(my_channel,event.message)
async def list_tasks(self):
pass
async def add_task(self,parse_channel,my_channel):
print(parse_channel)
await self.__loop.create_task(self.handler_channel(parse_channel,my_channel))
async def stop_task(self,something):
pass
def run_until(self):
self.client.run_until_disconnected()
Upvotes: 2
Views: 1734
Reputation: 15309
You seem to be mixing two frameworks here, telebot
and telethon
. telethon
is based around the async-await
syntax (asynchronous code) while telebot
is not (synchronous code).
The fact that async and sync code don't play well together and can't easily be called from each other has been a major criticism of the introduction of async-await
in Python.
Thus the issue is you have:
async def take_message(message):
...
await client.add_task(parse_channel,my_channel)
when telebot
expects:
def take_message(message):
# ...and NO 'await's inside function body!
You couuuuld attempt to add the task you start in take_message
to telethon
's event loop (using loop.create_task
) inside the synchronous code, but I wouldn't recommend that. Instead, I would try to do what you need to do using just one framework, not both at once.
Upvotes: 3