mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Asyncio.run back
This commit is contained in:
parent
7588c9fb9f
commit
e92946301f
@ -84,4 +84,5 @@ async def back_callback(call: types.CallbackQuery):
|
||||
|
||||
|
||||
bot.add_custom_filter(ProductsCallbackFilter())
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -8,4 +8,5 @@ async def make_some(message: telebot.types.ChatJoinRequest):
|
||||
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)
|
||||
|
||||
bot.polling(skip_pending=True)
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
@ -30,4 +30,5 @@ async def my_chat_m(message: types.ChatMemberUpdated):
|
||||
@bot.message_handler(content_types=util.content_type_service)
|
||||
async def delall(message: types.Message):
|
||||
await bot.delete_message(message.chat.id,message.message_id)
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -1,6 +1,6 @@
|
||||
from telebot.async_telebot import AsyncTeleBot
|
||||
from telebot import asyncio_filters
|
||||
bot = AsyncTeleBot('TOKEN')
|
||||
bot = AsyncTeleBot('1297441208:AAGiez5xhzai5russPtPKmZjbdjybW4T0U8')
|
||||
|
||||
# Handler
|
||||
@bot.message_handler(chat_types=['supergroup'], is_chat_admin=True)
|
||||
@ -9,4 +9,6 @@ async def answer_for_admin(message):
|
||||
|
||||
# Register filter
|
||||
bot.add_custom_filter(asyncio_filters.IsAdminFilter(bot))
|
||||
bot.polling()
|
||||
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -40,4 +40,5 @@ async def bye_user(message):
|
||||
bot.add_custom_filter(MainFilter())
|
||||
bot.add_custom_filter(IsAdmin())
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -14,4 +14,5 @@ async def not_admin(message):
|
||||
|
||||
# Do not forget to register
|
||||
bot.add_custom_filter(telebot.asyncio_filters.ChatFilter())
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -19,4 +19,5 @@ async def text_filter(message):
|
||||
bot.add_custom_filter(telebot.asyncio_filters.IsReplyFilter())
|
||||
bot.add_custom_filter(telebot.asyncio_filters.ForwardFilter())
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -17,4 +17,5 @@ async def text_filter(message):
|
||||
bot.add_custom_filter(telebot.asyncio_filters.TextMatchFilter())
|
||||
bot.add_custom_filter(telebot.asyncio_filters.TextStartsFilter())
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -71,4 +71,5 @@ bot.add_custom_filter(asyncio_filters.IsDigitFilter())
|
||||
# set saving states into file.
|
||||
bot.enable_saving_states() # you can delete this if you do not need to save states
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
@ -17,4 +17,5 @@ async def new_message(message: telebot.types.Message):
|
||||
await bot.edit_message_text(chat_id=message.chat.id, message_id=result_message.id, text='<i>Done!</i>', parse_mode='HTML')
|
||||
|
||||
|
||||
bot.polling(skip_pending=True)
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -23,4 +23,5 @@ async def echo_message(message):
|
||||
await bot.reply_to(message, message.text)
|
||||
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -24,4 +24,5 @@ async def photo_send(message: telebot.types.Message):
|
||||
|
||||
|
||||
|
||||
bot.polling(skip_pending=True)
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -36,4 +36,5 @@ bot.setup_middleware(SimpleMiddleware(2))
|
||||
async def start(message):
|
||||
await bot.send_message(message.chat.id, 'Hello!')
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
@ -45,4 +45,5 @@ async def start(message, data: dict):
|
||||
await bot.send_message(message.chat.id, data['response'])
|
||||
|
||||
|
||||
bot.polling()
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -15,4 +15,6 @@ bot.register_message_handler(start_executor, commands=['start']) # Start command
|
||||
# bot.register_edited_message_handler(*args, **kwargs)
|
||||
# And other functions..
|
||||
|
||||
bot.polling(skip_pending=True)
|
||||
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -24,4 +24,6 @@ async def photos_send(message: telebot.types.Message):
|
||||
|
||||
|
||||
|
||||
bot.polling(skip_pending=True)
|
||||
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
||||
|
@ -10,4 +10,6 @@ async def send_welcome(message):
|
||||
async def echo_all(message):
|
||||
await bot.reply_to(message, message.text)
|
||||
|
||||
bot.polling(skip_pending=True)# Skip pending skips old updates
|
||||
|
||||
import asyncio
|
||||
asyncio.run(bot.polling(skip_pending=True)) # to skip updates
|
||||
|
@ -11,4 +11,6 @@ async def update_listener(messages):
|
||||
|
||||
bot.set_update_listener(update_listener)
|
||||
|
||||
bot.polling()
|
||||
|
||||
import asyncio
|
||||
asyncio.run(bot.polling())
|
@ -211,7 +211,7 @@ class AsyncTeleBot:
|
||||
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]
|
||||
|
||||
def polling(self, non_stop: bool=False, skip_pending=False, interval: int=0, timeout: int=20,
|
||||
async 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,
|
||||
none_stop: Optional[bool]=None):
|
||||
"""
|
||||
@ -240,10 +240,10 @@ class AsyncTeleBot:
|
||||
non_stop = none_stop
|
||||
|
||||
if skip_pending:
|
||||
asyncio.run(self.skip_updates())
|
||||
asyncio.run(self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates))
|
||||
await self.skip_updates()
|
||||
await self._process_polling(non_stop, interval, timeout, request_timeout, allowed_updates)
|
||||
|
||||
def infinity_polling(self, timeout: int=20, skip_pending: bool=False, request_timeout: int=20, logger_level=logging.ERROR,
|
||||
async 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):
|
||||
"""
|
||||
Wrap polling with infinite loop and exception handling to avoid bot stops polling.
|
||||
@ -263,12 +263,12 @@ class AsyncTeleBot:
|
||||
so unwanted updates may be received for a short period of time.
|
||||
"""
|
||||
if skip_pending:
|
||||
asyncio.run(self.skip_updates())
|
||||
await self.skip_updates()
|
||||
self._polling = True
|
||||
while self._polling:
|
||||
try:
|
||||
asyncio.run( self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
|
||||
allowed_updates=allowed_updates, *args, **kwargs) )
|
||||
await self._process_polling(non_stop=True, timeout=timeout, request_timeout=request_timeout,
|
||||
allowed_updates=allowed_updates, *args, **kwargs)
|
||||
except Exception as e:
|
||||
if logger_level and logger_level >= logging.ERROR:
|
||||
logger.error("Infinity polling exception: %s", str(e))
|
||||
|
Loading…
Reference in New Issue
Block a user