Talking sessions are now ready.

This commit is contained in:
Davte 2018-11-26 17:32:21 +01:00
parent 328258d2af
commit 2ab0712036
2 changed files with 194 additions and 71 deletions

View File

@ -7,7 +7,7 @@ __author__ = "Davide Testa"
__email__ = "davte@libero.it"
__credits__ = "Marco Origlia"
__license__ = "GNU General Public License v3.0"
__version__ = "1.4.15"
__version__ = "1.4.16"
__maintainer__ = "Davide Testa"
__contact__ = "t.me/davte"

View File

@ -10,13 +10,21 @@ davtelepot.admin_tools.init(my_bot)
# Third party modules
from davteutil.utilities import (
async_wrapper, get_cleaned_text, get_user, escape_html_chars, extract,
line_drawing_unordered_list, make_button, make_inline_keyboard,
async_wrapper, Confirmator, get_cleaned_text, get_user, escape_html_chars,
extract, line_drawing_unordered_list, make_button, make_inline_keyboard,
remove_html_tags
)
TALK_MESSAGES = dict(
admin_session_ended=dict(
en=(
'Session with user {u} ended.'
),
it={
'Sessione terminata con l\'utente {u}.'
},
),
admin_warning=dict(
en=(
'You are now talking to {u}.\n'
@ -29,6 +37,14 @@ TALK_MESSAGES = dict(
'qui saranno inoltrati a {u}, e ti inoltrerò i suoi.'
),
),
end_session=dict(
en=(
'End session?'
),
it=(
'Chiudere la sessione?'
),
),
help_text=dict(
en='Press the button to search for user.',
it='Premi il pulsante per scegliere un utente.'
@ -69,6 +85,14 @@ TALK_MESSAGES = dict(
'Termina la sessione'
},
),
user_session_ended=dict(
en=(
'Session with admin {u} ended.'
),
it={
'Sessione terminata con l\'amministratore {u}.'
},
),
user_warning=dict(
en=(
'{u}, admin of this bot, wants to talk to you.\n'
@ -98,7 +122,22 @@ TALK_MESSAGES = dict(
async def _forward_to(update, bot, sender, addressee, is_admin=False):
if update['text'].lower() in ['stop'] and is_admin:
pass # Remove custom parser to sender and addressee
with bot.db as db:
admin_record = db['users'].find_one(
telegram_id=sender
)
session_record = db['talking_sessions'].find_one(
admin=admin_record['id'],
cancelled=0
)
user_record = db['users'].find_one(
id=session_record['user']
)
await end_session(
bot=bot,
user_record=user_record,
admin_record=admin_record
)
else:
bot.set_custom_parser(
await async_wrapper(
@ -269,34 +308,13 @@ async def _talk_command(update, bot):
)
async def _talk_button(update, bot):
telegram_id = update['from']['id']
command, *arguments = extract(update['data'], '///').split('|')
result, text, reply_markup = '', '', None
if command == 'search':
bot.set_custom_parser(
await async_wrapper(
_talk_command,
bot=bot
),
update
)
text = bot.get_message(
'talk', 'instructions',
update=update
)
reply_markup = None
elif command == 'select':
if len(arguments) < 1:
result = "Errore!"
else:
async def start_session(bot, user_record, admin_record):
"""Start talking session between user and admin.
Register session in database, so it gets loaded before message_loop starts.
Send a notification both to admin and user, set custom parsers and return.
"""
with bot.db as db:
user_record = db['users'].find_one(
id=int(arguments[0])
)
admin_record = db['users'].find_one(
telegram_id=telegram_id
)
db['talking_sessions'].insert(
dict(
user=user_record['id'],
@ -308,7 +326,7 @@ async def _talk_button(update, bot):
chat_id=user_record['telegram_id'],
text=bot.get_message(
'talk', 'user_warning',
update=update,
user_record=user_record,
u=get_user(admin_record)
)
)
@ -316,7 +334,7 @@ async def _talk_button(update, bot):
chat_id=admin_record['telegram_id'],
text=bot.get_message(
'talk', 'admin_warning',
update=update,
user_record=admin_record,
u=get_user(user_record)
),
reply_markup=make_inline_keyboard(
@ -324,10 +342,10 @@ async def _talk_button(update, bot):
make_button(
bot.get_message(
'talk', 'stop',
update=update
user_record=admin_record
),
prefix='talk:///',
data=['stop']
data=['stop', user_record['id']]
)
]
)
@ -352,11 +370,17 @@ async def _talk_button(update, bot):
),
admin_record['telegram_id']
)
elif command == 'stop':
return
async def end_session(bot, user_record, admin_record):
"""End talking session between user and admin.
Cancel session in database, so it will not be loaded anymore.
Send a notification both to admin and user, clear custom parsers
and return.
"""
with bot.db as db:
admin_record = db['users'].find_one(
telegram_id=telegram_id
)
db['talking_sessions'].update(
dict(
admin=admin_record['id'],
@ -364,8 +388,91 @@ async def _talk_button(update, bot):
),
['admin']
)
# TODO: message which says it has stopped
# TODO: register talking session in database
await bot.send_message(
chat_id=user_record['telegram_id'],
text=bot.get_message(
'talk', 'user_session_ended',
user_record=user_record,
u=get_user(admin_record)
)
)
await bot.send_message(
chat_id=admin_record['telegram_id'],
text=bot.get_message(
'talk', 'admin_session_ended',
user_record=admin_record,
u=get_user(user_record)
),
)
for record in (admin_record, user_record, ):
telegram_id = record['telegram_id']
if telegram_id in bot.custom_parsers:
del bot.custom_parsers[telegram_id]
return
async def _talk_button(update, bot):
telegram_id = update['from']['id']
command, *arguments = extract(update['data'], '///').split('|')
result, text, reply_markup = '', '', None
if command == 'search':
bot.set_custom_parser(
await async_wrapper(
_talk_command,
bot=bot
),
update
)
text = bot.get_message(
'talk', 'instructions',
update=update
)
reply_markup = None
elif command == 'select':
if (
len(arguments) < 1
or not arguments[0].isnumeric()
):
result = "Errore!"
else:
with bot.db as db:
user_record = db['users'].find_one(
id=int(arguments[0])
)
admin_record = db['users'].find_one(
telegram_id=telegram_id
)
await start_session(
bot,
user_record=user_record,
admin_record=admin_record
)
elif command == 'stop':
if (
len(arguments) < 1
or not arguments[0].isnumeric()
):
result = "Errore!"
elif not Confirmator.get('stop_bots').confirm(telegram_id):
result = bot.get_message(
'talk', 'end_session',
update=update,
)
else:
with bot.db as db:
user_record = db['users'].find_one(
id=int(arguments[0])
)
admin_record = db['users'].find_one(
telegram_id=telegram_id
)
await end_session(
bot,
user_record=user_record,
admin_record=admin_record
)
text = "Session ended."
reply_markup = None
if text:
return dict(
text=result,
@ -394,6 +501,7 @@ def init(bot):
@bot.additional_task(when='BEFORE')
async def load_talking_sessions():
sessions = []
with bot.db as db:
for session in db.query(
"""SELECT *
@ -401,7 +509,22 @@ def init(bot):
WHERE NOT cancelled
"""
):
pass # Set cutom_parser
sessions.append(
dict(
user_record=db['users'].find_one(
id=session['user']
),
admin_record=db['users'].find_one(
id=session['admin']
),
)
)
for session in sessions:
await start_session(
bot=bot,
user_record=session['user_record'],
admin_record=session['admin_record']
)
@bot.command(command='/talk', aliases=[], show_in_keyboard=False,
descr="Choose a user and forward messages to each other.",