Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telegram import InlineKeyboardButton, InlineKeyboardMarkup
- from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
- TOKEN = "6077037275:AAH-Bmwj-ym2onsPEwUDTSF8yNgdvU3NnSQ"
- delay = 60*60 # 1 час в секундах
- num_messages = 5
- channel_name = '@daqwuy'
- chat_names = ['@fbfrr', '@dahjjkj', '@awrghj']
- import time
- from telegram import ParseMode
- from telegram.error import TelegramError
- def send_channel_messages_to_groups(channel_name, chat_names, num_messages=5, delay=60):
- bot = telegram.Bot(TOKEN)
- # Get the channel ID from the channel name
- channel_id = bot.get_chat(channel_name).id
- # Loop through each chat and send messages from the channel
- for chat_name in chat_names:
- try:
- # Get the chat ID from the chat name
- chat_id = bot.get_chat(chat_name).id
- # Initialize a message counter and delayed message counter
- message_counter = 0
- delayed_message_counter = 0
- # Loop through the messages in the channel
- for message in bot.get_chat_messages(chat_id=channel_id):
- # If the message is not a media message, send it to the chat
- if not message.media:
- bot.send_message(chat_id=chat_id, text=message.text, parse_mode=ParseMode.HTML)
- # Increment the message counter and delayed message counter
- message_counter += 1
- delayed_message_counter += 1
- # If we have reached the message limit or the delayed message limit, pause for the delay time
- if message_counter == num_messages or delayed_message_counter == 5:
- time.sleep(delay)
- delayed_message_counter = 0
- # Send a final message indicating that all messages have been sent
- bot.send_message(chat_id=chat_id, text="All messages from the channel have been sent to this group.")
- # Catch any errors and print them to the console
- except TelegramError as e:
- print(f"Error sending messages to chat {chat_name}: {e}")
- ###################################################################################
- def start(update, context):
- keyboard = [
- [InlineKeyboardButton("Количество сообщений", callback_data='num_messages')],
- [InlineKeyboardButton("Задержка между сообщениями", callback_data='delay')]
- ]
- reply_markup = InlineKeyboardMarkup(keyboard)
- update.message.reply_text('Привет! Выбери, что ты хочешь изменить:', reply_markup=reply_markup)
- def button(update, context):
- query = update.callback_query
- if query.data == 'num_messages':
- query.answer()
- query.message.reply_text(f"Текущее количество сообщений: {num_messages}")
- query.message.reply_text("Введите новое количество сообщений (от 1 до 100):")
- elif query.data == 'delay':
- query.answer()
- query.message.reply_text(f"Текущая задержка между сообщениями: {delay/60} минут.")
- query.message.reply_text("Введите новую задержку между сообщениями (в формате чч:мм):")
- def set_num_messages(update, context):
- new_num_messages = int(update.message.text)
- global num_messages
- num_messages = new_num_messages
- update.message.reply_text(f"Количество сообщений сохранено: {num_messages}")
- def set_delay(update, context):
- new_delay = update.message.text
- try:
- hours, minutes = map(int, new_delay.split(':'))
- if hours not in range(24) or minutes not in range(60):
- raise ValueError
- global delay
- delay = (hours*60 + minutes)*60
- update.message.reply_text(f"Задержка между сообщениями сохранена: {hours} часов {minutes} минут.")
- except ValueError:
- update.message.reply_text("Неправильный формат времени. Введите время в формате чч:мм (от 00:00 до 23:59).")
- updater = Updater(TOKEN, use_context=True)
- dispatcher = updater.dispatcher
- dispatcher.add_handler(CommandHandler('start', start))
- dispatcher.add_handler(CallbackQueryHandler(button))
- dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command & Filters.regex('^\d+$'), set_num_messages))
- dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command & Filters.regex('^\d{2}:\d{2}$'), set_delay))
- updater.start_polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement