From 1a2f01fa8e8a3dffdaf2f041aada630607915d62 Mon Sep 17 00:00:00 2001 From: Philotheephilix Date: Sat, 9 Sep 2023 17:48:55 +0530 Subject: [PATCH] code maintenence initial redesign of code using functions and packages --- .env | 4 --- .vscode/launch.json | 29 +++++++++++++++ README.md | 5 ++- bot_action.txt | 7 ++++ multi_bot.py | 88 +++++++++++++++++++++------------------------ 5 files changed, 79 insertions(+), 54 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 bot_action.txt diff --git a/.env b/.env index cefe1b7..e69de29 100644 --- a/.env +++ b/.env @@ -1,4 +0,0 @@ -E-mail_for_checking_email= -Password_for_checking_mail= -Telegram_bot_API= -Open_Weather_API= diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c70af6b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Attach", + "type": "cppdbg", + "request": "attach", + "program": "enter program name, for example ${workspaceFolder}/a.exe", + "MIMode": "gdb", + "miDebuggerPath": "/path/to/gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 91f9868..f1c1fdf 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,8 @@ This bot can automate these following tasks 7. Commands : This command returns all the available commands - 8. Reels : when the user sends the link of the instagram reel the bot retrieves the reels video from instagram and sends it to the user - 9. Posts : when the user sends the link of the instagram post the bot retrieves the post from instagram and sends it to the user + 8. Reels : when the user sends the link of the instagram reel, The bot retrieves the reels video from instagram and sends it to the user + 9. Posts : when the user sends the link of the instagram post, The bot retrieves the post from instagram and sends it to the user ## Links [Releases](https://github.com/Philotheephilix/Multi-funtional-telegram-bot/releases) @@ -64,7 +64,6 @@ BotFather is the official bot used to create and manage Telegram bots. Here are jpg2pdf - converts images to pdf sos - emergency contact number commands - list all commands - reels - download instagram reels HOORAY Your bot is now created and ready to be configured. diff --git a/bot_action.txt b/bot_action.txt new file mode 100644 index 0000000..1d921d7 --- /dev/null +++ b/bot_action.txt @@ -0,0 +1,7 @@ +/start - greet to check life of the bot +/tell_joke - returns a random joke +/weather - view the weather of the city +/check_email - check email for unread messages +/jpg2pdf - converts images to pdf +/sos - emergency contact number +/commands - return list of commands \ No newline at end of file diff --git a/multi_bot.py b/multi_bot.py index 85d2f7e..7bca1ee 100644 --- a/multi_bot.py +++ b/multi_bot.py @@ -26,33 +26,38 @@ cl = Client() tempdir=os.getcwd() Credentials={} - - +bot = "" +BASE_URL = "http://api.openweathermap.org/data/2.5/weather?" +message="" #Code for handling credential encryption and decryption -a=1 -if a==1: - try: - with open('crypt.key', 'rb') as filekey: - key = filekey.read() +def decrypt(): + a=1 + if a==1: + try: + with open('crypt.key', 'rb') as filekey: + key = filekey.read() - fernet = Fernet(key) - with open('.env', 'rb') as file: - original = file.read() - decrypted = fernet.decrypt(original) - with open('.env', 'wb+') as decrypted_file: - decrypted_file.write(decrypted) - dnc=decrypted_file.read() - print("decrypted") - except : - print("Invalid Key or Already decrypted") - - #loading credentials and keys from env file - load_dotenv(tempdir+"\.env") - username=os.getenv("E-mail_for_checking_email") - password=os.getenv("Password_for_checking_mail") - TELE_API_KEY = os.getenv("Telegram_bot_API") - print(TELE_API_KEY) - OW_API = os.getenv("Open_Weather_API") + fernet = Fernet(key) + with open('.env', 'rb') as file: + original = file.read() + decrypted = fernet.decrypt(original) + with open('.env', 'wb+') as decrypted_file: + decrypted_file.write(decrypted) + dnc=decrypted_file.read() + print("decrypted") + except : + print("Invalid Key or Already decrypted") +decrypt() + +#loading credentials and keys from env file + +load_dotenv(tempdir+"\.env") +username=os.getenv("E-mail_for_checking_email") +password=os.getenv("Password_for_checking_mail") +TELE_API_KEY = os.getenv("Telegram_bot_API") +print(TELE_API_KEY) +OW_API = os.getenv("Open_Weather_API") +def encrypt(): with open(".env","r") as credentials: Credentials=credentials.readlines() print(Credentials) @@ -72,9 +77,9 @@ enc=encrypted_file.read() print("encrypted") except: - print("encrypt failed") - -print(Credentials) + print("encrypt failed") + print(Credentials) +encrypt() # Directory creation and verification def init_dirs(): @@ -88,9 +93,11 @@ def init_dirs(): print("dirs already exists") init_dirs() #Bot initialization -bot = telebot.TeleBot(TELE_API_KEY) -BASE_URL = "http://api.openweathermap.org/data/2.5/weather?" -bot = telebot.TeleBot(TELE_API_KEY) +def bot_init(): + global bot + bot = telebot.TeleBot(TELE_API_KEY) + bot = telebot.TeleBot(TELE_API_KEY) +bot_init() # Initialize Joke.txt Dataset def remove(list): pattern = '[0-9]' @@ -110,14 +117,8 @@ def handle_photos(message): #Code for handling /commands @bot.message_handler(commands=["commands"]) def message(message): - commands="""/start - greet to check life of the bot -/tell_joke - returns a random joke -/weather - view the weather of the city -/check_email - check email for unread messages -/jpg2pdf - converts images to pdf -/sos - emergency contact number -/commands - return list of commands -""" + bot_action=open("bot_action.txt","r") + commands=bot_action.read() bot.reply_to(message,"Here are the list of commands \n"+commands) #Code to handle jpg2pdf conversion @bot.message_handler(commands=["jpg2pdf"]) @@ -276,14 +277,6 @@ def city(message): sos="Emergency Numbers For "+state+"\nAmbulance = "+tmplist[0]+"\nFire = "+tmplist[1]+"\nPolice = "+tmplist[2] bot.reply_to(message,sos) sos_active="0" - elif reels_active=="1": - ori_reel=str(message.text) - reel=ori_reel.split("https://www.instagram.com/reel/") - base_url="https://www.ddinstagram.com/reel/" - Reel_to_send=base_url+reel[1] - Reel_markup="[REEL]("+Reel_to_send+")" - print(Reel_markup) - bot.reply_to(message,Reel_markup) else: status = str(message.text) if status.startswith("https://www.instagram.com/reel/"): @@ -306,5 +299,6 @@ def city(message): bot.reply_to(message,Post_markup) else: bot.reply_to(message,"Enter valid command \n type /commands to list all commands") + #polling command to receive commands from bot bot.infinity_polling()