forked from Avishekbhattacharjee/Harita-Robot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsg_types.py
144 lines (117 loc) · 5.33 KB
/
msg_types.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from enum import IntEnum, unique
from telegram import Message
from tg_bot.modules.helper_funcs.string_handling import button_markdown_parser
@unique
class Types(IntEnum):
TEXT = 0
BUTTON_TEXT = 1
STICKER = 2
DOCUMENT = 3
PHOTO = 4
AUDIO = 5
VOICE = 6
VIDEO = 7
VIDEO_NOTE = 8
def get_note_type(msg: Message):
data_type = None
content = None
text = ""
raw_text = msg.text or msg.caption
args = raw_text.split(None, 2) # use python's maxsplit to separate cmd and args
note_name = args[1]
buttons = []
# determine what the contents of the filter are - text, image, sticker, etc
if len(args) >= 3:
offset = len(args[2]) - len(raw_text) # set correct offset relative to command + notename
text, buttons = button_markdown_parser(args[2], entities=msg.parse_entities() or msg.parse_caption_entities(),
offset=offset)
if buttons:
data_type = Types.BUTTON_TEXT
else:
data_type = Types.TEXT
elif msg.reply_to_message:
entities = msg.reply_to_message.parse_entities() or msg.reply_to_message.parse_caption_entities()
msgtext = msg.reply_to_message.text or msg.reply_to_message.caption
if len(args) >= 2 and msg.reply_to_message.text: # not caption, text
text, buttons = button_markdown_parser(msgtext,
entities=entities)
if buttons:
data_type = Types.BUTTON_TEXT
else:
data_type = Types.TEXT
elif msg.reply_to_message.sticker:
content = msg.reply_to_message.sticker.file_id
data_type = Types.STICKER
elif msg.reply_to_message.document:
content = msg.reply_to_message.document.file_id
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.DOCUMENT
elif msg.reply_to_message.photo:
content = msg.reply_to_message.photo[-1].file_id # last elem = best quality
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.PHOTO
elif msg.reply_to_message.audio:
content = msg.reply_to_message.audio.file_id
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.AUDIO
elif msg.reply_to_message.voice:
content = msg.reply_to_message.voice.file_id
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.VOICE
elif msg.reply_to_message.video:
content = msg.reply_to_message.video.file_id
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.VIDEO
elif msg.reply_to_message.video_note:
content = msg.reply_to_message.video_note.file_id
text, buttons = button_markdown_parser(msgtext, entities=entities)
data_type = Types.VIDEO_NOTE
return note_name, text, data_type, content, buttons
# note: add own args?
def get_welcome_type(msg: Message):
data_type = None
content = None
text = ""
args = msg.text.split(None, 1) # use python's maxsplit to separate cmd and args
buttons = []
# determine what the contents of the filter are - text, image, sticker, etc
# some media, cannot have captions in the Telegram BOT API
if len(args) >= 2 and not msg.reply_to_message:
offset = len(args[1]) - len(msg.text) # set correct offset relative to command + notename
text, buttons = button_markdown_parser(args[1], entities=msg.parse_entities(), offset=offset)
if buttons:
data_type = Types.BUTTON_TEXT
else:
data_type = Types.TEXT
elif msg.reply_to_message and msg.reply_to_message.sticker:
content = msg.reply_to_message.sticker.file_id
text = msg.reply_to_message.caption
data_type = Types.STICKER
elif msg.reply_to_message and msg.reply_to_message.document:
content = msg.reply_to_message.document.file_id
text = msg.reply_to_message.caption
data_type = Types.DOCUMENT
elif msg.reply_to_message and msg.reply_to_message.photo:
content = msg.reply_to_message.photo[-1].file_id # last elem = best quality
text = msg.reply_to_message.caption
data_type = Types.PHOTO
elif msg.reply_to_message and msg.reply_to_message.audio:
content = msg.reply_to_message.audio.file_id
text = msg.reply_to_message.caption
data_type = Types.AUDIO
elif msg.reply_to_message and msg.reply_to_message.voice:
content = msg.reply_to_message.voice.file_id
text = msg.reply_to_message.caption
data_type = Types.VOICE
elif msg.reply_to_message and msg.reply_to_message.video:
content = msg.reply_to_message.video.file_id
text = msg.reply_to_message.caption
data_type = Types.VIDEO
elif msg.reply_to_message.video_note:
msgtext = ""
if len(args) > 1:
msgtext = args[1]
content = msg.reply_to_message.video_note.file_id
text, buttons = button_markdown_parser(msgtext, entities=msg.reply_to_message.parse_caption_entities(), offset=0)
data_type = Types.VIDEO_NOTE
return text, data_type, content, buttons