-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_utils.py
90 lines (74 loc) · 3.11 KB
/
message_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from telegram.message import Message
from telegram.update import Update
import time
from bot import AUTO_DELETE_MESSAGE_DURATION, LOGGER, bot, \
status_reply_dict, status_reply_dict_lock
from bot.helper.ext_utils.bot_utils import get_readable_message
from telegram.error import TimedOut, BadRequest
from bot import bot
def sendMessage(text: str, bot, update: Update):
try:
return bot.send_message(update.message.chat_id,
reply_to_message_id=update.message.message_id,
text=text, parse_mode='HTMl')
except Exception as e:
LOGGER.error(str(e))
def editMessage(text: str, message: Message):
try:
bot.edit_message_text(text=text, message_id=message.message_id,
chat_id=message.chat.id,
parse_mode='HTMl')
except Exception as e:
LOGGER.error(str(e))
def deleteMessage(bot, message: Message):
try:
bot.delete_message(chat_id=message.chat.id,
message_id=message.message_id)
except Exception as e:
LOGGER.error(str(e))
def sendLogFile(bot, update: Update):
with open('log.txt', 'rb') as f:
bot.send_document(document=f, filename=f.name,
reply_to_message_id=update.message.message_id,
chat_id=update.message.chat_id)
def auto_delete_message(bot, cmd_message: Message, bot_message: Message):
if AUTO_DELETE_MESSAGE_DURATION != -1:
time.sleep(AUTO_DELETE_MESSAGE_DURATION)
try:
# Skip if None is passed meaning we don't want to delete bot xor cmd message
deleteMessage(bot, cmd_message)
deleteMessage(bot, bot_message)
except AttributeError:
pass
def delete_all_messages():
with status_reply_dict_lock:
for message in list(status_reply_dict.values()):
try:
deleteMessage(bot, message)
del status_reply_dict[message.chat.id]
except Exception as e:
LOGGER.error(str(e))
def update_all_messages():
msg = get_readable_message()
with status_reply_dict_lock:
for chat_id in list(status_reply_dict.keys()):
if status_reply_dict[chat_id] and msg != status_reply_dict[chat_id].text:
try:
editMessage(msg, status_reply_dict[chat_id])
except Exception as e:
LOGGER.error(str(e))
status_reply_dict[chat_id].text = msg
def sendStatusMessage(msg, bot):
progress = get_readable_message()
with status_reply_dict_lock:
if msg.message.chat.id in list(status_reply_dict.keys()):
try:
message = status_reply_dict[msg.message.chat.id]
deleteMessage(bot, message)
del status_reply_dict[msg.message.chat.id]
except Exception as e:
LOGGER.error(str(e))
del status_reply_dict[msg.message.chat.id]
pass
message = sendMessage(progress, bot, msg)
status_reply_dict[msg.message.chat.id] = message