{"id":556,"date":"2024-08-30T22:02:49","date_gmt":"2024-08-30T19:02:49","guid":{"rendered":"https:\/\/itgen.itbumper.com\/?page_id=556"},"modified":"2024-09-03T02:24:37","modified_gmt":"2024-09-02T23:24:37","slug":"executive-telegram-bot","status":"publish","type":"page","link":"https:\/\/itgen.itbumper.com\/?page_id=556","title":{"rendered":"0010_Executive Telegram Bot"},"content":{"rendered":"<p style=\"text-align: justify;\" data-tadv-p=\"keep\">The working version of my bot, according to the principle: &#8220;It works, and okay,&#8221; I&#8217;m not a programmer; I can do that this way. I will give explanations and an example of the work as soon as I have time.<\/p>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Version: 1.47\nimport os\nimport logging\nimport telebot\nfrom telebot.types import KeyboardButton, ReplyKeyboardMarkup, InlineKeyboardMarkup, InlineKeyboardButton\n\n# Telegram bot token and phone numbers of users in groups\nTELEGRAM_TOKEN = '&lt;YOUR TOKEN&gt;'\nUSER_GROUPS = {\n    'group1': &#x5B;'+XXXXXXXXXXX', '1YYYYYYYYYYYY'],\n    'group2': &#x5B;'3HHHHHHHHHHH'],\n    'group3': &#x5B;'5GGGGGGGGGGG'],\n}\n\n# Initializing the bot\nbot = telebot.TeleBot(TELEGRAM_TOKEN)\n\n# Setting up logging\nlogging.basicConfig(filename='\/SOME_FOLDER\/telebot.log',\n                    level=logging.DEBUG,\n                    format='%(asctime)s - %(levelname)s - %(message)s')\n\n# Dictionary for storing user contact information\nuser_contacts = {}\n\nBASE_PATH = '\/SOME_FOLDER\/scripts'\n\ndef get_user_groups(phone_number):\n    &quot;&quot;&quot;\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0433\u0440\u0443\u043f\u043f\u044b, \u043a \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043f\u043e \u0435\u0433\u043e \u043d\u043e\u043c\u0435\u0440\u0443 \u0442\u0435\u043b\u0435\u0444\u043e\u043d\u0430.\n       Determines the groups a user belongs to by their phone number.&quot;&quot;&quot;\n    user_groups = &#x5B;group for group, numbers in USER_GROUPS.items() if phone_number in numbers]\n    return user_groups\n\n@bot.message_handler(commands=&#x5B;'start'])\ndef start(message):\n    &quot;&quot;&quot;\u041e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043a\u043e\u043d\u0442\u0430\u043a\u0442\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.\n       Sends a request for the user's contact information.&quot;&quot;&quot;\n    markup = ReplyKeyboardMarkup(one_time_keyboard=True, resize_keyboard=True)\n    contact_button = KeyboardButton(text=&quot;\u041e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u043d\u0442\u0430\u043a\u0442&quot;, request_contact=True)\n    markup.add(contact_button)\n    bot.send_message(message.chat.id, &quot;Please share your contact to continue.&quot;, reply_markup=markup)\n\n@bot.message_handler(content_types=&#x5B;'contact'])\ndef handle_contact(message):\n    &quot;&quot;&quot;\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u0431\u043e\u0442\u043e\u043c.\n       Checks if the user is allowed to interact with the bot.&quot;&quot;&quot;\n    user_phone = message.contact.phone_number\n    user_groups = get_user_groups(user_phone)\n    \n    if user_groups:\n        bot.send_message(message.chat.id, f&quot;Welcome! You belong to groups: {', '.join(user_groups)}&quot;)\n        user_contacts&#x5B;message.chat.id] = user_phone\n        navigate_folders(message.chat.id, BASE_PATH, user_groups)\n    else:\n        bot.send_message(message.chat.id, &quot;\u0414\u043e\u0441\u0442\u0443\u043f \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d.&quot;)\n        logging.warning(f&quot;Unauthorized access attempt from {user_phone}&quot;)\n\ndef navigate_folders(chat_id, current_path, user_groups):\n    &quot;&quot;&quot;\u041d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044f \u043f\u043e \u043f\u0430\u043f\u043a\u0430\u043c \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0433\u0440\u0443\u043f\u043f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.\n       Navigates folders considering the user's group permissions.&quot;&quot;&quot;\n    markup = InlineKeyboardMarkup()\n    \n    # The &quot;Back&quot; button if not in the root directory\n    if current_path != BASE_PATH:\n        markup.add(InlineKeyboardButton('Back', callback_data=f&quot;nav:{os.path.dirname(current_path)}&quot;))\n    \n    # List of directories and files\n    for item in os.listdir(current_path):\n        item_path = os.path.join(current_path, item)\n        if item_path.startswith(BASE_PATH):  # \u0423\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u043f\u0443\u0442\u044c \u043d\u0435 \u0432\u044b\u0448\u0435 BASE_PATH\n            if os.path.isdir(item_path):\n                markup.add(InlineKeyboardButton(f&quot;&#x5B;DIR] {item}&quot;, callback_data=f&quot;nav:{item_path}&quot;))\n            elif os.path.isfile(item_path):\n                # Checking the visibility of the file for the user\n                if (item.startswith('100_') and 'group1' in user_groups) or \\\n                   (item.startswith('101_') and 'group2' in user_groups) or \\\n                   (item.startswith('111_') and 'group3' in user_groups):\n                    markup.add(InlineKeyboardButton(item, callback_data=f&quot;file:{item_path}&quot;))\n    \n    bot.send_message(chat_id, f&quot;\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u044f: {current_path}&quot;, reply_markup=markup)\n\n@bot.callback_query_handler(func=lambda call: call.data.startswith('nav:'))\ndef handle_navigation(call):\n    &quot;&quot;&quot;\u041e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u044e \u043f\u043e \u043f\u0430\u043f\u043a\u0430\u043c.\n       Handles folder navigation.&quot;&quot;&quot;\n    _, path = call.data.split(':')\n    \n    # Checking for folder access\n    if path.startswith(BASE_PATH):\n        user_phone = user_contacts.get(call.message.chat.id, None)\n        if user_phone:\n            user_groups = get_user_groups(user_phone)\n            navigate_folders(call.message.chat.id, path, user_groups)\n    else:\n        bot.send_message(call.message.chat.id, &quot;\u0414\u043e\u0441\u0442\u0443\u043f \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d.&quot;)\n        logging.warning(f&quot;\u041f\u043e\u043f\u044b\u0442\u043a\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0435\u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043f\u043a\u0435: {path}&quot;)\n\n@bot.callback_query_handler(func=lambda call: call.data.startswith('file:'))\ndef handle_file_selection(call):\n    &quot;&quot;&quot;\u0417\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u043a\u0440\u0438\u043f\u0442\u0430.\n       Requests confirmation to execute the script.&quot;&quot;&quot;\n    _, file_path = call.data.split(':')\n    confirm_markup = InlineKeyboardMarkup()\n    confirm_markup.add(InlineKeyboardButton('\u041f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u0442\u044c', callback_data=f&quot;run:{file_path}&quot;))\n    bot.send_message(call.message.chat.id, f&quot;\u0412\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c {os.path.basename(file_path)}?&quot;, reply_markup=confirm_markup)\n\n@bot.callback_query_handler(func=lambda call: call.data.startswith('run:'))\ndef handle_script_execution(call):\n    &quot;&quot;&quot;\u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441\u043a\u0440\u0438\u043f\u0442 \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u044f.\n       Executes the script after confirmation.&quot;&quot;&quot;\n    _, file_path = call.data.split(':')\n    \n    # Checking for file access\n    if file_path.startswith(BASE_PATH):\n        os.system(f&quot;bash {file_path}&quot;)\n        bot.send_message(call.message.chat.id, f&quot;The script  {os.path.basename(file_path)}  is executed.&quot;)\n        logging.info(f&quot;The script is executed: {file_path}&quot;)\n    else:\n        bot.send_message(call.message.chat.id, &quot;\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0444\u0430\u0439\u043b\u0443 \u0437\u0430\u043f\u0440\u0435\u0449\u0435\u043d.&quot;)\n        logging.warning(f&quot;Attempt to execute an unresolved file: {file_path}&quot;)\n\n# Launching the bot\nbot.polling()\n\n\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>The working version of my bot, according to the principle: &#8220;It works, and okay,&#8221; I&#8217;m not a programmer; I can do that this way. I will give explanations and an example of the work as soon as I have time.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/556"}],"collection":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=556"}],"version-history":[{"count":3,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/556\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=\/wp\/v2\/pages\/556\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itgen.itbumper.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}