├── .env.sample ├── .gitignore ├── README.md ├── bot.py ├── helpers.py └── requirements.txt /.env.sample: -------------------------------------------------------------------------------- 1 | BOT_TOKEN= -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | __pycache__/ 2 | *.session 3 | .env -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Morse Code Encoder. 2 | 3 | ## Deploying 4 | Deploy an empty app on heroku and add the BOT_TOKEN variable. 5 | or 6 | Make a .env file in the root of the repository and add BOT_TOKEN, then run the bot using `python(3) bot.py ` command. 7 | 8 | ## Idea 9 | Based off the suggestion by [this guy!](https://t.me/BotzHubChat/67883) :) 10 | ## Credits 11 | - Telethon 12 | - [Me](https://github.com/xditya) 13 | - GeeksForGeeks. -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # Morse Code Encoder. 2 | # < (c) @xditya > 3 | 4 | from telethon import events, Button, TelegramClient 5 | from decouple import config 6 | import logging 7 | from helpers import encode_deocde, send_start 8 | 9 | logging.basicConfig( 10 | level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" 11 | ) 12 | 13 | logging.info("Starting...") 14 | 15 | APP_ID = 6 16 | API_HASH = "eb06d4abfb49dc3eeb1aeb98ae0f581e" 17 | 18 | try: 19 | BOT_TOKEN = config("BOT_TOKEN") 20 | except: 21 | logging.warning("Please create a .env file with BOT_TOKEN.") 22 | exit() 23 | 24 | try: 25 | bot = TelegramClient("BotzHub", APP_ID, API_HASH).start(bot_token=BOT_TOKEN) 26 | except Exception as e: 27 | logging.info(f"Error\n{e}") 28 | exit(0) 29 | 30 | 31 | @bot.on( 32 | events.NewMessage(incoming=True, func=lambda e: e.is_private, pattern="^/start$") 33 | ) 34 | async def start_ms(event): 35 | await send_start(event, "message") 36 | 37 | 38 | @bot.on(events.callbackquery.CallbackQuery(data="start_back")) 39 | async def back_to_strt(event): 40 | await send_start(event, "edt") 41 | 42 | 43 | @bot.on(events.callbackquery.CallbackQuery(data="help_me")) 44 | async def help_me(event): 45 | await event.edit( 46 | f"""**Help menu!**\n 47 | Commands available: 48 | `/encode ` - Encode text in english to morse code. 49 | `/deocde ` - Decode text from morse code to english.""", 50 | buttons=Button.inline("Back", data="start_back"), 51 | ) 52 | 53 | 54 | @bot.on( 55 | events.NewMessage(incoming=True, func=lambda e: e.is_private, pattern="^/encode") 56 | ) 57 | async def encoder(event): 58 | await encode_deocde(event, "encode") 59 | 60 | 61 | @bot.on( 62 | events.NewMessage(incoming=True, func=lambda e: e.is_private, pattern="^/decode") 63 | ) 64 | async def decoder(event): 65 | await encode_deocde(event, "decode") 66 | 67 | 68 | logging.info("Started. Join @BotzHub for more :)") 69 | bot.run_until_disconnected() 70 | -------------------------------------------------------------------------------- /helpers.py: -------------------------------------------------------------------------------- 1 | # By @xditya 2 | 3 | # Translator based off GeeksForGeeks, modded and made simple for suiting my needs. 4 | # https://www.geeksforgeeks.org/morse-code-translator-python/ 5 | 6 | from telethon import Button 7 | 8 | """ 9 | VARIABLE KEY 10 | 'cipher' -> 'stores the morse translated form of the english string' 11 | 'decipher' -> 'stores the english translated form of the morse string' 12 | 'citext' -> 'stores morse code of a single character' 13 | 'i' -> 'keeps count of the spaces between morse characters' 14 | 'message' -> 'stores the string to be encoded or decoded' 15 | """ 16 | 17 | MORSE_CODE_DICT = { 18 | "A": ".-", 19 | "B": "-...", 20 | "C": "-.-.", 21 | "D": "-..", 22 | "E": ".", 23 | "F": "..-.", 24 | "G": "--.", 25 | "H": "....", 26 | "I": "..", 27 | "J": ".---", 28 | "K": "-.-", 29 | "L": ".-..", 30 | "M": "--", 31 | "N": "-.", 32 | "O": "---", 33 | "P": ".--.", 34 | "Q": "--.-", 35 | "R": ".-.", 36 | "S": "...", 37 | "T": "-", 38 | "U": "..-", 39 | "V": "...-", 40 | "W": ".--", 41 | "X": "-..-", 42 | "Y": "-.--", 43 | "Z": "--..", 44 | "1": ".----", 45 | "2": "..---", 46 | "3": "...--", 47 | "4": "....-", 48 | "5": ".....", 49 | "6": "-....", 50 | "7": "--...", 51 | "8": "---..", 52 | "9": "----.", 53 | "0": "-----", 54 | ", ": "--..--", 55 | ".": ".-.-.-", 56 | "?": "..--..", 57 | "/": "-..-.", 58 | "-": "-....-", 59 | "(": "-.--.", 60 | ")": "-.--.-", 61 | } 62 | 63 | # Function to encrypt the string 64 | # according to the morse code chart 65 | def encrypt(message): 66 | return "".join( 67 | MORSE_CODE_DICT[letter] + " " if letter != " " else " " for letter in message 68 | ) 69 | 70 | 71 | # Function to decrypt the string 72 | # from morse to english 73 | def decrypt(message): 74 | # extra space added at the end to access the 75 | # last morse code 76 | message += " " 77 | decipher = "" 78 | citext = "" 79 | for letter in message: 80 | # checks for space 81 | if letter != " ": 82 | # counter to keep track of space 83 | i = 0 84 | # storing morse code of a single character 85 | citext += letter 86 | # in case of space 87 | else: 88 | # if i = 1 that indicates a new character 89 | i += 1 90 | 91 | # if i = 2 that indicates a new word 92 | if i == 2: 93 | # adding space to separate words 94 | decipher += " " 95 | else: 96 | # accessing the keys using their values (reverse of encryption) 97 | decipher += list(MORSE_CODE_DICT.keys())[ 98 | list(MORSE_CODE_DICT.values()).index(citext) 99 | ] 100 | citext = "" 101 | return decipher 102 | 103 | 104 | async def encode_deocde(event, type_): 105 | msg = event.text.split(" ", 1) 106 | try: 107 | text = msg[1] 108 | except IndexError: 109 | return await event.reply("Please use /{type_} ") 110 | msgg = encrypt(text.upper()) if type_ == "encode" else decrypt(text) 111 | await event.reply(f"`{msgg}`") 112 | 113 | 114 | async def send_start(event, type): 115 | user = await event.client.get_entity(event.sender_id) 116 | msg = f"Hi {user.first_name}, I'm a Morse Code Encoder bot. \nCheck the help for more info." 117 | buttons = [ 118 | [Button.inline("Help 🆘", data="help_me")], 119 | [Button.url("Channel 💭", url="https://t.me/BotzHub")], 120 | ] 121 | if type == "message": 122 | await event.reply(msg, buttons=buttons) 123 | elif type == "edt": 124 | await event.edit(msg, buttons=buttons) 125 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | telethon 2 | python-decouple --------------------------------------------------------------------------------