Skip to content

Commit

Permalink
Single render function
Browse files Browse the repository at this point in the history
  • Loading branch information
f213 committed Jan 13, 2024
1 parent d46bbda commit da5d849
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 32 deletions.
19 changes: 10 additions & 9 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
from .framework import reply
from .helpers import download, enable_logging, get_subject, init_sentry
from .models import User, create_tables, get_user_instance
from .t import HumanMessage, MessageUpdate, TemplateRenderFunction, TextMessageUpdate
from .t import HumanMessage, MessageUpdate, TextMessageUpdate
from .tpl import render

load_dotenv()


@reply
async def start(update: TextMessageUpdate, render: TemplateRenderFunction) -> None:
async def start(update: TextMessageUpdate) -> None:
await update.message.reply_text(
text=render("hello_message"),
)


@reply
async def reset_email(update: TextMessageUpdate, user: User, render: TemplateRenderFunction) -> None:
async def reset_email(update: TextMessageUpdate, user: User) -> None:
user.email = None
user.is_confirmed = False
user.save()
Expand All @@ -36,7 +37,7 @@ async def reset_email(update: TextMessageUpdate, user: User, render: TemplateRen


@reply
async def confirm_email(update: TextMessageUpdate, user: User, render: TemplateRenderFunction) -> None:
async def confirm_email(update: TextMessageUpdate, user: User) -> None:
key = update.message.text.strip()

if user.confirmation != key:
Expand All @@ -50,7 +51,7 @@ async def confirm_email(update: TextMessageUpdate, user: User, render: TemplateR


@reply
async def send_text_message(update: TextMessageUpdate, user: User, render: TemplateRenderFunction) -> None:
async def send_text_message(update: TextMessageUpdate, user: User) -> None:
text = update.message.text
subject = get_subject(text)

Expand All @@ -64,7 +65,7 @@ async def send_text_message(update: TextMessageUpdate, user: User, render: Templ


@reply
async def send_photo(update: MessageUpdate, user: User, render: TemplateRenderFunction) -> None:
async def send_photo(update: MessageUpdate, user: User) -> None:
file = await update.message.photo[-1].get_file()
photo = await download(file)
subject = "Photo note to self"
Expand All @@ -87,12 +88,12 @@ async def send_photo(update: MessageUpdate, user: User, render: TemplateRenderFu


@reply
async def prompt_for_setting_email(update: TextMessageUpdate, render: TemplateRenderFunction) -> None:
async def prompt_for_setting_email(update: TextMessageUpdate) -> None:
await update.message.reply_text(text=render("please_send_email"))


@reply
async def send_confirmation(update: TextMessageUpdate, user: User, render: TemplateRenderFunction) -> None:
async def send_confirmation(update: TextMessageUpdate, user: User) -> None:
email = update.message.text.strip()

if User.select().where(User.email == email):
Expand All @@ -110,7 +111,7 @@ async def send_confirmation(update: TextMessageUpdate, user: User, render: Templ


@reply
async def prompt_for_confirm(update: TextMessageUpdate, render: TemplateRenderFunction) -> None:
async def prompt_for_confirm(update: TextMessageUpdate) -> None:
reply_markup = ReplyKeyboardMarkup([["Change email"]])
await update.message.reply_text(render("waiting_for_confirmation"), reply_markup=reply_markup)

Expand Down
3 changes: 2 additions & 1 deletion src/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from celery import Celery
from dotenv import load_dotenv
from .helpers import init_sentry

from .helpers import init_sentry
from .mail import send_mail
from .models import User
from .tpl import get_template
Expand All @@ -23,6 +23,7 @@

init_sentry()


@celery.task
def send_confirmation_mail(user_id: int) -> None:
user = User.get(User.pk == user_id)
Expand Down
9 changes: 0 additions & 9 deletions src/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from .models import User, get_user_instance
from .t import HumanMessage, MessageUpdate
from .tpl import get_template


def _get_user(update: MessageUpdate) -> User:
Expand All @@ -17,11 +16,6 @@ def _get_user(update: MessageUpdate) -> User:
)


def _render(tpl: str, **kwargs: str | User) -> str:
template = get_template("messages/" + tpl + ".txt")
return template.render(**kwargs)


def reply(fn: Callable) -> Callable[[Update, CallbackContext], Coroutine]:
params = signature(fn).parameters

Expand All @@ -36,9 +30,6 @@ async def call(update: Update, context: CallbackContext) -> Any:
if "context" in params:
kwargs["context"] = context

if "render" in params:
kwargs["render"] = _render

return await fn(**kwargs)

return call
Expand Down
13 changes: 8 additions & 5 deletions src/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ def init_sentry() -> None:
sentry_dsn = os.getenv("SENTRY_DSN", None)

if sentry_dsn:
sentry_sdk.init(sentry_dsn, integrations=[
AsyncioIntegration(),
CeleryIntegration(),
FlaskIntegration(),
])
sentry_sdk.init(
sentry_dsn,
integrations=[
AsyncioIntegration(),
CeleryIntegration(),
FlaskIntegration(),
],
)


def capfirst(x: str) -> str:
Expand Down
7 changes: 0 additions & 7 deletions src/t.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Any, Protocol

from telegram import Message, PhotoSize, Update, User


Expand All @@ -26,8 +24,3 @@ class PhotoMessage(HumanMessage):

class FileMessageUpdate(MessageUpdate):
message: PhotoMessage


class TemplateRenderFunction(Protocol):
def __call__(self, tpl: str, **kwargs: Any) -> str:
...
8 changes: 8 additions & 0 deletions src/tpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from jinja2 import Environment, FileSystemLoader, Template

from .models import User

env = Environment(
loader=FileSystemLoader(Path(__file__).parent / "templates"),
)
Expand All @@ -11,6 +13,12 @@ def get_template(template_name: str) -> Template:
return env.get_template(template_name)


def render(tpl: str, **kwargs: str | User) -> str:
template = get_template("messages/" + tpl + ".txt")
return template.render(**kwargs)


__all__ = [
"get_template",
"render",
]
3 changes: 2 additions & 1 deletion src/web.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dotenv import load_dotenv
from .helpers import init_sentry
from flask import Flask, render_template

from .helpers import init_sentry
from .models import get_user_by_confirmation_link

load_dotenv()
Expand All @@ -10,6 +10,7 @@

init_sentry()


@app.route("/confirm/<key>/")
def confirm(key: str) -> str:
user = get_user_by_confirmation_link(key)
Expand Down

0 comments on commit da5d849

Please sign in to comment.