-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForwarder.py
135 lines (110 loc) · 6.01 KB
/
Forwarder.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
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, CallbackQueryHandler, ConversationHandler, Filters
def start(update, context):
if 'channels' not in context.user_data:
context.bot.send_message(chat_id=update.message.chat_id, text="Hi there, please send me a list of usernames of channels that the bot is a admin of it separated by comma")
return SELECT_CHANNEL
else:
buttons = []
buttons.append([InlineKeyboardButton("Send to All Channels", callback_data="all")])
for channel in context.user_data['channels']:
buttons.append([InlineKeyboardButton(channel, callback_data=channel)])
reply_markup = InlineKeyboardMarkup(buttons)
context.bot.send_message(chat_id=update.message.chat_id, text="Please select a channel:", reply_markup=reply_markup)
return MESSAGE
def select_channel(update, context):
channels = update.message.text.split(",")
channels = [x.strip() for x in channels]
context.user_data['channels'] = channels
buttons = []
for channel in channels:
buttons.append([InlineKeyboardButton(channel, callback_data=channel)])
reply_markup = InlineKeyboardMarkup(buttons)
context.bot.send_message(chat_id=update.message.chat_id, text="Please select a channel:", reply_markup=reply_markup)
return MESSAGE
def add_channel(update, context):
new_channel = update.message.text.split()[1]
if 'channels' not in context.user_data:
context.user_data['channels'] = []
if new_channel in context.user_data['channels']:
context.bot.send_message(chat_id=update.message.chat_id, text="Channel '{}' already exists".format(new_channel))
else:
chat_member = context.bot.get_chat_member(new_channel, context.bot.id)
if chat_member.status in ['administrator', 'creator']:
context.user_data['channels'].append(new_channel)
context.bot.send_message(chat_id=update.message.chat_id, text="Channel '{}' added successfully".format(new_channel))
else:
context.bot.send_message(chat_id=update.message.chat_id, text="The bot is not an admin in channel '{}'".format(new_channel))
def remove_channel(update, context):
channel_to_remove = update.message.text.split()[1]
if 'channels' in context.user_data and channel_to_remove in context.user_data['channels']:
context.user_data['channels'].remove(channel_to_remove)
context.bot.send_message(chat_id=update.message.chat_id, text="Channel '{}' removed successfully".format(channel_to_remove))
else:
context.bot.send_message(chat_id=update.message.chat_id, text="Channel '{}' not found in list of channels".format(channel_to_remove))
def message(update, context):
query = update.callback_query
if context.user_data.get("selected_channel") == "all":
for channel in context.user_data['channels']:
if update.message.photo:
context.bot.send_photo(chat_id=channel, photo=update.message.photo[-1].file_id, caption=update.message.caption)
elif update.message.document:
context.bot.send_document(chat_id=channel, document=update.message.document.file_id, caption=update.message.caption)
elif update.message.video:
context.bot.send_video(chat_id=channel, video=update.message.video.file_id, caption=update.message.caption)
else:
context.bot.send_message(chat_id=channel, text=update.message.text)
else:
selected_channel = context.user_data["selected_channel"]
if update.message.photo:
context.bot.send_photo(chat_id=selected_channel, photo=update.message.photo[-1].file_id, caption=update.message.caption)
elif update.message.document:
context.bot.send_document(chat_id=selected_channel, document=update.message.document.file_id, caption=update.message.caption)
elif update.message.video:
context.bot.send_video(chat_id=selected_channel, video=update.message.video.file_id, caption=update.message.caption)
else:
context.bot.send_message(chat_id=selected_channel, text=update.message.text)
return ConversationHandler.END
def button(update, context):
query = update.callback_query
selected_channel = query.data
context.user_data["selected_channel"] = selected_channel
context.bot.send_message(chat_id=query.message.chat_id, text="Selected channel: {} , please send a message to it.".format(selected_channel))
return MESSAGE
def cancel(update, context):
context.bot.send_message(chat_id=update.message.chat_id, text="Conversation cancelled.")
return ConversationHandler.END
def help_command(update, context):
context.bot.send_message(chat_id=update.message.chat_id, text="""
Available commands:
- /start: Start the bot
- /add <channel_username>: Add a channel
- /remove <channel_username>: Remove a channel
- /help: Show this help message
""")
def error(update, context):
context.bot.send_message(chat_id=update.message.chat_id, text="An error has occurred.")
SELECT_CHANNEL, MESSAGE = range(2)
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
SELECT_CHANNEL: [MessageHandler(Filters.text, select_channel)],
MESSAGE: [MessageHandler(Filters.text | Filters.document.mime_type("image/jpeg/mp4/mkv") | Filters.photo | Filters.video, message),
CallbackQueryHandler(button, pattern='^.*$')],
},
fallbacks=[CommandHandler("cancel", cancel)],
allow_reentry=True,
per_user=True,
per_chat=True,
)
def main():
updater = Updater(token="YOUR_BOT_TOKEN", use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(conv_handler)
dispatcher.add_handler(CommandHandler("add", add_channel))
dispatcher.add_handler(CommandHandler("remove", remove_channel))
dispatcher.add_handler(CommandHandler("help", help_command))
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()