-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotutils.py
More file actions
72 lines (54 loc) · 2.01 KB
/
botutils.py
File metadata and controls
72 lines (54 loc) · 2.01 KB
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
import io
import time
import requests
import botconfig
from scheduled_events import ScheduledEvent
import botstate
from telegram.helpers import escape_markdown
def print_to_string(*args, **kwargs):
output = io.StringIO()
print(*args, file=output, **kwargs)
contents = output.getvalue()
output.close()
return contents
def MD(text_input: str,version: int = 2) -> str:
"""Shortcut to escape MarkDown"""
return escape_markdown(text=text_input, version=version)
def S(text_input: str) -> str:
"""Strips punctuation and trailing/leading whitespace"""
t = text_input.maketrans("", "", ".,!:;\\/\"'?")
s = text_input.translate(t)
s = s.strip()
return s
def TU(usertag: str, userid: int) -> str:
"""Generates markup to tag a specific user with a specific text"""
return f" [{usertag}](tg://user?id={userid}) "
def md_safe_int(number: int) -> str:
if number < 0:
return "\\-" + str(number.__abs__())
return str(number)
def schedule_kill(chatid: int, msgid: int, expiration: float):
"""
Schedules a message to be deleted.
@param chatid: Chat ID where to delete the message.
@param msgid: Message ID to delete.
@param expiration: Time in seconds from current moment when the message is to be deleted.
@return:
"""
if expiration == -1:
return
expiration = expiration + time.time()
ScheduledEvent.schedule_event("msg_kill", chatid, expiration, msgid)
botstate.BotState.write()
print(f"scheduled to kill message {msgid}")
def cancel_kill(chatid: int, msgid: int):
"""
Cancels a scheduled message deletion.
@param chatid: Chat ID
@param msgid: Message ID to cancel deletion of.
@return:
"""
ScheduledEvent.fetch_events("msg_kill", 0,4_000_000_000,[(0, msgid)])
print(f"canceled message kill for {msgid}")
def kill_message(chatid: int, msgid: int) -> requests.Response:
return requests.get(f"https://api.telegram.org/bot{botconfig.bottoken}/deleteMessage?chat_id={chatid}&message_id={msgid}")