diff --git a/.gitignore b/.gitignore index f26b9ef..9d1abf1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .token +.token~ +.env +.env~ __pycache__ \ No newline at end of file diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..a7632ab --- /dev/null +++ b/Pipfile @@ -0,0 +1,13 @@ +[[source]] +url = "https://pypi.org/simple" +verify_ssl = true +name = "pypi" + +[packages] +python-telegram-bot = "*" +requests = "*" + +[dev-packages] + +[requires] +python_version = "3.9" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..3990bb7 --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,148 @@ +{ + "_meta": { + "hash": { + "sha256": "60357ddbdb31438d647403628575c898327cf880391f35376d65b4809e986080" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.9" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "apscheduler": { + "hashes": [ + "sha256:3bb5229eed6fbbdafc13ce962712ae66e175aa214c69bed35a06bffcf0c5e244", + "sha256:e8b1ecdb4c7cb2818913f766d5898183c7cb8936680710a4d3a966e02262e526" + ], + "version": "==3.6.3" + }, + "cachetools": { + "hashes": [ + "sha256:2cc0b89715337ab6dbba85b5b50effe2b0c74e035d83ee8ed637cf52f12ae001", + "sha256:61b5ed1e22a0924aed1d23b478f37e8d52549ff8a961de2909c69bf950020cff" + ], + "version": "==4.2.2" + }, + "certifi": { + "hashes": [ + "sha256:2bbf76fd432960138b3ef6dda3dde0544f27cbf8546c458e60baf371917ba9ee", + "sha256:50b1e4f8446b06f41be7dd6338db18e0990601dce795c2b1686458aa7e8fa7d8" + ], + "version": "==2021.5.30" + }, + "charset-normalizer": { + "hashes": [ + "sha256:5d209c0a931f215cee683b6445e2d77677e7e75e159f78def0db09d68fafcaa6", + "sha256:5ec46d183433dcbd0ab716f2d7f29d8dee50505b3fdb40c6b985c7c4f5a3591f" + ], + "markers": "python_version >= '3'", + "version": "==2.0.6" + }, + "idna": { + "hashes": [ + "sha256:14475042e284991034cb48e06f6851428fb14c4dc953acd9be9a5e95c7b6dd7a", + "sha256:467fbad99067910785144ce333826c71fb0e63a425657295239737f7ecd125f3" + ], + "markers": "python_version >= '3'", + "version": "==3.2" + }, + "python-telegram-bot": { + "hashes": [ + "sha256:24df75459e335b96baffa6991679f844bd426978af5a69ca419a0ac43a40602c", + "sha256:3bf210862744068aa789d5110f8e3a00d98912ce50863384836440a18abf76b5" + ], + "index": "pypi", + "version": "==13.7" + }, + "pytz": { + "hashes": [ + "sha256:83a4a90894bf38e243cf052c8b58f381bfe9a7a483f6a9cab140bc7f702ac4da", + "sha256:eb10ce3e7736052ed3623d49975ce333bcd712c7bb19a58b9e2089d4057d0798" + ], + "version": "==2021.1" + }, + "requests": { + "hashes": [ + "sha256:6c1246513ecd5ecd4528a0906f910e8f0f9c6b8ec72030dc9fd154dc1a6efd24", + "sha256:b8aa58f8cf793ffd8782d3d8cb19e66ef36f7aba4353eec859e74678b01b07a7" + ], + "index": "pypi", + "version": "==2.26.0" + }, + "six": { + "hashes": [ + "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", + "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254" + ], + "version": "==1.16.0" + }, + "tornado": { + "hashes": [ + "sha256:0a00ff4561e2929a2c37ce706cb8233b7907e0cdc22eab98888aca5dd3775feb", + "sha256:0d321a39c36e5f2c4ff12b4ed58d41390460f798422c4504e09eb5678e09998c", + "sha256:1e8225a1070cd8eec59a996c43229fe8f95689cb16e552d130b9793cb570a288", + "sha256:20241b3cb4f425e971cb0a8e4ffc9b0a861530ae3c52f2b0434e6c1b57e9fd95", + "sha256:25ad220258349a12ae87ede08a7b04aca51237721f63b1808d39bdb4b2164558", + "sha256:33892118b165401f291070100d6d09359ca74addda679b60390b09f8ef325ffe", + "sha256:33c6e81d7bd55b468d2e793517c909b139960b6c790a60b7991b9b6b76fb9791", + "sha256:3447475585bae2e77ecb832fc0300c3695516a47d46cefa0528181a34c5b9d3d", + "sha256:34ca2dac9e4d7afb0bed4677512e36a52f09caa6fded70b4e3e1c89dbd92c326", + "sha256:3e63498f680547ed24d2c71e6497f24bca791aca2fe116dbc2bd0ac7f191691b", + "sha256:548430be2740e327b3fe0201abe471f314741efcb0067ec4f2d7dcfb4825f3e4", + "sha256:6196a5c39286cc37c024cd78834fb9345e464525d8991c21e908cc046d1cc02c", + "sha256:61b32d06ae8a036a6607805e6720ef00a3c98207038444ba7fd3d169cd998910", + "sha256:6286efab1ed6e74b7028327365cf7346b1d777d63ab30e21a0f4d5b275fc17d5", + "sha256:65d98939f1a2e74b58839f8c4dab3b6b3c1ce84972ae712be02845e65391ac7c", + "sha256:66324e4e1beede9ac79e60f88de548da58b1f8ab4b2f1354d8375774f997e6c0", + "sha256:6c77c9937962577a6a76917845d06af6ab9197702a42e1346d8ae2e76b5e3675", + "sha256:70dec29e8ac485dbf57481baee40781c63e381bebea080991893cd297742b8fd", + "sha256:7250a3fa399f08ec9cb3f7b1b987955d17e044f1ade821b32e5f435130250d7f", + "sha256:748290bf9112b581c525e6e6d3820621ff020ed95af6f17fedef416b27ed564c", + "sha256:7da13da6f985aab7f6f28debab00c67ff9cbacd588e8477034c0652ac141feea", + "sha256:8f959b26f2634a091bb42241c3ed8d3cedb506e7c27b8dd5c7b9f745318ddbb6", + "sha256:9de9e5188a782be6b1ce866e8a51bc76a0fbaa0e16613823fc38e4fc2556ad05", + "sha256:a48900ecea1cbb71b8c71c620dee15b62f85f7c14189bdeee54966fbd9a0c5bd", + "sha256:b87936fd2c317b6ee08a5741ea06b9d11a6074ef4cc42e031bc6403f82a32575", + "sha256:c77da1263aa361938476f04c4b6c8916001b90b2c2fdd92d8d535e1af48fba5a", + "sha256:cb5ec8eead331e3bb4ce8066cf06d2dfef1bfb1b2a73082dfe8a161301b76e37", + "sha256:cc0ee35043162abbf717b7df924597ade8e5395e7b66d18270116f8745ceb795", + "sha256:d14d30e7f46a0476efb0deb5b61343b1526f73ebb5ed84f23dc794bdb88f9d9f", + "sha256:d371e811d6b156d82aa5f9a4e08b58debf97c302a35714f6f45e35139c332e32", + "sha256:d3d20ea5782ba63ed13bc2b8c291a053c8d807a8fa927d941bd718468f7b950c", + "sha256:d3f7594930c423fd9f5d1a76bee85a2c36fd8b4b16921cae7e965f22575e9c01", + "sha256:dcef026f608f678c118779cd6591c8af6e9b4155c44e0d1bc0c87c036fb8c8c4", + "sha256:e0791ac58d91ac58f694d8d2957884df8e4e2f6687cdf367ef7eb7497f79eaa2", + "sha256:e385b637ac3acaae8022e7e47dfa7b83d3620e432e3ecb9a3f7f58f150e50921", + "sha256:e519d64089b0876c7b467274468709dadf11e41d65f63bba207e04217f47c085", + "sha256:e7229e60ac41a1202444497ddde70a48d33909e484f96eb0da9baf8dc68541df", + "sha256:ed3ad863b1b40cd1d4bd21e7498329ccaece75db5a5bf58cd3c9f130843e7102", + "sha256:f0ba29bafd8e7e22920567ce0d232c26d4d47c8b5cf4ed7b562b5db39fa199c5", + "sha256:fa2ba70284fa42c2a5ecb35e322e68823288a4251f9ba9cc77be04ae15eada68", + "sha256:fba85b6cd9c39be262fcd23865652920832b61583de2a2ca907dbd8e8a8c81e5" + ], + "version": "==6.1" + }, + "tzlocal": { + "hashes": [ + "sha256:c736f2540713deb5938d789ca7c3fc25391e9a20803f05b60ec64987cf086559", + "sha256:f4e6e36db50499e0d92f79b67361041f048e2609d166e93456b50746dc4aef12" + ], + "version": "==3.0" + }, + "urllib3": { + "hashes": [ + "sha256:39fb8672126159acb139a7718dd10806104dec1e2f0f6c88aab05d17df10c8d4", + "sha256:f57b4c16c62fa2760b7e3d97c35b255512fb6b59a259730f36ba32ce9f8e342f" + ], + "version": "==1.26.6" + } + }, + "develop": {} +} diff --git a/README.md b/README.md index fb195d0..b200f68 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,31 @@ It can do all sorts of random things. Add it to your telegram group chat and some of the features will definitely brighten up your chatting experience. +# Getting Started +## Dependencies +From the main directory start the pipenv shell. +``` +pipenv shell +``` + +Once in the shell, install dependencies. +``` +pipenv install +``` + +## Environment +`/PyukuBot/.env` file: +``` +BOT_TOKEN= /* put your token here for testing */ +``` + +## Run + +From the `PyukuBot/src` directory +``` +pipenv run python3 run.py +``` + # Contributing You can contribute to the pyuku bot in multiple ways. Generally, any sort of feature supported by a new command is very much pleased. diff --git a/src/.token~ b/src/.token~ deleted file mode 100644 index 724e240..0000000 --- a/src/.token~ +++ /dev/null @@ -1 +0,0 @@ -1850056100:AAGki5PflfrwQlX4aQ7a6ErnCCxfwLRrxyA diff --git a/src/commands/__init__.py b/src/commands/__init__.py index ee09104..6a9f343 100644 --- a/src/commands/__init__.py +++ b/src/commands/__init__.py @@ -1,18 +1,18 @@ from os import walk +from importlib import import_module f = [] commands = {} # Make a list of command names -for (dirpath,dirnames,filenames) in walk("commands"): +for (dirpath, dirnames, filenames) in walk("commands"): if dirpath == "commands": f.extend(filenames) f.remove("__init__.py") # Import each of them and populate the commands dictionary for each in f: - cm = each[:len(each)- 3] # Truncate the .py at the end - exec(f"from .{cm} import *") - commands[cm] = (eval(f"{cm}.main"), eval(f"{cm}.description")) - + cm = each[: len(each) - 3] # Truncate the .py at the end + module = import_module(f".{cm}", "commands") + commands[cm] = module.main, module.description diff --git a/src/commands/contributors.py b/src/commands/contributors.py new file mode 100644 index 0000000..37d8193 --- /dev/null +++ b/src/commands/contributors.py @@ -0,0 +1,27 @@ +import requests +from decorators import static_command + +description = "shows the contributors of the bot" + + +@static_command +def main(): + try: + resp = requests.get( + "https://api.github.com/repos/adibozzhanov/PyukuBot/contributors?anon=1" + ) + resp.raise_for_status() + except requests.HTTPError: + return "Error. Github API cannot be accessed." + else: + data = resp.json() + contributors = [user["login"] for user in data] + if len(contributors) == 1: + return "The contributor to PyukuBot is" + contributors[0] + "." + return ( + "The contributors to PyukuBot are " + + ", ".join(contributors[:-1]) + + " and " + + contributors[-1] + + "." + ) diff --git a/src/commands/flip.py b/src/commands/flip.py index 40cc5aa..7f27946 100644 --- a/src/commands/flip.py +++ b/src/commands/flip.py @@ -1,7 +1,9 @@ import random from decorators import static_command + description = "flips a coin" + @static_command def main(): choices = ["HEADS", "TAILS"] diff --git a/src/commands/hello_pyuku.py b/src/commands/hello_pyuku.py index 4a65c26..de70396 100644 --- a/src/commands/hello_pyuku.py +++ b/src/commands/hello_pyuku.py @@ -2,7 +2,14 @@ from decorators import named_static_command description = "says hello to the user" + + @named_static_command def main(name): - greetings = ["pleased to meet you", "it's very nice to meet you", "fuck you", "pleased to make your acquaintance"] + greetings = [ + "pleased to meet you", + "it's very nice to meet you", + "fuck you", + "pleased to make your acquaintance", + ] return f"Hello {name}, {choice(greetings)}!" diff --git a/src/commands/roll.py b/src/commands/roll.py index cb313af..d46e637 100644 --- a/src/commands/roll.py +++ b/src/commands/roll.py @@ -3,7 +3,8 @@ description = "rolls a number between 0 and 100" + @named_static_command def main(name): - num = random.randint(0,100) + num = random.randint(0, 100) return f"{name} rolled a number: {num}" diff --git a/src/commands/yn.py b/src/commands/yn.py index d3d37a6..bc6603f 100644 --- a/src/commands/yn.py +++ b/src/commands/yn.py @@ -3,10 +3,8 @@ description = "answers yes or no" + @static_command def main(): answers = ["YES", "NO"] return random.choice(answers) - - - diff --git a/src/decorators.py b/src/decorators.py index c6496c2..2e243fe 100644 --- a/src/decorators.py +++ b/src/decorators.py @@ -1,4 +1,4 @@ -bot = None # this will get overwritten in the bot class. Very hacky but Idc +bot = None # this will get overwritten in the bot class. Very hacky but Idc # Documentaion for how to use these can be found in the readme @@ -8,18 +8,17 @@ def static_command(func): def command(update, context): chat_id = update.message.chat.id text = func() - bot.send_message(chat_id = chat_id, text = text) + bot.send_message(chat_id=chat_id, text=text) + return command + # use that if you want to include senders name in the reply message def named_static_command(func): def command(update, context): chat_id = update.message.chat.id name = update.message.from_user.first_name text = func(name) - bot.send_message(chat_id = chat_id, text = text) + bot.send_message(chat_id=chat_id, text=text) + return command - - - - diff --git a/src/pyuku.py b/src/pyuku.py index 4d0049d..8ad1b60 100644 --- a/src/pyuku.py +++ b/src/pyuku.py @@ -4,12 +4,12 @@ from telegram import Bot from telegram.ext import Updater, CommandHandler, MessageHandler, Filters +# This is the main bot class class Pyuku(Bot): - def __init__(self, token): super().__init__(token) - updater = Updater(token, use_context = True) + updater = Updater(token, use_context=True) self.dp = updater.dispatcher @@ -29,10 +29,3 @@ def initHandlers(self): # Help is a special command handler that contains info about each command # Thus we create it automatically after all handlers are initialised - - - - - - - diff --git a/src/run.py b/src/run.py index 1d78de5..139698c 100644 --- a/src/run.py +++ b/src/run.py @@ -1,13 +1,11 @@ # driver code +import os from pyuku import Pyuku - if __name__ == "__main__": - try: - with open(".token", "r") as tokenFile: - token = tokenFile.readline().strip() - Pyuku(token) - - except IOError: - print("No token file detected.") - + env = os.environ + if "BOT_TOKEN" in env: + token = env["BOT_TOKEN"] + Pyuku(token) + else: + print("There is no token present")