def main(): start_handler = CommandHandler("start", start, filters=Filters.user(OWNER_ID), run_async=True) help_handler = CommandHandler("help", help, filters=Filters.user(OWNER_ID), run_async=True) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) if WEBHOOK and URL: LOGGER.info("Using webhooks.") updater.start_webhook(listen=IP_ADDRESS, port=PORT, url_path=API_KEY) if CERT_PATH: updater.bot.set_webhook(url=URL + API_KEY, certificate=open(CERT_PATH, "rb")) else: updater.bot.set_webhook(url=URL + API_KEY) else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=4) updater.idle()
def forward(update, context): message = update.effective_message # type: Optional[Message] from_chat_id = update.effective_chat.id from_chat_name = update.effective_chat.title or update.effective_chat.first_name for chat in TO_CHATS: to_chat_name = context.bot.get_chat(chat).title or context.bot.get_chat(chat).first_name try: context.bot.forward_message(chat_id=chat, from_chat_id=from_chat_id, message_id=message.message_id) except: LOGGER.exception("Error while forwarding message from chat \"{}\" to chat \"{}\".".\ format(from_chat_name, to_chat_name))
def forward(update: Update, context: CallbackContext): message = update.effective_message chat = update.effective_chat if not message or not chat: return from_chat_name = chat.title or chat.first_name for chat in TO_CHATS: to_chat_name = (context.bot.get_chat(chat).title or context.bot.get_chat(chat).first_name) try: send_message(message, chat) except ChatMigrated as err: send_message(message, err.new_chat_id) LOGGER.warning( f"Chat {chat} has been migrated to {err.new_chat_id}!! Edit the config file!!" ) except: LOGGER.exception( 'Error while forwarding message from chat "{}" to chat "{}".'. format(from_chat_name, to_chat_name))
start, filters=Filters.user(OWNER_ID), run_async=True) help_handler = CommandHandler("help", help, filters=Filters.user(OWNER_ID), run_async=True) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) if WEBHOOK and URL: LOGGER.info("Using webhooks.") updater.start_webhook(listen=IP_ADDRESS, port=PORT, url_path=API_KEY) if CERT_PATH: updater.bot.set_webhook(url=URL + API_KEY, certificate=open(CERT_PATH, "rb")) else: updater.bot.set_webhook(url=URL + API_KEY) else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=4) updater.idle() if __name__ == "__main__": LOGGER.info("Successfully loaded modules: " + str(ALL_MODULES)) main()
from forwarder import FROM_CHATS, TO_CHATS, LOGGER, dispatcher def forward(update, context): message = update.effective_message # type: Optional[Message] from_chat_id = update.effective_chat.id from_chat_name = update.effective_chat.title or update.effective_chat.first_name for chat in TO_CHATS: to_chat_name = context.bot.get_chat(chat).title or context.bot.get_chat(chat).first_name try: context.bot.forward_message(chat_id=chat, from_chat_id=from_chat_id, message_id=message.message_id) except: LOGGER.exception("Error while forwarding message from chat \"{}\" to chat \"{}\".".\ format(from_chat_name, to_chat_name)) try: FORWARD_HANDLER = MessageHandler( Filters.chat(FROM_CHATS) & Filters.update.channel_posts & ~Filters.status_update & ~Filters.command, forward, run_async=True ) dispatcher.add_handler(FORWARD_HANDLER) except ValueError: # When FROM_CHATS list is not set because user doesn't know chat id(s) LOGGER.warn("I can't FORWARD_HANDLER because your FROM_CHATS list is empty.")
start, filters=Filters.user(OWNER_ID), run_async=True) help_handler = CommandHandler("help", help, filters=Filters.user(OWNER_ID), run_async=True) dispatcher.add_handler(start_handler) dispatcher.add_handler(help_handler) if WEBHOOK: LOGGER.info("Using webhooks.") updater.start_webhook(listen=IP_ADDRESS, port=PORT, url_path=API_KEY) if CERT_PATH: updater.bot.set_webhook(url=URL + API_KEY, certificate=open(CERT_PATH, 'rb')) else: updater.bot.set_webhook(url=URL + API_KEY) else: LOGGER.info("Using long polling.") updater.start_polling(timeout=15, read_latency=4) updater.idle() if __name__ == '__main__': LOGGER.info("Plugins cargados con éxito: " + str(ALL_MODULES)) main()
from forwarder import LOGGER def __list_all_modules(): from os.path import dirname, basename, isfile import glob # This generates a list of modules in this folder for the * in __main__ to work. mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith('__init__.py') ] return all_modules ALL_MODULES = sorted(__list_all_modules()) LOGGER.info("Modules to load: " + str(ALL_MODULES))
from forwarder import LOGGER def __list_all_modules(): from os.path import dirname, basename, isfile import glob # This generates a list of modules in this folder for the * in __main__ to work. mod_paths = glob.glob(dirname(__file__) + "/*.py") all_modules = [ basename(f)[:-3] for f in mod_paths if isfile(f) and f.endswith(".py") and not f.endswith('__init__.py') ] return all_modules ALL_MODULES = sorted(__list_all_modules()) LOGGER.info("Plugins para cargar: " + str(ALL_MODULES))
from forwarder import FROM_CHATS, TO_CHATS, LOGGER, dispatcher def forward(update, context): message = update.effective_message # type: Optional[Message] from_chat_id = update.effective_chat.id from_chat_name = update.effective_chat.title or update.effective_chat.first_name for chat in TO_CHATS: to_chat_name = context.bot.get_chat(chat).title or context.bot.get_chat(chat).first_name try: context.bot.forward_message(chat_id=chat, from_chat_id=from_chat_id, message_id=message.message_id) except: LOGGER.exception("Error al reenviar el mensaje del canal \"{}\" al chat \"{}\".".\ format(from_chat_name, to_chat_name)) try: FORWARD_HANDLER = MessageHandler( Filters.chat(FROM_CHATS) & ~Filters.status_update & ~Filters.command, forward, run_async=True ) dispatcher.add_handler(FORWARD_HANDLER) except ValueError: # When FROM_CHATS list is not set because user doesn't know chat id(s) LOGGER.warn("No puedo FORWARD_HANDLER porque su lista FROM_CHATS está vacía.")