|
@@ -13,14 +13,16 @@ import asyncio
|
|
import datetime
|
|
import datetime
|
|
import json
|
|
import json
|
|
import logging
|
|
import logging
|
|
|
|
+import types
|
|
|
|
|
|
-from typing import Union
|
|
|
|
|
|
+from typing import Union, List
|
|
|
|
|
|
# Third party modules
|
|
# Third party modules
|
|
from sqlalchemy.exc import ResourceClosedError
|
|
from sqlalchemy.exc import ResourceClosedError
|
|
|
|
|
|
# Project modules
|
|
# Project modules
|
|
-from . import bot as davtelepot_bot, messages
|
|
|
|
|
|
+from . import messages
|
|
|
|
+from .bot import Bot
|
|
from .utilities import (
|
|
from .utilities import (
|
|
async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text,
|
|
async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text,
|
|
get_user, escape_html_chars, line_drawing_unordered_list, make_button,
|
|
get_user, escape_html_chars, line_drawing_unordered_list, make_button,
|
|
@@ -29,7 +31,11 @@ from .utilities import (
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
-async def _forward_to(update, bot, sender, addressee, is_admin=False):
|
|
|
|
|
|
+async def _forward_to(update,
|
|
|
|
+ bot: Bot,
|
|
|
|
+ sender,
|
|
|
|
+ addressee,
|
|
|
|
+ is_admin=False):
|
|
if update['text'].lower() in ['stop'] and is_admin:
|
|
if update['text'].lower() in ['stop'] and is_admin:
|
|
with bot.db as db:
|
|
with bot.db as db:
|
|
admin_record = db['users'].find_one(
|
|
admin_record = db['users'].find_one(
|
|
@@ -64,7 +70,10 @@ async def _forward_to(update, bot, sender, addressee, is_admin=False):
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-def get_talk_panel(bot, update, user_record=None, text=''):
|
|
|
|
|
|
+def get_talk_panel(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record=None,
|
|
|
|
+ text: str = ''):
|
|
"""Return text and reply markup of talk panel.
|
|
"""Return text and reply markup of talk panel.
|
|
|
|
|
|
`text` may be:
|
|
`text` may be:
|
|
@@ -202,7 +211,9 @@ def get_talk_panel(bot, update, user_record=None, text=''):
|
|
return text, reply_markup
|
|
return text, reply_markup
|
|
|
|
|
|
|
|
|
|
-async def _talk_command(bot, update, user_record):
|
|
|
|
|
|
+async def _talk_command(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record):
|
|
text = get_cleaned_text(
|
|
text = get_cleaned_text(
|
|
update,
|
|
update,
|
|
bot,
|
|
bot,
|
|
@@ -217,7 +228,9 @@ async def _talk_command(bot, update, user_record):
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
-async def start_session(bot, other_user_record, admin_record):
|
|
|
|
|
|
+async def start_session(bot: Bot,
|
|
|
|
+ other_user_record,
|
|
|
|
+ admin_record):
|
|
"""Start talking session between user and admin.
|
|
"""Start talking session between user and admin.
|
|
|
|
|
|
Register session in database, so it gets loaded before message_loop starts.
|
|
Register session in database, so it gets loaded before message_loop starts.
|
|
@@ -280,7 +293,9 @@ async def start_session(bot, other_user_record, admin_record):
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-async def end_session(bot, other_user_record, admin_record):
|
|
|
|
|
|
+async def end_session(bot: Bot,
|
|
|
|
+ other_user_record,
|
|
|
|
+ admin_record):
|
|
"""End talking session between user and admin.
|
|
"""End talking session between user and admin.
|
|
|
|
|
|
Cancel session in database, so it will not be loaded anymore.
|
|
Cancel session in database, so it will not be loaded anymore.
|
|
@@ -316,7 +331,10 @@ async def end_session(bot, other_user_record, admin_record):
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-async def _talk_button(bot, update, user_record, data):
|
|
|
|
|
|
+async def _talk_button(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record,
|
|
|
|
+ data):
|
|
telegram_id = user_record['telegram_id']
|
|
telegram_id = user_record['telegram_id']
|
|
command, *arguments = data
|
|
command, *arguments = data
|
|
result, text, reply_markup = '', '', None
|
|
result, text, reply_markup = '', '', None
|
|
@@ -390,7 +408,9 @@ async def _talk_button(bot, update, user_record, data):
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
-async def _restart_command(bot, update, user_record):
|
|
|
|
|
|
+async def _restart_command(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record):
|
|
with bot.db as db:
|
|
with bot.db as db:
|
|
db['restart_messages'].insert(
|
|
db['restart_messages'].insert(
|
|
dict(
|
|
dict(
|
|
@@ -415,7 +435,9 @@ async def _restart_command(bot, update, user_record):
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-async def _stop_command(bot, update, user_record):
|
|
|
|
|
|
+async def _stop_command(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record):
|
|
text = bot.get_message(
|
|
text = bot.get_message(
|
|
'admin', 'stop_command', 'text',
|
|
'admin', 'stop_command', 'text',
|
|
update=update, user_record=user_record
|
|
update=update, user_record=user_record
|
|
@@ -448,14 +470,17 @@ async def _stop_command(bot, update, user_record):
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
-async def stop_bots(bot):
|
|
|
|
|
|
+async def stop_bots(bot: Bot):
|
|
"""Stop bots in `bot` class."""
|
|
"""Stop bots in `bot` class."""
|
|
await asyncio.sleep(2)
|
|
await asyncio.sleep(2)
|
|
bot.__class__.stop(message='=== STOP ===', final_state=0)
|
|
bot.__class__.stop(message='=== STOP ===', final_state=0)
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-async def _stop_button(bot, update, user_record, data):
|
|
|
|
|
|
+async def _stop_button(bot: Bot,
|
|
|
|
+ update,
|
|
|
|
+ user_record,
|
|
|
|
+ data: List[Union[int, str]]):
|
|
result, text, reply_markup = '', '', None
|
|
result, text, reply_markup = '', '', None
|
|
telegram_id = user_record['telegram_id']
|
|
telegram_id = user_record['telegram_id']
|
|
command = data[0] if len(data) > 0 else 'None'
|
|
command = data[0] if len(data) > 0 else 'None'
|
|
@@ -798,7 +823,7 @@ async def get_last_commit():
|
|
return last_commit
|
|
return last_commit
|
|
|
|
|
|
|
|
|
|
-async def _version_command(bot: davtelepot_bot, update, user_record):
|
|
|
|
|
|
+async def _version_command(bot: Bot, update, user_record):
|
|
last_commit = await get_last_commit()
|
|
last_commit = await get_last_commit()
|
|
text = bot.get_message(
|
|
text = bot.get_message(
|
|
'admin', 'version_command', 'header',
|
|
'admin', 'version_command', 'header',
|
|
@@ -813,7 +838,7 @@ async def _version_command(bot: davtelepot_bot, update, user_record):
|
|
return text
|
|
return text
|
|
|
|
|
|
|
|
|
|
-async def notify_new_version(bot: davtelepot_bot):
|
|
|
|
|
|
+async def notify_new_version(bot: Bot):
|
|
"""Notify `bot` administrators about new versions.
|
|
"""Notify `bot` administrators about new versions.
|
|
|
|
|
|
Notify admins when last commit and/or davtelepot version change.
|
|
Notify admins when last commit and/or davtelepot version change.
|
|
@@ -872,7 +897,7 @@ async def notify_new_version(bot: davtelepot_bot):
|
|
return
|
|
return
|
|
|
|
|
|
|
|
|
|
-async def get_package_updates(bot: davtelepot_bot,
|
|
|
|
|
|
+async def get_package_updates(bot: Bot,
|
|
monitoring_interval: Union[
|
|
monitoring_interval: Union[
|
|
int, datetime.timedelta
|
|
int, datetime.timedelta
|
|
] = 60 * 60,
|
|
] = 60 * 60,
|
|
@@ -945,10 +970,10 @@ async def get_package_updates(bot: davtelepot_bot,
|
|
await asyncio.sleep(monitoring_interval)
|
|
await asyncio.sleep(monitoring_interval)
|
|
|
|
|
|
|
|
|
|
-def init(telegram_bot,
|
|
|
|
- talk_messages=None,
|
|
|
|
- admin_messages=None,
|
|
|
|
- packages=None):
|
|
|
|
|
|
+def init(telegram_bot: Bot,
|
|
|
|
+ talk_messages: dict = None,
|
|
|
|
+ admin_messages: dict = None,
|
|
|
|
+ packages: List[types.ModuleType] = None):
|
|
"""Assign parsers, commands, buttons and queries to given `bot`."""
|
|
"""Assign parsers, commands, buttons and queries to given `bot`."""
|
|
if packages is None:
|
|
if packages is None:
|
|
packages = []
|
|
packages = []
|