Updated examples

This commit is contained in:
Davte 2020-08-23 14:21:37 +02:00
parent b797f3fffe
commit 48b7e1e4a2
Signed by: Davte
GPG Key ID: 209AE674A0007425
3 changed files with 35 additions and 31 deletions

View File

@ -11,7 +11,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.6.14" __version__ = "2.6.15"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -46,7 +46,7 @@ import sys
try: try:
from davtelepot.bot import Bot from davtelepot.bot import Bot
from davtelepot.utilities import ( from davtelepot.utilities import (
get_cleaned_text, make_inline_keyboard, make_button get_cleaned_text, get_user, make_inline_keyboard, make_button
) )
except ImportError: except ImportError:
logging.error( logging.error(
@ -64,23 +64,27 @@ except ImportError:
path = os.path.dirname(os.path.abspath(__file__)) path = os.path.dirname(os.path.abspath(__file__))
def initialize_bot(bot): def initialize_bot(telegram_bot):
"""Take a bot and set commands.""" """Take a bot and set commands."""
bot.set_callback_data_separator('|') telegram_bot.set_callback_data_separator('|')
@bot.command(command='foo', aliases=['Foo'], show_in_keyboard=True, @telegram_bot.command(command='foo', aliases=['Foo'],
description="Reply 'bar' to 'foo'", show_in_keyboard=True,
authorization_level='everybody') description="Reply 'bar' to 'foo'",
authorization_level='everybody')
async def foo_command(bot, update, user_record): async def foo_command(bot, update, user_record):
return 'Bar!' return (
"Bar!\n\n"
f"You wrote: {update['text']}\n"
f"I am @{bot.name} and you are {get_user(user_record)}"
)
def is_bar_text_message(text): def is_bar_text_message(lowered_text):
return text.startswith('bar') return lowered_text.startswith('bar')
@bot.parser(condition=is_bar_text_message, @telegram_bot.parser(condition=is_bar_text_message,
description='Reply Foo to users who write Bar', description='Reply Foo to users who write Bar',
authorization_level='everybody', authorization_level='everybody')
argument='text')
async def bar_parser(bot, update): async def bar_parser(bot, update):
text_except_foo = get_cleaned_text(update, bot, ['bar']) text_except_foo = get_cleaned_text(update, bot, ['bar'])
return f"Foo!\n{text_except_foo}" return f"Foo!\n{text_except_foo}"
@ -99,16 +103,16 @@ def initialize_bot(bot):
3 3
) )
@bot.command(command='buttons') @telegram_bot.command(command='buttons')
async def buttons_command(): async def buttons_command():
return dict( return dict(
text="Press a button!", text="Press a button!",
reply_markup=get_keyboard() reply_markup=get_keyboard()
) )
@bot.button(prefix='button:///', separator='|', @telegram_bot.button(prefix='button:///', separator='|',
authorization_level='everybody') authorization_level='everybody')
async def buttons_button(bot, update, user_record, data): async def buttons_button(data):
button_number = data[0] button_number = data[0]
return dict( return dict(
edit=dict( edit=dict(
@ -120,12 +124,12 @@ def initialize_bot(bot):
def starts_with_a(text): def starts_with_a(text):
return text.startswith('a') return text.startswith('a')
@bot.query( @telegram_bot.query(
condition=starts_with_a, condition=starts_with_a,
description='Mirror query text if it starts with letter `a`', description='Mirror query text if it starts with letter `a`',
authorization_level='everybody' authorization_level='everybody'
) )
async def inline_query(bot, update, user_record): async def inline_query(update):
return dict( return dict(
type='article', type='article',
id=10, id=10,
@ -135,7 +139,7 @@ def initialize_bot(bot):
) )
) )
bot.set_default_inline_query_answer( telegram_bot.set_default_inline_query_answer(
dict( dict(
type='article', type='article',
id=0, id=0,
@ -146,7 +150,7 @@ def initialize_bot(bot):
) )
) )
bot.set_unknown_command_message( telegram_bot.set_unknown_command_message(
"<b>Currently supported features</b>\n\n" "<b>Currently supported features</b>\n\n"
"- /foo (or text starting with `foo`): replies `Bar!`.\n" "- /foo (or text starting with `foo`): replies `Bar!`.\n"
"- Text starting with `bar`: replies `Foo!` followed by the rest of " "- Text starting with `bar`: replies `Foo!` followed by the rest of "
@ -177,10 +181,10 @@ def _main():
) )
root_logger = logging.getLogger() root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) root_logger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler() console_handler = logging.StreamHandler()
consoleHandler.setFormatter(log_formatter) console_handler.setFormatter(log_formatter)
consoleHandler.setLevel(logging.DEBUG) console_handler.setLevel(logging.DEBUG)
root_logger.addHandler(consoleHandler) root_logger.addHandler(console_handler)
# Instantiate, initialize and make `simple_bot` run. # Instantiate, initialize and make `simple_bot` run.
simple_bot = Bot(token=simple_bot_token, database_url=f"{path}/bot.db") simple_bot = Bot(token=simple_bot_token, database_url=f"{path}/bot.db")

View File

@ -28,7 +28,7 @@ except ImportError:
from a_simple_bot import initialize_bot from a_simple_bot import initialize_bot
# Get path of current script # Get path of current script
os.path.dirname(os.path.abspath(__file__)) path = os.path.dirname(os.path.abspath(__file__))
def _main(): def _main():
@ -96,10 +96,10 @@ def _main():
) )
root_logger = logging.getLogger() root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG) root_logger.setLevel(logging.DEBUG)
consoleHandler = logging.StreamHandler() console_handler = logging.StreamHandler()
consoleHandler.setFormatter(log_formatter) console_handler.setFormatter(log_formatter)
consoleHandler.setLevel(logging.DEBUG) console_handler.setLevel(logging.DEBUG)
root_logger.addHandler(consoleHandler) root_logger.addHandler(console_handler)
# Instantiate, initialize and make bots run. # Instantiate, initialize and make bots run.
webhook_bot = Bot( webhook_bot = Bot(