Advertisement
Guest User

Untitled

a guest
Mar 6th, 2023
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. from telegram import InlineKeyboardButton, InlineKeyboardMarkup
  2. from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
  3.  
  4. TOKEN = "6077037275:AAH-Bmwj-ym2onsPEwUDTSF8yNgdvU3NnSQ"
  5. delay = 60*60 # 1 час в секундах
  6. num_messages = 5
  7.  
  8. channel_name = '@daqwuy'
  9. chat_names = ['@fbfrr', '@dahjjkj', '@awrghj']
  10.  
  11. import time
  12. from telegram import ParseMode
  13. from telegram.error import TelegramError
  14.  
  15. def send_channel_messages_to_groups(channel_name, chat_names, num_messages=5, delay=60):
  16. bot = telegram.Bot(TOKEN)
  17. # Get the channel ID from the channel name
  18. channel_id = bot.get_chat(channel_name).id
  19.  
  20. # Loop through each chat and send messages from the channel
  21. for chat_name in chat_names:
  22. try:
  23. # Get the chat ID from the chat name
  24. chat_id = bot.get_chat(chat_name).id
  25.  
  26. # Initialize a message counter and delayed message counter
  27. message_counter = 0
  28. delayed_message_counter = 0
  29.  
  30. # Loop through the messages in the channel
  31. for message in bot.get_chat_messages(chat_id=channel_id):
  32. # If the message is not a media message, send it to the chat
  33. if not message.media:
  34. bot.send_message(chat_id=chat_id, text=message.text, parse_mode=ParseMode.HTML)
  35.  
  36. # Increment the message counter and delayed message counter
  37. message_counter += 1
  38. delayed_message_counter += 1
  39.  
  40. # If we have reached the message limit or the delayed message limit, pause for the delay time
  41. if message_counter == num_messages or delayed_message_counter == 5:
  42. time.sleep(delay)
  43. delayed_message_counter = 0
  44.  
  45. # Send a final message indicating that all messages have been sent
  46. bot.send_message(chat_id=chat_id, text="All messages from the channel have been sent to this group.")
  47.  
  48. # Catch any errors and print them to the console
  49. except TelegramError as e:
  50. print(f"Error sending messages to chat {chat_name}: {e}")
  51.  
  52.  
  53. ###################################################################################
  54. def start(update, context):
  55. keyboard = [
  56. [InlineKeyboardButton("Количество сообщений", callback_data='num_messages')],
  57. [InlineKeyboardButton("Задержка между сообщениями", callback_data='delay')]
  58. ]
  59. reply_markup = InlineKeyboardMarkup(keyboard)
  60. update.message.reply_text('Привет! Выбери, что ты хочешь изменить:', reply_markup=reply_markup)
  61.  
  62. def button(update, context):
  63. query = update.callback_query
  64. if query.data == 'num_messages':
  65. query.answer()
  66. query.message.reply_text(f"Текущее количество сообщений: {num_messages}")
  67. query.message.reply_text("Введите новое количество сообщений (от 1 до 100):")
  68. elif query.data == 'delay':
  69. query.answer()
  70. query.message.reply_text(f"Текущая задержка между сообщениями: {delay/60} минут.")
  71. query.message.reply_text("Введите новую задержку между сообщениями (в формате чч:мм):")
  72.  
  73. def set_num_messages(update, context):
  74. new_num_messages = int(update.message.text)
  75. global num_messages
  76. num_messages = new_num_messages
  77. update.message.reply_text(f"Количество сообщений сохранено: {num_messages}")
  78.  
  79. def set_delay(update, context):
  80. new_delay = update.message.text
  81. try:
  82. hours, minutes = map(int, new_delay.split(':'))
  83. if hours not in range(24) or minutes not in range(60):
  84. raise ValueError
  85. global delay
  86. delay = (hours*60 + minutes)*60
  87. update.message.reply_text(f"Задержка между сообщениями сохранена: {hours} часов {minutes} минут.")
  88. except ValueError:
  89. update.message.reply_text("Неправильный формат времени. Введите время в формате чч:мм (от 00:00 до 23:59).")
  90.  
  91. updater = Updater(TOKEN, use_context=True)
  92. dispatcher = updater.dispatcher
  93.  
  94. dispatcher.add_handler(CommandHandler('start', start))
  95. dispatcher.add_handler(CallbackQueryHandler(button))
  96. dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command & Filters.regex('^\d+$'), set_num_messages))
  97. dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command & Filters.regex('^\d{2}:\d{2}$'), set_delay))
  98.  
  99. updater.start_polling()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement