From 40465643b9227e4ed10bf825672462517029007d Mon Sep 17 00:00:00 2001 From: Artem Lavrenov Date: Fri, 21 Jan 2022 12:32:23 +0300 Subject: [PATCH 1/3] Add timer_bot sync and async example --- .../asynchronous_telebot/timer_bot_async.py | 52 +++++++++++++++++++ examples/timer_bot.py | 43 +++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 examples/asynchronous_telebot/timer_bot_async.py create mode 100644 examples/timer_bot.py diff --git a/examples/asynchronous_telebot/timer_bot_async.py b/examples/asynchronous_telebot/timer_bot_async.py new file mode 100644 index 0000000..1d1da65 --- /dev/null +++ b/examples/asynchronous_telebot/timer_bot_async.py @@ -0,0 +1,52 @@ +#!/usr/bin/python3 + +# This is a simple bot with schedule timer +# https://github.com/ibrb/python-aioschedule +# https://schedule.readthedocs.io + +import asyncio +import os, aioschedule +from telebot.async_telebot import AsyncTeleBot + +API_TOKEN = '' +bot = AsyncTeleBot(API_TOKEN) + + +async def beep(chat_id) -> None: + """Send the beep message.""" + await bot.send_message(chat_id, text='Beep!') + aioschedule.clear(chat_id) # return schedule.CancelJob not working in aioschedule use tag for delete + + +@bot.message_handler(commands=['help', 'start']) +async def send_welcome(message): + await bot.reply_to(message, "Hi! Use /set to set a timer") + + +@bot.message_handler(commands=['set']) +async def set_timer(message): + args = message.text.split() + if len(args) > 1 and args[1].isdigit(): + sec = int(args[1]) + aioschedule.every(sec).seconds.do(beep, message.chat.id).tag(message.chat.id) + else: + await bot.reply_to(message, 'Usage: /set ') + + +@bot.message_handler(commands=['unset']) +def unset_timer(message): + aioschedule.clean(message.chat.id) + + +async def scheduler(): + while True: + await aioschedule.run_pending() + await asyncio.sleep(1) + + +async def main(): + await asyncio.gather(bot.infinity_polling(), scheduler()) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/examples/timer_bot.py b/examples/timer_bot.py new file mode 100644 index 0000000..85f6697 --- /dev/null +++ b/examples/timer_bot.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +# This is a simple bot with schedule timer +# https://schedule.readthedocs.io + +import os, time, threading, schedule +from telebot import TeleBot + +API_TOKEN = '' +bot = TeleBot(API_TOKEN) + + +@bot.message_handler(commands=['help', 'start']) +def send_welcome(message): + bot.reply_to(message, "Hi! Use /set to set a timer") + + +def beep(chat_id) -> None: + """Send the beep message.""" + bot.send_message(chat_id, text='Beep!') + return schedule.CancelJob # Run a job once + + +@bot.message_handler(commands=['set']) +def set_timer(message): + args = message.text.split() + if len(args) > 1 and args[1].isdigit(): + sec = int(args[1]) + schedule.every(sec).seconds.do(beep, message.chat.id).tag(message.chat.id) + else: + bot.reply_to(message, 'Usage: /set ') + + +@bot.message_handler(commands=['unset']) +def unset_timer(message): + schedule.clean(message.chat.id) + + +if __name__ == '__main__': + threading.Thread(target=bot.infinity_polling, name='bot_infinity_polling', daemon=True).start() + while True: + schedule.run_pending() + time.sleep(1) From a07bf86c30c9c177fee09694ce9962dcb34d005e Mon Sep 17 00:00:00 2001 From: Artem Lavrenov Date: Fri, 21 Jan 2022 21:50:33 +0300 Subject: [PATCH 2/3] add default None for get_my_commands parameters scope and language_code sync\async, add examples for bot.set_my_commands --- .../set_command_example.py | 36 +++++++++++++++++++ examples/set_command_example.py | 29 +++++++++++++++ telebot/__init__.py | 6 ++-- telebot/async_telebot.py | 4 +-- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 examples/asynchronous_telebot/set_command_example.py create mode 100644 examples/set_command_example.py diff --git a/examples/asynchronous_telebot/set_command_example.py b/examples/asynchronous_telebot/set_command_example.py new file mode 100644 index 0000000..fae588d --- /dev/null +++ b/examples/asynchronous_telebot/set_command_example.py @@ -0,0 +1,36 @@ +#!/usr/bin/python + +# This is a set_my_commands example. +# Press on [/] button in telegram client. +# Important, to update the command menu, be sure to exit the chat with the bot and log in again +# Important, command for chat_id and for group have a higher priority than for all + +import asyncio +import os +import telebot +from telebot.async_telebot import AsyncTeleBot + + +API_TOKEN = '' +bot = AsyncTeleBot(API_TOKEN) + + +async def main(): + # use in for delete with the necessary scope and language_code if necessary + await bot.delete_my_commands(scope=None, language_code=None) + + await bot.set_my_commands( + commands=[ + telebot.types.BotCommand("command1", "command1 description"), + telebot.types.BotCommand("command2", "command2 description") + ], + # scope=telebot.types.BotCommandScopeChat(12345678) # use for personal command menu for users + # scope=telebot.types.BotCommandScopeAllPrivateChats() # use for all private chats + ) + + cmd = await bot.get_my_commands(scope=None, language_code=None) + print([c.to_json() for c in cmd]) + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/examples/set_command_example.py b/examples/set_command_example.py new file mode 100644 index 0000000..ba38c12 --- /dev/null +++ b/examples/set_command_example.py @@ -0,0 +1,29 @@ +#!/usr/bin/python + +# This is a set_my_commands example. +# Press on [/] button in telegram client. +# Important, to update the command menu, be sure to exit the chat with the bot and enter to chat again +# Important, command for chat_id and for group have a higher priority than for all + +import os +import telebot + + +API_TOKEN = '' +bot = telebot.TeleBot(API_TOKEN) + +# use in for delete with the necessary scope and language_code if necessary +bot.delete_my_commands(scope=None, language_code=None) + +bot.set_my_commands( + commands=[ + telebot.types.BotCommand("command1", "command1 description"), + telebot.types.BotCommand("command2", "command2 description") + ], + # scope=telebot.types.BotCommandScopeChat(12345678) # use for personal command for users + # scope=telebot.types.BotCommandScopeAllPrivateChats() # use for all private chats +) + +# check command +cmd = bot.get_my_commands(scope=None, language_code=None) +print([c.to_json() for c in cmd]) diff --git a/telebot/__init__.py b/telebot/__init__.py index 5fed5c1..549ac06 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -1899,8 +1899,8 @@ class TeleBot: """ return apihelper.delete_chat_photo(self.token, chat_id) - def get_my_commands(self, scope: Optional[types.BotCommandScope], - language_code: Optional[str]) -> List[types.BotCommand]: + def get_my_commands(self, scope: Optional[types.BotCommandScope]=None, + language_code: Optional[str]=None) -> List[types.BotCommand]: """ Use this method to get the current list of the bot's commands. Returns List of BotCommand on success. @@ -3415,4 +3415,4 @@ class TeleBot: - \ No newline at end of file + diff --git a/telebot/async_telebot.py b/telebot/async_telebot.py index 6bfa799..689ba9f 100644 --- a/telebot/async_telebot.py +++ b/telebot/async_telebot.py @@ -2443,8 +2443,8 @@ class AsyncTeleBot: """ return await asyncio_helper.delete_chat_photo(self.token, chat_id) - async def get_my_commands(self, scope: Optional[types.BotCommandScope], - language_code: Optional[str]) -> List[types.BotCommand]: + async def get_my_commands(self, scope: Optional[types.BotCommandScope]=None, + language_code: Optional[str]=None) -> List[types.BotCommand]: """ Use this method to get the current list of the bot's commands. Returns List of BotCommand on success. From 7eb759d1fd8c61f50d2890a9a8bf9fcd8eda6ae5 Mon Sep 17 00:00:00 2001 From: Artem Lavrenov Date: Fri, 21 Jan 2022 22:25:06 +0300 Subject: [PATCH 3/3] remove unused import --- examples/asynchronous_telebot/set_command_example.py | 1 - examples/asynchronous_telebot/timer_bot_async.py | 2 +- examples/set_command_example.py | 1 - examples/timer_bot.py | 2 +- 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/asynchronous_telebot/set_command_example.py b/examples/asynchronous_telebot/set_command_example.py index fae588d..2de53f4 100644 --- a/examples/asynchronous_telebot/set_command_example.py +++ b/examples/asynchronous_telebot/set_command_example.py @@ -6,7 +6,6 @@ # Important, command for chat_id and for group have a higher priority than for all import asyncio -import os import telebot from telebot.async_telebot import AsyncTeleBot diff --git a/examples/asynchronous_telebot/timer_bot_async.py b/examples/asynchronous_telebot/timer_bot_async.py index 1d1da65..c263dcb 100644 --- a/examples/asynchronous_telebot/timer_bot_async.py +++ b/examples/asynchronous_telebot/timer_bot_async.py @@ -5,7 +5,7 @@ # https://schedule.readthedocs.io import asyncio -import os, aioschedule +import aioschedule from telebot.async_telebot import AsyncTeleBot API_TOKEN = '' diff --git a/examples/set_command_example.py b/examples/set_command_example.py index ba38c12..1573e91 100644 --- a/examples/set_command_example.py +++ b/examples/set_command_example.py @@ -5,7 +5,6 @@ # Important, to update the command menu, be sure to exit the chat with the bot and enter to chat again # Important, command for chat_id and for group have a higher priority than for all -import os import telebot diff --git a/examples/timer_bot.py b/examples/timer_bot.py index 85f6697..d82e2d0 100644 --- a/examples/timer_bot.py +++ b/examples/timer_bot.py @@ -3,7 +3,7 @@ # This is a simple bot with schedule timer # https://schedule.readthedocs.io -import os, time, threading, schedule +import time, threading, schedule from telebot import TeleBot API_TOKEN = ''