Compliance with bot API 7.9

Implemented a system to get administration privileges if running the bot in absence of administrators (the token is provided through the command line).
This commit is contained in:
Davte 2024-08-17 21:12:00 +02:00
parent 02386d69da
commit db240ce199
Signed by: Davte
GPG Key ID: 8A65AF02E41CA5C9
4 changed files with 94 additions and 4 deletions

View File

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

View File

@ -29,8 +29,8 @@ from davtelepot.messages import default_admin_messages, default_talk_messages
from davtelepot.bot import Bot
from davtelepot.utilities import (
async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text,
get_user, clean_html_string, line_drawing_unordered_list, make_button,
make_inline_keyboard, remove_html_tags, send_part_of_text_file,
get_secure_key, get_user, clean_html_string, line_drawing_unordered_list,
make_button, make_inline_keyboard, remove_html_tags, send_part_of_text_file,
send_csv_file, make_lines_of_buttons, join_path
)
@ -1859,6 +1859,50 @@ async def config_command(bot: Bot, update: dict,
language=language)
async def become_administrator(bot: Bot, update: dict,
user_record: dict, language: str):
"""When the bot has no administrator, become one providing a token.
The token will be printed to the stdout on the machine running the bot.
"""
if len(bot.administrators) > 0:
return
def _get_message(*args):
return bot.get_message('admin', 'become_admin', *args,
update=update, user_record=user_record,
language=language)
token = get_cleaned_text(update=update, bot=bot,
replace=['become_administrator',
'00become_administrator'],
strip='/ @_')
if token != bot.administration_token:
return _get_message('wrong_token')
with bot.db as db:
db['users'].update({**user_record, 'privileges': 1},
['id'])
return _get_message('success')
async def create_promotion_command(bot: Bot):
"""If bot has no administrators, users can elevate themselves.
To do so, they need to provide a token, that will be printed to the stdout
of the machine running the bot.
"""
await bot.get_me()
bot.administration_token = get_secure_key(length=10)
print(f"To become administrator click "
f"https://t.me/{bot.name}?start="
f"00become_administrator_{bot.administration_token}")
@bot.command(command='become_administrator',
authorization_level='everybody',
aliases=['00become_administrator'])
async def _become_administrator(bot, update, user_record, language):
return await become_administrator(bot=bot, update=update,
user_record=user_record,
language=language)
def init(telegram_bot: Bot,
talk_messages: dict = None,
admin_messages: dict = None,
@ -2088,3 +2132,6 @@ def init(telegram_bot: Bot,
update=update,
user_record=user_record,
language=language)
if len(telegram_bot.administrators) == 0:
asyncio.ensure_future(create_promotion_command(bot=telegram_bot))

View File

@ -3109,7 +3109,8 @@ class TelegramBot:
async def sendPaidMedia(self, chat_id: Union[int, str], star_count: int,
media: List[InputPaidMedia],
caption: str, parse_mode: str,
business_connection_id: str = None,
caption: str = None, parse_mode: str = None,
caption_entities: List[dict] = None,
show_caption_above_media: bool = None,
disable_notification: bool = None,
@ -3155,3 +3156,35 @@ class TelegramBot:
'refundStarPayment',
parameters=locals()
)
async def createChatSubscriptionInviteLink(self, chat_id: Union[int, str],
name: str,
subscription_period: int,
subscription_price: int):
"""Create a subscription invite link for a channel chat.
The bot must have the can_invite_users administrator rights.
The link can be edited using the method editChatSubscriptionInviteLink
or revoked using the method revokeChatInviteLink.
Returns the new invite link as a ChatInviteLink object.
See https://core.telegram.org/bots/api#createchatsubscriptioninvitelink
for details.
"""
return await self.api_request(
'createChatSubscriptionInviteLink',
parameters=locals()
)
async def editChatSubscriptionInviteLink(self, chat_id: Union[int, str],
invite_link: str, name: str):
"""Edit a subscription invite link created by the bot.
The bot must have the can_invite_users administrator rights.
Returns the edited invite link as a ChatInviteLink object.
See https://core.telegram.org/bots/api#editchatsubscriptioninvitelink
for details.
"""
return await self.api_request(
'editChatSubscriptionInviteLink',
parameters=locals()
)

View File

@ -22,6 +22,16 @@ davtelepot_messages = {
}
default_admin_messages = {
'become_admin': {
'success': {
'en': "🎉 You are now administrator! 👑",
'it': "🎉 Ora hai diritti di amministrazione! 👑",
},
'wrong_token': {
'en': "❌ Wrong token 🚷",
'it': "❌ Password errata 🚷",
},
},
'cancel': {
'button': {
'en': "↩️ Cancel",