Working on /talk command
This commit is contained in:
parent
86bab73216
commit
54e81f57df
@ -7,7 +7,7 @@ __author__ = "Davide Testa"
|
|||||||
__email__ = "davte@libero.it"
|
__email__ = "davte@libero.it"
|
||||||
__credits__ = "Marco Origlia"
|
__credits__ = "Marco Origlia"
|
||||||
__license__ = "GNU General Public License v3.0"
|
__license__ = "GNU General Public License v3.0"
|
||||||
__version__ = "1.4.8"
|
__version__ = "1.4.9"
|
||||||
__maintainer__ = "Davide Testa"
|
__maintainer__ = "Davide Testa"
|
||||||
__contact__ = "t.me/davte"
|
__contact__ = "t.me/davte"
|
||||||
|
|
||||||
|
@ -10,20 +10,28 @@ davtelepot.admin_tools.init(my_bot)
|
|||||||
|
|
||||||
# Third party modules
|
# Third party modules
|
||||||
from davteutil.utilities import (
|
from davteutil.utilities import (
|
||||||
get_cleaned_text, get_user, escape_html_chars, extract,
|
async_wrapper, get_cleaned_text, get_user, escape_html_chars, extract,
|
||||||
line_drawing_unordered_list, make_button, make_inline_keyboard,
|
line_drawing_unordered_list, make_button, make_inline_keyboard,
|
||||||
remove_html_tags
|
remove_html_tags
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
TALK_MESSAGES = dict(
|
TALK_MESSAGES = dict(
|
||||||
confirm_user_button=dict(
|
admin_warning=dict(
|
||||||
en='Talk to {u}?',
|
en=(
|
||||||
it='Parla con {u}?'
|
'You are now talking to {u}.\n'
|
||||||
|
'Until you end this session, your messages will be '
|
||||||
|
'forwarded to each other.'
|
||||||
|
),
|
||||||
|
it={
|
||||||
|
'Sei ora connesso con {u}.\n'
|
||||||
|
'Finché non chiuderai la connessione, i messaggi che scriverai '
|
||||||
|
'qui saranno inoltrati a {u}, e ti inoltrerò i suoi.'
|
||||||
|
},
|
||||||
),
|
),
|
||||||
confirm_user_text=dict(
|
help_text=dict(
|
||||||
en='Do you want to talk to {u}?',
|
en='Press the button to search for user.',
|
||||||
it='Vuoi parlare con {u}?'
|
it='Premi il pulsante per scegliere un utente.'
|
||||||
),
|
),
|
||||||
search_button=dict(
|
search_button=dict(
|
||||||
en="🔍 Search for user",
|
en="🔍 Search for user",
|
||||||
@ -43,13 +51,72 @@ TALK_MESSAGES = dict(
|
|||||||
"<code>{q}</code>"
|
"<code>{q}</code>"
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
instructions=dict(
|
||||||
|
en=(
|
||||||
|
'Write a part of name, surname or username of the user you want '
|
||||||
|
'to talk to.'
|
||||||
|
),
|
||||||
|
it=(
|
||||||
|
'Scrivi una parte del nome, cognome o username dell\'utente con '
|
||||||
|
'cui vuoi parlare.'
|
||||||
|
),
|
||||||
|
),
|
||||||
|
stop=dict(
|
||||||
|
en=(
|
||||||
|
'End session'
|
||||||
|
),
|
||||||
|
it={
|
||||||
|
'Termina la sessione'
|
||||||
|
},
|
||||||
|
),
|
||||||
|
user_warning=dict(
|
||||||
|
en=(
|
||||||
|
'{u}, admin of this bot, wants to talk to you.\n'
|
||||||
|
'Until this session is ended by {u}, your messages will be '
|
||||||
|
'forwarded to each other.'
|
||||||
|
),
|
||||||
|
it={
|
||||||
|
'{u}, amministratore di questo bot, vuole parlare con te.\n'
|
||||||
|
'Finché non chiuderà la connessione, i messaggi che scriverai '
|
||||||
|
'qui saranno inoltrati a {u}, e ti inoltrerò i suoi.'
|
||||||
|
},
|
||||||
|
),
|
||||||
# key=dict(
|
# key=dict(
|
||||||
# en='',
|
# en='',
|
||||||
# it='',
|
# it='',
|
||||||
# ),
|
# ),
|
||||||
|
# key=dict(
|
||||||
|
# en=(
|
||||||
|
# ''
|
||||||
|
# ),
|
||||||
|
# it={
|
||||||
|
# ''
|
||||||
|
# },
|
||||||
|
# ),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
bot.set_custom_parser(
|
||||||
|
async_wrapper(
|
||||||
|
_forward_to,
|
||||||
|
bot=bot,
|
||||||
|
sender=sender,
|
||||||
|
addressee=addressee,
|
||||||
|
is_admin=is_admin
|
||||||
|
),
|
||||||
|
sender
|
||||||
|
)
|
||||||
|
await bot.forward_message(
|
||||||
|
chat_id=addressee,
|
||||||
|
update=update
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
def get_talk_panel(text, bot, update):
|
def get_talk_panel(text, bot, update):
|
||||||
"""Return text and reply markup of talk panel.
|
"""Return text and reply markup of talk panel.
|
||||||
|
|
||||||
@ -76,14 +143,30 @@ def get_talk_panel(text, bot, update):
|
|||||||
last_name,
|
last_name,
|
||||||
first_name
|
first_name
|
||||||
) LIKE '%{username}%'
|
) LIKE '%{username}%'
|
||||||
|
ORDER BY LOWER(
|
||||||
|
COALESCE(
|
||||||
|
first_name || last_name || username,
|
||||||
|
last_name || username,
|
||||||
|
first_name || username,
|
||||||
|
username,
|
||||||
|
first_name || last_name,
|
||||||
|
last_name,
|
||||||
|
first_name
|
||||||
|
)
|
||||||
|
)
|
||||||
|
LIMIT 26
|
||||||
""".format(
|
""".format(
|
||||||
username=text
|
username=text
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if len(users) == 0:
|
if len(text) == 0:
|
||||||
text = (
|
text = (
|
||||||
bot.get_message('talk', 'user_not_found', update=update)
|
bot.get_message(
|
||||||
|
'talk',
|
||||||
|
'help_text',
|
||||||
|
update=update
|
||||||
|
)
|
||||||
).format(
|
).format(
|
||||||
q=escape_html_chars(
|
q=escape_html_chars(
|
||||||
remove_html_tags(text)
|
remove_html_tags(text)
|
||||||
@ -102,33 +185,33 @@ def get_talk_panel(text, bot, update):
|
|||||||
],
|
],
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
elif len(users) == 1:
|
elif len(users) == 0:
|
||||||
user = users[0]
|
|
||||||
text = (
|
text = (
|
||||||
bot.get_message(
|
bot.get_message(
|
||||||
'talk', 'confirm_user_text',
|
'talk',
|
||||||
|
'user_not_found',
|
||||||
update=update
|
update=update
|
||||||
)
|
)
|
||||||
).format(
|
).format(
|
||||||
u=get_user(user)
|
q=escape_html_chars(
|
||||||
|
remove_html_tags(text)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
reply_markup = make_inline_keyboard(
|
reply_markup = make_inline_keyboard(
|
||||||
[
|
[
|
||||||
make_button(
|
make_button(
|
||||||
(
|
bot.get_message(
|
||||||
bot.get_message(
|
'talk', 'search_button',
|
||||||
'talk', 'confirm_user_button',
|
update=update
|
||||||
update=update
|
),
|
||||||
)
|
prefix='talk:///',
|
||||||
).format(
|
data=['search']
|
||||||
u=get_user(user)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
1
|
1
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
text = "{header}\n\n{u}".format(
|
text = "{header}\n\n{u}{etc}".format(
|
||||||
header=bot.get_message(
|
header=bot.get_message(
|
||||||
'talk', 'select_user',
|
'talk', 'select_user',
|
||||||
update=update
|
update=update
|
||||||
@ -136,8 +219,13 @@ def get_talk_panel(text, bot, update):
|
|||||||
u=line_drawing_unordered_list(
|
u=line_drawing_unordered_list(
|
||||||
[
|
[
|
||||||
get_user(user)
|
get_user(user)
|
||||||
for user in users
|
for user in users[:25]
|
||||||
]
|
]
|
||||||
|
),
|
||||||
|
etc=(
|
||||||
|
'\n\n[...]'
|
||||||
|
if len(users) > 25
|
||||||
|
else ''
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
reply_markup = make_inline_keyboard(
|
reply_markup = make_inline_keyboard(
|
||||||
@ -162,7 +250,7 @@ def get_talk_panel(text, bot, update):
|
|||||||
user['id']
|
user['id']
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
for user in users
|
for user in users[:25]
|
||||||
],
|
],
|
||||||
2
|
2
|
||||||
)
|
)
|
||||||
@ -176,13 +264,94 @@ async def _talk_command(update, bot):
|
|||||||
['talk']
|
['talk']
|
||||||
)
|
)
|
||||||
text, reply_markup = get_talk_panel(text, bot, update)
|
text, reply_markup = get_talk_panel(text, bot, update)
|
||||||
return
|
return dict(
|
||||||
|
text=text,
|
||||||
|
parse_mode='HTML',
|
||||||
|
reply_markup=reply_markup,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _talk_button(update, bot):
|
async def _talk_button(update, bot):
|
||||||
telegram_id = update['from']['id']
|
telegram_id = update['from']['id']
|
||||||
command, *arguments = extract(update['data'], '///').split('|')
|
command, *arguments = extract(update['data'], '///').split('|')
|
||||||
result, text, reply_markup = '', '', None
|
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:
|
||||||
|
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'],
|
||||||
|
admin=admin_record['id'],
|
||||||
|
cancelled=0
|
||||||
|
)
|
||||||
|
)
|
||||||
|
await bot.send_message(
|
||||||
|
chat_id=user_record['telegram_id'],
|
||||||
|
text=bot.get_message(
|
||||||
|
'talk', 'user_warning',
|
||||||
|
update=update
|
||||||
|
)
|
||||||
|
)
|
||||||
|
await bot.send_message(
|
||||||
|
chat_id=admin_record['telegram_id'],
|
||||||
|
text=bot.get_message(
|
||||||
|
'talk', 'admin_warning',
|
||||||
|
update=update
|
||||||
|
),
|
||||||
|
reply_markup=make_inline_keyboard(
|
||||||
|
[
|
||||||
|
make_button(
|
||||||
|
bot.get_message(
|
||||||
|
'talk', 'stop',
|
||||||
|
update=update
|
||||||
|
),
|
||||||
|
prefix='talk:///',
|
||||||
|
data=['stop']
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
bot.set_custom_parser(
|
||||||
|
await async_wrapper(
|
||||||
|
_forward_to,
|
||||||
|
bot=bot
|
||||||
|
),
|
||||||
|
user_telegram_id=user_record['telegram_id'],
|
||||||
|
admin_telegram_id=admin_record['telegram_id'],
|
||||||
|
)
|
||||||
|
elif command == 'stop':
|
||||||
|
with bot.db as db:
|
||||||
|
admin_record = db['users'].find_one(
|
||||||
|
telegram_id=telegram_id
|
||||||
|
)
|
||||||
|
db['talking_sessions'].update(
|
||||||
|
dict(
|
||||||
|
admin=admin_record['id'],
|
||||||
|
cancelled=1
|
||||||
|
),
|
||||||
|
['admin']
|
||||||
|
)
|
||||||
if text:
|
if text:
|
||||||
return dict(
|
return dict(
|
||||||
text=result,
|
text=result,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user