-
Notifications
You must be signed in to change notification settings - Fork 0
/
Managepost.py
330 lines (239 loc) Β· 14.3 KB
/
Managepost.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# from User import User
import re
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update, ParseMode
import telegram
from telegram.ext import (
ConversationHandler,
CallbackContext,
)
from telegram.files.inputmedia import InputMediaPhoto
import globals
import subfile
from Makepost import checkForAngleBrackets
# MANAGE POSTS
# ----------------------------------------------------------
def manageposts(update: Update, context: CallbackContext) -> int:
text = subfile.get_userdict()[update.message.from_user.id].listMessages()
subfile.get_userdict()[update.message.from_user.id].setRequestedIndex(0)
if text != "":
update.message.reply_text("List of your submitted posts:\n" + text, parse_mode=ParseMode.HTML)
update.message.reply_text("Type the number of the post to manage. (or /cancel)", reply_markup=ReplyKeyboardRemove())
return globals.POSTTOMANAGE
else:
update.message.reply_text("You have no posts to manage. Press /start to return to the main menu.", reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
def checkpost(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
input = update.message.text
index = 0
if input != 'Return to post π':
index = int(re.sub("[^0-9]", "", input)) - 1
subfile.get_userdict()[userid].setRequestedIndex(index)
else:
index = subfile.get_userdict()[userid].requestedIndex
try:
# Create Message
message = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
update.message.reply_text("Your post is: ")
if subfile.get_userdict()[userid].messageList[index].hasphoto:
photoid = subfile.get_userdict()[userid].messageList[index].photoid
context.bot.send_photo(chat_id = update.effective_chat.id, photo = photoid, caption = message, parse_mode=ParseMode.HTML)
else:
update.message.reply_text(message, parse_mode=ParseMode.HTML)
if subfile.get_userdict()[userid].messageList[index].hasphoto:
reply_keyboard = [['Edit π'], ['Change Photo πΌοΈ'], ['Change Status βοΈβ'], ['Delete π'], ['Return to posts π']]
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text("What would you like to do? (Edit, Delete, Change Photo, Return to posts)", reply_markup = reply_markup)
else:
reply_keyboard = [['Edit π'], ['Delete π'], ['Change Status βοΈβ'], ['Return to posts π']]
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text("What would you like to do? (Edit, Delete, Return to posts)", reply_markup = reply_markup)
return globals.MANAGEPOST
except(IndexError):
update.message.reply_text("Post of that value not found. Type the number again, or /cancel.")
return globals.POSTTOMANAGE
#EDIT POST TEXT
#========================================================================
def getedittext(update: Update, context: CallbackContext) -> int:
update.message.reply_text("""<b>Type in anything you want to say in your post.</b>.
Example template would be:
<i>***Place of Stay***
Condition of item
Preferred meetup location (if applicable)
etc details </i>
<b>β οΈFor consistency purposes, we do not allow the title to be edited.β οΈ</b>""", parse_mode=ParseMode.HTML, reply_markup=ReplyKeyboardRemove())
return globals.EDITPREVIEW
def generateedittext(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
text = checkForAngleBrackets(update.message.text)
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
#Update text
subfile.get_userdict()[userid].messageList[index].set_text(text)
message = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
update.message.reply_text("Your post will be changed to: ")
if subfile.get_userdict()[userid].messageList[index].hasphoto:
photoid = subfile.get_userdict()[userid].messageList[index].photoid
context.bot.send_photo(chat_id = update.effective_chat.id, photo = photoid, caption = message, parse_mode=ParseMode.HTML)
else:
update.message.reply_text(message, parse_mode=ParseMode.HTML)
update.message.reply_text("Will this be ok? Type 'OK' (in caps) to confirm, or /cancel.")
return globals.EDIT
def editInChannel(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
msgid = subfile.get_userdict()[userid].messageList[index].id
edited = False
# Send message
message = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
try:
if subfile.get_userdict()[userid].messageList[index].hasphoto:
edited = context.bot.editMessageCaption(chat_id = globals.CHANNELID, message_id = msgid, caption = message, parse_mode=ParseMode.HTML)
else:
edited = context.bot.editMessageText(chat_id=globals.CHANNELID,
message_id = msgid,
text = message,
parse_mode=ParseMode.HTML)
except telegram.error.BadRequest:
update.message.reply_text("The message cannot be edited. /start to return to the main menu.")
return ConversationHandler.END
if (edited):
link = "https://t.me/c/" + str(globals.CHANNELLINKID) + "/" + str(msgid)
update.message.reply_text("""Edited! Thanks for using the channel! π
<b>View your post here: """ + link + "</b>" +
"""\n\n<b>Please remember to update your post once your transaction is complete.</b>
Hit /start to return to the main menu.""",
parse_mode=ParseMode.HTML)
else:
update.message.reply_text("Failed to edit your message. Your edited message may have been the same as the original, or that the post was redacted. /start to return to the main menu.")
return ConversationHandler.END
# PHOTO FUNCTIONALITY
#========================================================================
def editphoto(update: Update, context: CallbackContext) -> int:
update.message.reply_text("Send your new photo. (or /cancel)")
return globals.EDITPHOTO
def generatenewphoto(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
photoid = context.bot.getFile(update.message.photo[-1].file_id).file_id
subfile.get_userdict()[userid].messageList[index].set_photoid(photoid)
text = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
update.message.reply_text("Your post is: ")
if subfile.get_userdict()[userid].messageList[index].hasphoto:
photoid = subfile.get_userdict()[userid].messageList[index].photoid
context.bot.send_photo(chat_id = update.effective_chat.id, photo = photoid, caption = text, parse_mode=ParseMode.HTML)
else:
update.message.reply_text(text, parse_mode=ParseMode.HTML)
update.message.reply_text("Will this be ok? Type 'OK' (in caps) to confirm, or /cancel.")
return globals.EDITPHOTOPOST
def editPhotoInChannel(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
msgid = subfile.get_userdict()[userid].messageList[index].id
photoid = subfile.get_userdict()[userid].messageList[index].photoid
message = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
result = context.bot.editMessageMedia(chat_id = globals.CHANNELID, message_id = msgid, media = InputMediaPhoto(media = photoid))
result.photo = [result.photo[-1]]
edited = context.bot.editMessageCaption(chat_id = globals.CHANNELID, message_id = msgid, caption = message, parse_mode=ParseMode.HTML)
if (edited):
link = "https://t.me/c/" + str(globals.CHANNELLINKID) + "/" + str(msgid)
update.message.reply_text("""Edited! Thanks for using the channel! π
<b>View your post here: """ + link + "</b>" +
"""\n\n<b>Please remember to update the status of your post once your transaction is complete.</b>
Hit /start to return to the main menu.""",
parse_mode=ParseMode.HTML)
else:
update.message.reply_text("Failed to edit your message. /start to return to the main menu.")
return ConversationHandler.END
#DELETE PHOTO
#=========================================================================
def deletepostreason(update: Update, context: CallbackContext) -> int:
update.message.reply_text("""NOTE!! π¨<b>Telegram only allows the deletion of posts 48 hours within its time of submission!</b>
<b>Completed transactions should have their status marked as completed instead of being deleted.</b>
Please briefly state your reason for deletion. This will be recorded to help us better improve our channel :) (Or /cancel)""",
reply_markup=ReplyKeyboardRemove(), parse_mode=ParseMode.HTML)
return globals.DELETEPOSTCONFIRM
def deletepostconfirmation(update: Update, context: CallbackContext) -> int:
# Send reason for deletion to me (or a dedicated channel)
reason = update.message.text
context.bot.send_message(chat_id=globals.ADMINCHANNELID, text = "DELETION BY @" + update.message.from_user.username + ": " + reason)
update.message.reply_text("""ARE YOU SURE YOU WANT TO DELETE?
Type 'OK' (in caps) or /cancel""")
return globals.DELETEPOST
def deletepost(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
deleted = update.message.bot.deleteMessage(chat_id=globals.CHANNELID, message_id=subfile.get_userdict()[userid].messageList[index].id)
if deleted:
text = subfile.get_userdict()[userid].messageList[index].generateMessage(user.username)
context.bot.send_message(chat_id=globals.ADMINCHANNELID, text = "DELETION TEXT BY @" + update.message.from_user.username + ": \n" + text)
del subfile.get_userdict()[userid].messageList[index]
update.message.reply_text("Post successfully deleted π. /start to return to the main menu")
else:
update.message.reply_text("I failed to delete the message. The post was likely made more than 48 hours ago. You may prefer to edit the message instead. /start to return to the main menu.")
return ConversationHandler.END
# STATUS CHANGE
#=================================================================================
def statuschange(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
status = subfile.get_userdict()[userid].messageList[index].getstatus()
type = subfile.get_userdict()[userid].messageList[index].type
reply_keyboard = []
if status == "[Available]":
if type == 1:
reply_keyboard = [['My item is taken βοΈ'], ['Withdraw Post β'], ['Return to post π']]
elif type == 2:
reply_keyboard = [['It is currently loaned βοΈ'], ['Withdraw Post β'], ['Return to post π']]
elif status == "[Pending]":
reply_keyboard = [['I found my item βοΈ'], ['Withdraw Post β'], ['Return to post π']]
elif status == "[Completed]":
reply_keyboard = [['Put the post back up'], ['Return to post π']]
elif status == "[On Loan]":
reply_keyboard = [['The item has been returned βοΈ'], ['Return to post π']]
elif status == "[Post Redacted]":
reply_keyboard = [['Return to post π']]
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text("You have withdrawn this post. You cannot change its status. You may make a new post.", reply_markup = reply_markup)
return globals.CHANGESTATUS
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text("Change the status of your post.", reply_markup = reply_markup)
return globals.CHANGESTATUS
def updatestatus(update: Update, context: CallbackContext) -> int:
user = update.message.from_user
userid = user.id
index = subfile.get_userdict()[userid].requestedIndex
target = subfile.get_userdict()[userid].messageList[index]
msgid = target.id
action = update.message.text
if action == 'My item is taken βοΈ' or action == 'It is currently loaned βοΈ' or action == 'I found my item βοΈ':
target.changestatus(1)
subfile.counttransactions += 1
elif action == 'Put the post back up' or action == 'The item has been returned βοΈ':
target.changestatus(0)
elif action == 'Withdraw Post β':
target.changestatus(2)
message = target.generateMessage(user.username)
link = "https://t.me/c/" + str(globals.CHANNELLINKID) + "/" + str(msgid)
if subfile.get_userdict()[userid].messageList[index].hasphoto:
context.bot.editMessageCaption(chat_id = globals.CHANNELID, message_id = msgid, caption = message, parse_mode=ParseMode.HTML)
else:
context.bot.editMessageText(chat_id=globals.CHANNELID,
message_id = msgid,
text = message,
parse_mode=ParseMode.HTML
)
context.bot.send_message(chat_id=globals.ADMINCHANNELID, text = "STATUS UPDATE BY @" + user.username + ": " + message + "\n" + action, parse_mode=ParseMode.HTML)
reply_keyboard = [['Return to post π']]
reply_markup=ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True)
update.message.reply_text("""Status updated! π
<b>View your post here: """ + link + """</b>
""",
parse_mode=ParseMode.HTML, reply_markup = reply_markup)
return globals.CHANGESTATUS