Skip to content
This repository has been archived by the owner on Jan 8, 2022. It is now read-only.

Commit

Permalink
improve the user experience for bot commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aahnik committed May 12, 2021
1 parent 70585e6 commit 840f44d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
26 changes: 22 additions & 4 deletions telewater/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
from telethon import events

from telewater import conf
from telewater.utils import cleanup, download_image, get_args, stamp
from telewater.utils import cleanup, download_image, get_args, stamp,gen_kv_str
from telewater.watermark import watermark_image, watermark_video


async def start(event):
await event.respond("Hi! I am alive.")
await event.respond(conf.START)
raise events.StopPropagation


Expand All @@ -24,10 +24,18 @@ async def bot_help(event):


async def set_config(event):
"""usage /set KEY: VAL"""

notes = f"""This command is used to set the value of a config variable.
Usage `/set key: val`
Example `/set watermark: https://link/to/watermark.png`
{gen_kv_str()}
""".replace(" ","")

global config
try:
pos_arg = get_args(event.message.text)
if not pos_arg:
raise ValueError(f"{notes}")
splitted = pos_arg.split(":", 1)

if not len(splitted) == 2:
Expand Down Expand Up @@ -62,9 +70,19 @@ async def set_config(event):


async def get_config(event):
"""usage /get KEY"""

notes = f"""This command is used to get the value of a configuration variable.
Usage `/get key`
Example `/get x_off`
{gen_kv_str()}
""".replace(" ","")



try:
key = get_args(event.message.text)
if not key:
raise ValueError(f"{notes}")
config_dict = conf.config.dict()
await event.respond(f"{config_dict.get(key)}")
except ValueError as err:
Expand Down
12 changes: 9 additions & 3 deletions telewater/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ def validate_preset(val):
raise ValueError(f"Choose preset from {allowed}")
return val

START = """This bot is made by aahnik.dev
See source code at github.com/aahnik/telewater
"""

HELP = """
This bot is made with free and open source code.
Using the bot is very simple. Just send a photo, video or gif to the bot. The bot will reply with the watermarked media.
Please star on GitHub
The bot commands `/set` and `/get` can set and get the value of the configuration variables. The commands are simple and intuitive. The bot will show you the usage if you send an incorrect argument.
https://github.com/aahnik/telewater
Syntax for `/set` ➜ `/set key: value`
Syntax for `/get` ➜ `/get key`
"""

Expand Down
11 changes: 10 additions & 1 deletion telewater/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import requests

from telewater import conf


def download_image(url: str, filename: str = "image.png") -> bool:
if filename in os.listdir():
Expand All @@ -34,7 +36,7 @@ def download_image(url: str, filename: str = "image.png") -> bool:
def get_args(text: str):
splitted = text.split(" ", 1)
if not len(splitted) == 2:
raise ValueError(f"No args provided to command.")
return ""
else:
prefix, args = splitted
print(prefix)
Expand Down Expand Up @@ -64,3 +66,10 @@ def stamp(file: str, user: str):

def safe_name(file_name: str):
return re.sub(pattern="[-!@#$%^&*()\s]", repl="_", string=file_name)


def gen_kv_str():
kv_string = "\n**Below is your current configuration**\n\n"
for k, v in conf.config.dict().items():
kv_string += f"`{k} : {v}`\n"
return kv_string

0 comments on commit 840f44d

Please sign in to comment.