Compare commits

..
10 Commits
Author SHA1 Message Date
Davte 654ac2d0fb Include last commit message in version report 2026-03-29 19:18:27 +02:00
Davte 6cf63716ff Compliance with bot API 9.0
Also, the repository is not hosted on GiTea
2025-04-15 21:27:19 +02:00
Davte 9c61b564b1 Working on compliance with bot API 9.0 2025-04-13 00:00:51 +02:00
Davte 4a79535321 Compliance with bot API 8.02
Also, closed sessions are now removed from Bot.sessions
2025-02-01 16:46:33 +01:00
Davte 425902d2ac Compliance with bot API 7.10 2024-09-06 16:01:50 +02:00
Davte 6759e5c704 Administrators may be set after administration module initialization
Do not check for administrators until bot has got information about itself. Prevent duplicate calls to bot.get_me() async method
2024-09-01 18:27:28 +02:00
Davte db240ce199 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).
2024-08-17 21:12:00 +02:00
Davte 02386d69da Get package version from metadata when package has no __version__ attribute 2024-08-04 18:49:41 +02:00
Davte cc4cbbacbd Compliance with bot API 7.8 2024-08-04 18:29:21 +02:00
Davte fd4374f328 Compliance with bot API 7.7 2024-07-07 14:36:58 +02:00
8 changed files with 1112 additions and 41 deletions
+1
View File
@@ -41,6 +41,7 @@ __pycache__/
# Distribution / packaging
.Python
env/
.venv/
build/
develop-eggs/
dist/
+1 -1
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.2"
__version__ = "2.10.11"
__maintainer__ = "Davide Testa"
__contact__ = "t.me/davte"
+95 -18
View File
@@ -18,6 +18,7 @@ import re
import types
from collections import OrderedDict
from importlib.metadata import version as get_package_version_from_metadata
from typing import Union, List, Tuple
# Third party modules
@@ -28,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
)
@@ -44,6 +45,13 @@ variable_regex = re.compile(r"(?P<name>[a-zA-Z]\w*)\s*=\s*"
r"\"[^\"]*\")")
def get_package_version(package: types.ModuleType):
"""Get version of given package."""
if hasattr(package, '__version__'):
return package.__version__
return get_package_version_from_metadata(package.__name__)
async def _forward_to(update,
bot: Bot,
sender,
@@ -828,7 +836,8 @@ def get_maintenance_exception_criterion(bot, allowed_command):
async def get_last_commit():
"""Get last commit hash and davtelepot version."""
"""Get last commit hash and message."""
last_commit_message = None
try:
_subprocess = await asyncio.create_subprocess_exec(
'git', 'rev-parse', 'HEAD',
@@ -836,12 +845,22 @@ async def get_last_commit():
stderr=asyncio.subprocess.STDOUT
)
stdout, _ = await _subprocess.communicate()
last_commit = stdout.decode().strip()
last_commit_hash = stdout.decode().strip()
except Exception as e:
last_commit = f"{e}"
if last_commit.startswith("fatal: not a git repository"):
last_commit = "-"
return last_commit
last_commit_hash = f"{e}"
try:
_subprocess = await asyncio.create_subprocess_exec(
'git', 'log', '--pretty=%B', "-n", "1", last_commit_hash,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.STDOUT
)
stdout, _ = await _subprocess.communicate()
last_commit_message = stdout.decode().strip()
except Exception:
pass
if last_commit_hash.startswith("fatal: not a git repository"):
last_commit_hash = "-"
return last_commit_hash, last_commit_message
async def get_new_versions(bot: Bot,
@@ -865,7 +884,12 @@ async def get_new_versions(bot: Bot,
"skipping...")
continue
new_version = web_page['info']['version']
current_version = package.__version__
try:
current_version = get_package_version(package)
except TypeError:
current_version = "NA"
logging.error("Could not get current version of "
"package %s", package.__name__)
notification_record = bot.db['updates_notifications'].find_one(
package=package.__name__,
order_by=['-id'],
@@ -886,16 +910,19 @@ async def get_new_versions(bot: Bot,
async def version_command(bot: Bot, update: dict,
user_record: OrderedDict, language: str):
last_commit = await get_last_commit()
last_commit_hash, last_commit_message = await get_last_commit()
text = bot.get_message(
'admin', 'version_command', 'header',
last_commit=last_commit,
last_commit=last_commit_hash,
update=update, user_record=user_record
) + '\n\n'
)
if last_commit_message:
text += "\n<i>" + last_commit_message + "</i>"
text += "\n\n"
text += f'<b>Python: </b> <code>{platform.python_version()}</code>\n'
text += '\n'.join(
f"<b>{package.__name__}</b>: "
f"<code>{package.__version__}</code>"
f"<code>{get_package_version(package)}</code>"
for package in bot.packages
)
temporary_message = await bot.send_message(
@@ -934,15 +961,15 @@ async def notify_new_version(bot: Bot):
Notify admins when last commit and/or davtelepot version change.
"""
last_commit = await get_last_commit()
last_commit_hash, last_commit_message = await get_last_commit()
old_record = bot.db['version_history'].find_one(
order_by=['-id']
)
current_versions = {
f"{package.__name__}_version": package.__version__
f"{package.__name__}_version": get_package_version(package)
for package in bot.packages
}
current_versions['last_commit'] = last_commit
current_versions['last_commit'] = last_commit_hash
if old_record is None:
old_record = dict(
updated_at=datetime.datetime.min,
@@ -965,13 +992,16 @@ async def notify_new_version(bot: Bot):
'admin', 'new_version', 'title',
user_record=admin
) + '\n\n'
if last_commit != old_record['last_commit']:
if last_commit_hash != old_record['last_commit']:
text += bot.get_message(
'admin', 'new_version', 'last_commit',
old_record=old_record,
new_record=current_versions,
user_record=admin
) + '\n\n'
)
if last_commit_message:
text += f"\n<i>{last_commit_message}</i>"
text += "\n\n"
text += '\n'.join(
f"<b>{name[:-len('_version')]}</b>: "
f"<code>{old_record[name]}</code> —> "
@@ -1846,6 +1876,52 @@ 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()
if len(bot.administrators) > 0:
return
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,
@@ -2075,3 +2151,4 @@ def init(telegram_bot: Bot,
update=update,
user_record=user_record,
language=language)
asyncio.ensure_future(create_promotion_command(bot=telegram_bot))
+926 -9
View File
File diff suppressed because it is too large Load Diff
+5 -3
View File
@@ -25,6 +25,7 @@ class TelegramApiMethod(object):
'Boolean': "bool",
'Integer': "int",
'Integer or String': "Union[int, str]",
'Array of Integer': "List[str]",
'String': "str",
}
"""Telegram bot API method."""
@@ -55,14 +56,15 @@ class TelegramApiMethod(object):
for n, paragraph in enumerate(self.description.replace('.', '.\n').split('\n')):
additional_indentation = 0
if n == 0 and paragraph.startswith(redundant_string):
paragraph = paragraph[len(redundant_string)].upper() + paragraph[len(redundant_string)+1:]
paragraph = (paragraph[len(redundant_string)].upper()
+ paragraph[len(redundant_string)+1:])
for word in paragraph.split(' '):
if len(current_line) + len(word) > 80 - indentation - additional_indentation:
additional_indentation = max(additional_indentation, 4)
result += f"{current_line.strip()}\n{' ' * additional_indentation}"
current_line = ""
current_line += f"{word} "
if len(current_line):
if len(current_line) > 0:
result += f"{current_line.strip()}\n"
current_line = ""
if n == 0:
@@ -203,7 +205,7 @@ async def print_api_methods(filename=None,
)
)
if output_file:
with open(output_file, 'w') as file:
with open(output_file, 'w', encoding="utf-8") as file:
if new_methods:
file.write(
"from typing import List, Union\n"
+73 -9
View File
@@ -122,12 +122,47 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
'edited_message': self.edited_message_handler,
'channel_post': self.channel_post_handler,
'edited_channel_post': self.edited_channel_post_handler,
'business_connection': self.get_update_handler(
update_type='business_connection'
),
'business_message': self.get_update_handler(
update_type='business_message'
),
'edited_business_message': self.get_update_handler(
update_type='edited_business_message'
),
'deleted_business_messages': self.get_update_handler(
update_type='deleted_business_messages'
),
'message_reaction': self.get_update_handler(
update_type='message_reaction'
),
'message_reaction_count': self.get_update_handler(
update_type='message_reaction_count'
),
'inline_query': self.inline_query_handler,
'chosen_inline_result': self.chosen_inline_result_handler,
'callback_query': self.callback_query_handler,
'shipping_query': self.shipping_query_handler,
'pre_checkout_query': self.pre_checkout_query_handler,
'purchased_paid_media': self.get_update_handler(
update_type='purchased_paid_media'
),
'poll': self.poll_handler,
'poll_answer': self.get_update_handler(
update_type='poll_answer'
),
'my_chat_member': self.get_update_handler(
update_type='my_chat_member'
),
'chat_member': self.get_update_handler( update_type='chat_member' ),
'chat_join_request': self.get_update_handler(
update_type='chat_join_request'
),
'chat_boost': self.get_update_handler( update_type='chat_boost' ),
'removed_chat_boost': self.get_update_handler(
update_type='removed_chat_boost'
),
}
# Different message update types need different handlers
self.message_handlers = {
@@ -221,6 +256,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
self.Role = None
self.packages = [sys.modules['davtelepot']]
self._documents_max_dimension = None
self._getting_me = False
self._got_me = False
# Add `users` table with its fields if missing
if 'users' not in self.db.tables:
table = self.db.create_table(
@@ -680,6 +717,18 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
)
return
def get_update_handler(self, update_type: str):
"""Get update handler for `update_type`."""
bot = self
async def handler(update, user_record, language=None):
"""Handle update message of type `update_type`"""
logging.info(
"The following update was received: %s\n"
"However, this `%s` handler does nothing yet.",
update, update_type
)
return handler
async def edited_channel_post_handler(self, update, user_record, language=None):
"""Handle Telegram `edited_channel_post` update."""
logging.info(
@@ -2804,9 +2853,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
"""
if not callable(condition):
raise TypeError(
'Condition {c} is not a callable'.format(
c=condition.__name__
)
f'Condition {condition.__name__} is not a callable'
)
def query_decorator(handler):
@@ -3147,26 +3194,41 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
Restart bots if bot can't be got.
"""
for _ in range(60):
if not self._getting_me:
break
await asyncio.sleep(0.5)
else:
raise TimeoutError("Getting bot information was in progress but "
"did not make it in 30 seconds...")
if self._got_me:
return
self._getting_me = True
try:
me = await self.getMe()
if isinstance(me, Exception):
raise me
elif me is None:
raise Exception('getMe returned None')
if me is None:
raise TypeError('getMe returned None')
self._name = me["username"]
self._telegram_id = me['id']
except Exception as e:
logging.error(
f"API getMe method failed, information about this bot could "
f"not be retrieved. Restarting in 5 minutes...\n\n"
f"Error information:\n{e}"
"API getMe method failed, information about this bot could "
"not be retrieved. Restarting in 5 minutes...\n\n"
"Error information:\n%s", e
)
self._getting_me = False
await asyncio.sleep(5*60)
if self._got_me:
return
self.__class__.stop(
message="Information about this bot could not be retrieved.\n"
"Restarting...",
final_state=65
)
self._getting_me = False
self._got_me = True
def setup(self):
"""Make bot ask for updates and handle responses."""
@@ -3181,9 +3243,11 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
async def close_sessions(self):
"""Close open sessions."""
for session_name, session in self.sessions.items():
for session_name in list(self.sessions.keys()):
session = self.sessions[session_name]
if not session.closed:
await session.close()
del self.sessions[session_name]
async def send_one_message(self, *args, **kwargs):
sent_message = await self.send_message(*args, **kwargs)
+10
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",
+1 -1
View File
@@ -51,7 +51,7 @@ setuptools.setup(
license=find_information("license", "davtelepot", "__init__.py"),
long_description=long_description,
long_description_content_type="text/markdown",
url="https://gogs.davte.it/davte/davtelepot",
url="https://gitea.davte.it/davte/davtelepot",
packages=setuptools.find_packages(),
platforms=['any'],
install_requires=[