1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

No asyncio.run()

This commit is contained in:
_run 2021-11-28 01:04:49 +05:00
parent d58336adcb
commit 411c7e915a
12 changed files with 22 additions and 30 deletions

View File

@ -1,5 +1,5 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
import telebot import telebot
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -8,4 +8,4 @@ async def make_some(message: telebot.types.ChatJoinRequest):
await bot.send_message(message.chat.id, 'I accepted a new user!') await bot.send_message(message.chat.id, 'I accepted a new user!')
await bot.approve_chat_join_request(message.chat.id, message.from_user.id) await bot.approve_chat_join_request(message.chat.id, message.from_user.id)
asyncio.run(bot.polling(skip_pending=True)) bot.polling(skip_pending=True)

View File

@ -1,6 +1,6 @@
from telebot import types,util from telebot import types,util
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
#chat_member_handler. When status changes, telegram gives update. check status from old_chat_member and new_chat_member. #chat_member_handler. When status changes, telegram gives update. check status from old_chat_member and new_chat_member.
@ -30,4 +30,4 @@ async def my_chat_m(message: types.ChatMemberUpdated):
@bot.message_handler(content_types=util.content_type_service) @bot.message_handler(content_types=util.content_type_service)
async def delall(message: types.Message): async def delall(message: types.Message):
await bot.delete_message(message.chat.id,message.message_id) await bot.delete_message(message.chat.id,message.message_id)
asyncio.run(bot.polling()) bot.polling()

View File

@ -1,4 +1,3 @@
import asyncio
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
from telebot import asyncio_filters from telebot import asyncio_filters
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -10,4 +9,4 @@ async def answer_for_admin(message):
# Register filter # Register filter
bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot)) bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot))
asyncio.run(bot.polling()) bot.polling()

View File

@ -40,5 +40,4 @@ async def bye_user(message):
bot.add_custom_filter(MainFilter()) bot.add_custom_filter(MainFilter())
bot.add_custom_filter(IsAdmin()) bot.add_custom_filter(IsAdmin())
import asyncio bot.polling()
asyncio.run(bot.polling())

View File

@ -1,6 +1,5 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import telebot import telebot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -15,5 +14,4 @@ async def not_admin(message):
# Do not forget to register # Do not forget to register
bot.add_custom_filter(telebot.asyncio_filters.ChatFilter()) bot.add_custom_filter(telebot.asyncio_filters.ChatFilter())
bot.polling()
asyncio.run(bot.polling())

View File

@ -1,6 +1,6 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import telebot import telebot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -19,4 +19,4 @@ async def text_filter(message):
bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter()) bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter())
bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter()) bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter())
asyncio.run(bot.polling()) bot.polling()

View File

@ -1,6 +1,5 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import telebot import telebot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -18,4 +17,4 @@ async def text_filter(message):
bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter())
bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter()) bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter())
asyncio.run(bot.polling()) bot.polling()

View File

@ -1,7 +1,6 @@
import telebot import telebot
from telebot import asyncio_filters from telebot import asyncio_filters
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -72,4 +71,4 @@ bot.add_custom_filter(asyncio_filters.IsDigitFilter())
# set saving states into file. # set saving states into file.
bot.enable_saving_states() # you can delete this if you do not need to save states bot.enable_saving_states() # you can delete this if you do not need to save states
asyncio.run(bot.polling()) bot.polling()

View File

@ -4,7 +4,6 @@
# It echoes any incoming text messages. # It echoes any incoming text messages.
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@ -24,4 +23,4 @@ async def echo_message(message):
await bot.reply_to(message, message.text) await bot.reply_to(message, message.text)
asyncio.run(bot.polling()) bot.polling()

View File

@ -1,5 +1,4 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
async def start_executor(message): async def start_executor(message):
@ -16,4 +15,4 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command
# bot.register_edited_message_handler(*args, **kwargs) # bot.register_edited_message_handler(*args, **kwargs)
# And other functions.. # And other functions..
asyncio.run(bot.polling(skip_pending=True)) bot.polling(skip_pending=True)

View File

@ -1,5 +1,5 @@
from telebot.async_telebot import AsyncTeleBot from telebot.async_telebot import AsyncTeleBot
import asyncio
bot = AsyncTeleBot('TOKEN') bot = AsyncTeleBot('TOKEN')
@bot.message_handler(commands=['start', 'help']) @bot.message_handler(commands=['start', 'help'])
@ -10,4 +10,4 @@ async def send_welcome(message):
async def echo_all(message): async def echo_all(message):
await bot.reply_to(message, message.text) await bot.reply_to(message, message.text)
asyncio.run(bot.polling(skip_pending=True))# Skip pending skips old updates bot.polling(skip_pending=True)# Skip pending skips old updates

View File

@ -138,7 +138,7 @@ class AsyncTeleBot:
json_updates = await asyncio_helper.get_updates(self.token, offset, limit, timeout, allowed_updates, request_timeout) json_updates = await asyncio_helper.get_updates(self.token, offset, limit, timeout, allowed_updates, request_timeout)
return [types.Update.de_json(ju) for ju in json_updates] return [types.Update.de_json(ju) for ju in json_updates]
async def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20, def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
request_timeout: int=20, allowed_updates: Optional[List[str]]=None, request_timeout: int=20, allowed_updates: Optional[List[str]]=None,
none_stop: Optional[bool]=None): none_stop: Optional[bool]=None):
""" """
@ -167,10 +167,10 @@ class AsyncTeleBot:
non_stop = none_stop non_stop = none_stop
if skip_pending: if skip_pending:
await self.skip_updates() asyncio.run(self.skip_updates())
await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates) asyncio.run(self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates))
async def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR, def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR,
allowed_updates: Optional[List[str]]=None, *args, **kwargs): allowed_updates: Optional[List[str]]=None, *args, **kwargs):
""" """
Wrap polling with infinite loop and exception handling to avoid bot stops polling. Wrap polling with infinite loop and exception handling to avoid bot stops polling.
@ -190,12 +190,12 @@ class AsyncTeleBot:
so unwanted updates may be received for a short period of time. so unwanted updates may be received for a short period of time.
""" """
if skip_pending: if skip_pending:
await self.skip_updates() asyncio.run(self.skip_updates())
self._polling = True self._polling = True
while self._polling: while self._polling:
try: try:
await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout, asyncio.run( self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
allowed_updates=allowed_updates, *args, **kwargs) allowed_updates=allowed_updates, *args, **kwargs) )
except Exception as e: except Exception as e:
if logger_level and logger_level >= logging.ERROR: if logger_level and logger_level >= logging.ERROR:
logger.error("Infinity polling exception: %s", str(e)) logger.error("Infinity polling exception: %s", str(e))