Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.token
.token~
.env
.env~
__pycache__
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -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"
148 changes: 148 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 0 additions & 1 deletion src/.token~

This file was deleted.

10 changes: 5 additions & 5 deletions src/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions src/commands/contributors.py
Original file line number Diff line number Diff line change
@@ -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]
+ "."
)
2 changes: 2 additions & 0 deletions src/commands/flip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import random
from decorators import static_command

description = "flips a coin"


@static_command
def main():
choices = ["HEADS", "TAILS"]
Expand Down
9 changes: 8 additions & 1 deletion src/commands/hello_pyuku.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}!"
3 changes: 2 additions & 1 deletion src/commands/roll.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
4 changes: 1 addition & 3 deletions src/commands/yn.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

description = "answers yes or no"


@static_command
def main():
answers = ["YES", "NO"]
return random.choice(answers)



13 changes: 6 additions & 7 deletions src/decorators.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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




11 changes: 2 additions & 9 deletions src/pyuku.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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







16 changes: 7 additions & 9 deletions src/run.py
Original file line number Diff line number Diff line change
@@ -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")