-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_create.py
282 lines (257 loc) · 14.3 KB
/
task_create.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from firebase_admin import db
from telegram.ext import ConversationHandler
from functions import *
TITLE, TASK_TYPE, GROUP_ID, FLEXIBLE_INPUT, USER_TARGET, DAILY_TARGET, ADMINS_LIST, CONFIRM = range(8)
async def create_task(update, context):
message = update.message
if message.chat.type == 'group' or message.chat.type == 'supergroup':
return ConversationHandler.END
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
reply_keyboard = [["cancel"]]
await bot.send_message(chat_id=chat_id, text="Send the task title",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True,
one_time_keyboard=True),
reply_to_message_id=update.message.message_id)
return TITLE
async def title(update, context):
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if len(text) >= 5:
context.user_data["title"] = text
reply_keyboard = [["Filter", "Normal"], ["cancel"]]
await bot.send_message(chat_id=chat_id, text=f"Task title set to : {text}\n\n"
f"Select the task type",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True,
one_time_keyboard=True),
reply_to_message_id=update.message.message_id)
return TASK_TYPE
else:
await bot.send_message(chat_id=chat_id, text=f"Title too short , re-try",
reply_to_message_id=update.message.message_id)
return TITLE
async def task_type(update, context):
chat_id = update.message.chat_id
text = update.message.text
text = text.lower()
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
task_li = ["filter", "normal"]
if text in task_li:
context.user_data["task_type"] = text
reply_keyboard = [["cancel"]]
await bot.send_message(chat_id=chat_id, text=f"Chat type set to : {text} type\n\n"
f"Now send the group id",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True,
one_time_keyboard=True),
reply_to_message_id=update.message.message_id)
return GROUP_ID
else:
await bot.send_message(chat_id=chat_id, text=f"Type only can be {task_li}",
reply_to_message_id=update.message.message_id)
return TASK_TYPE
async def group_id(update, context):
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if not text.isnumeric():
await bot.send_message(chat_id=chat_id, text="Chat id must be number",
reply_to_message_id=update.message.message_id)
return GROUP_ID
try:
chat_info = await bot.get_chat(chat_id=int("-" + str(text)))
except:
try:
chat_info = await bot.get_chat(chat_id=int("-100" + str(text)))
except:
await bot.send_message(chat_id=chat_id,
text="Invalid group id , Add me in the group and send the proper group id")
return GROUP_ID
if not chat_info['permissions']['can_send_messages']:
await bot.send_message(chat_id=chat_id, text=f"I must need admin permission in : {chat_info['title']}")
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if chat_info.type not in ['group', 'supergroup']:
await bot.send_message(chat_id=chat_id, text="It is not a group or supergroup",
reply_to_message_id=update.message.message_id)
return GROUP_ID
context.user_data["group_type"] = chat_info.type
context.user_data["group_id"] = chat_info['id']
context.user_data["group_title"] = chat_info.title
flexible_text = "Now send the text link for filtering" if context.user_data[
"task_type"] == "filter" else "Now send the work group ID"
await bot.send_message(chat_id=chat_id, text=f"Promo group ID: {text}\n"
f"Chat type: {chat_info.type}\n"
f"Now send the {flexible_text}",
reply_to_message_id=update.message.message_id)
return FLEXIBLE_INPUT
async def flex_input(update, context):
chat_id = update.message.chat_id
text = update.message.text.lower()
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if context.user_data['task_type'] == "normal":
if not text.isnumeric():
await bot.send_message(chat_id=chat_id, text="Chat id must be number",
reply_to_message_id=update.message.message_id)
return FLEXIBLE_INPUT
try:
chat_info = await bot.get_chat(chat_id=int("-" + str(text)))
except:
try:
chat_info = await bot.get_chat(chat_id=int("-100" + str(text)))
except:
await bot.send_message(chat_id=chat_id,
text="Invalid group id , Add me in the group and send the proper group id")
return FLEXIBLE_INPUT
if not chat_info['permissions']['can_send_messages']:
await bot.send_message(chat_id=chat_id, text=f"I must need admin permission in : {chat_info['title']}")
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if chat_info.type not in ['group', 'supergroup']:
await bot.send_message(chat_id=chat_id, text="It is not a group or supergroup",
reply_to_message_id=update.message.message_id)
return FLEXIBLE_INPUT
context.user_data["work_group_type"] = chat_info.type
context.user_data["work_group_id"] = chat_info['id']
context.user_data["work_group_title"] = chat_info.title
flexible_text = "Now send the daily user tweet target"
await bot.send_message(chat_id=chat_id, text=f"Work group ID: {text}\n"
f"Chat type: {chat_info.type}\n"
f"{flexible_text}",
reply_to_message_id=update.message.message_id)
else:
if '.' in text or "any" in text:
context.user_data['filter_text'] = text
flex_text = "All the message will accepted from the promo group" if text == 'any' else f"Message will be filtered by text: {text}"
await bot.send_message(chat_id=chat_id, text=f"{flex_text}\n\n"
f"Now send the daily user target")
else:
await bot.send_message(chat_id=chat_id, text=f"Not a valid verification text\n"
f"Example: twitter.com , facebook.com\n"
f"Send 'any' to accept all messages")
return FLEXIBLE_INPUT
return USER_TARGET
async def user_target(update, context):
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if text.isdigit():
context.user_data["user_target"] = text
flexible_text = f"Telegram message daily user target set to : {text}" if context.user_data[
"task_type"] == "normal" else f"Daily user link target set to {text}"
flexible = "Now set the daily total link target" if context.user_data[
"task_type"] == "filter" else "Now send the daily " \
"message target"
await bot.send_message(chat_id=chat_id, text=f"{flexible_text}\n\n"
f"{flexible}",
reply_to_message_id=update.message.message_id)
return DAILY_TARGET
else:
await bot.send_message(chat_id=chat_id, text=f"Tweet limit only can be a number, re-try",
reply_to_message_id=update.message.message_id)
return USER_TARGET
async def daily_target(update, context):
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
if text.isdigit():
context.user_data["daily_target"] = text
flexible_text = f"Telegram message daily target set to : {text}" if context.user_data[
"task_type"] == "normal" else f"Daily link target set to : {text}"
await bot.send_message(chat_id=chat_id, text=f"{flexible_text}\n\n"
f"Now send admins telegram handle with comma separated\n"
f"NOTE: Don't include @",
reply_to_message_id=update.message.message_id)
return ADMINS_LIST
else:
await bot.send_message(chat_id=chat_id, text=f"Tweet limit only can be a number, re-try",
reply_to_message_id=update.message.message_id)
return DAILY_TARGET
async def admins_list(update, context):
chat_id = update.message.chat_id
text = update.message.text
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
await bot.send_message(chat_id=chat_id, text="Got it , Now please confirm the above details once again",
reply_to_message_id=update.message.message_id)
reply_keyboard = [["confirm", "cancel"]]
await bot.send_message(chat_id=chat_id, text=f"Title: {context.user_data['title']}\n"
f"Chat_id: {context.user_data['group_id']}\n"
f"Task type: {context.user_data['task_type']}\n"
f"User target: {context.user_data['user_target']}\n"
f"Daily target: {context.user_data['daily_target']}\n"
f"Admins: {text}",
reply_markup=ReplyKeyboardMarkup(reply_keyboard, resize_keyboard=True,
one_time_keyboard=True),
reply_to_message_id=update.message.message_id)
context.user_data["admins_list"] = text
return CONFIRM
async def confirm(update, context):
chat_id = update.message.chat_id
text = update.message.text.lower()
if text == "cancel":
await bot.send_message(chat_id=chat_id, text="Task creation Cancelled")
await menu_button(update, context)
return ConversationHandler.END
username = update.message.chat.username
if text == "confirm":
group_id = context.user_data["group_id"]
admins_list = context.user_data["admins_list"].replace(" ", "").replace('@', '').split(",")
# ref_id = db.reference('last_task').get() or {}
ref_id = db['last_task'].find_one({"_id": "stable"})
get_id = 0 if not ref_id else ref_id["task_id"]
tasks_col.insert_one({
'group_id': group_id,
'title': context.user_data['title'],
'task_id': get_id + 1,
'user_target': context.user_data['user_target'],
'daily_target': context.user_data['daily_target'],
'created_by': username,
'group_title': context.user_data['group_title'],
'group_type': context.user_data['group_type'],
'task_type': context.user_data['task_type'],
'admins_list': context.user_data['admins_list'],
'work_group': context.user_data['group_id'] if context.user_data['task_type'] == 'filter' else
context.user_data['work_group_id'],
'filter_text': context.user_data['filter_text'] if 'filter_text' in context.user_data else None,
'status': 'active'
})
if context.user_data['task_type'] == 'normal':
tasks_col.update_one({"group_id": group_id}, {"$set": {
'work_group_id': context.user_data['work_group_id'],
'work_group_title': context.user_data['work_group_title'],
'work_group_type': context.user_data['work_group_type']
}})
db['last_task'].update_one({"_id": 'stable'}, {"$set": {"task_id": get_id + 1}})
await bot.send_message(chat_id=chat_id, text=f"Task created! Task ID : {get_id + 1}")
await menu_button(update, context)
return ConversationHandler.END
else:
await bot.send_message(chat_id=chat_id, text="Please 'confirm' or 'cancel'")
return CONFIRM