Skip to content

Instantly share code, notes, and snippets.

@Happy-Ferret
Created May 9, 2022 23:20
Show Gist options
  • Save Happy-Ferret/058352b36040de1bd7f86931a52e62f8 to your computer and use it in GitHub Desktop.
Save Happy-Ferret/058352b36040de1bd7f86931a52e62f8 to your computer and use it in GitHub Desktop.
Basic Telegram module system for Telethon
api_id = 0
api_hash = ""
botToken = ""
from telethon import TelegramClient, events
import mods
bot = TelegramClient('bot', api_id, api_hash)
bot.start(bot_token=botToken)
modules = mods.get("modules")
@bot.on(events.NewMessage(pattern='/start'))
async def start(event):
"""Send a message when the command /start is issued."""
await event.respond('Hi!')
raise events.StopPropagation
@bot.on(events.NewMessage)
async def message(event):
"""Handle all the callback methods."""
params = event.text.split(" ")
module, params = params[0][1:], params[1:]
index = module.find("@")
if index != -1: module = module[:index]
print("Module name:", module)
if module in modules:
resText = await modules.get(module).callback(params, event)
#await event.respond(resText)
def main():
"""Start the bot."""
bot.run_until_disconnected()
if __name__ == '__main__':
main()
import os
from importlib import import_module
def get(name):
mods = {}
print("Loading module.")
for file in os.listdir(name):
if file.endswith(".py"):
name = file[:file.find(".")]
print(name)
mods[name] = import_module("modules." + name, name)
return mods
import random
import urllib.request, bs4
async def callback(params, event):
url = random.choice(Cards)
cardUrl = url.replace(".html", ".jpg")
doc = bs4.BeautifulSoup(urllib.request.urlopen("http://" + url), features="html.parser")
text = doc.body.find("p").text + "\n" + url
await event.respond(text, file="http://" + cardUrl, force_document=True)
Cards = [
"randomtarotcard.com/TheFool.html",
"randomtarotcard.com/TheMagician.html",
"randomtarotcard.com/TheHighPriestess.html",
"randomtarotcard.com/TheEmpress.html",
"randomtarotcard.com/TheEmperor.html",
"randomtarotcard.com/TheHierophant.html",
"randomtarotcard.com/TheLovers.html",
"randomtarotcard.com/TheChariot.html",
"randomtarotcard.com/Strength.html",
"randomtarotcard.com/TheHermit.html",
"randomtarotcard.com/WheelofFortune.html",
"randomtarotcard.com/Justice.html",
"randomtarotcard.com/TheHangedMan.html",
"randomtarotcard.com/Death.html",
"randomtarotcard.com/Temperance.html",
"randomtarotcard.com/TheDevil.html",
"randomtarotcard.com/TheTower.html",
"randomtarotcard.com/TheStar.html",
"randomtarotcard.com/TheMoon.html",
"randomtarotcard.com/TheSun.html",
"randomtarotcard.com/Judgement.html",
"randomtarotcard.com/TheWorld.html",
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment