Skip to content

Commit

Permalink
Invite to change users email to eliminate the most frequent support r…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
f213 committed Jan 15, 2024
1 parent ff7e3ac commit c2b14ec
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
20 changes: 6 additions & 14 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ async def send_text_message(update: TextMessageUpdate, user: User) -> None:
text = update.message.text
subject = get_subject(text)

await update.message.reply_text(text=render("message_is_sent"))

tasks.send_text.delay(
user_id=user.pk,
subject=subject,
text=text,
)
await update.message.reply_text(text=render("message_is_sent", invite_to_change_email=("@" in text)))


@reply
Expand Down Expand Up @@ -116,25 +115,18 @@ async def prompt_for_confirm(update: TextMessageUpdate) -> None:
await update.message.reply_text(render("waiting_for_confirmation"), reply_markup=reply_markup)


class ConfirmedUserFilter(filters.MessageFilter):
def filter(self, message: HumanMessage) -> bool:
user = get_user_instance(message.from_user, message.chat_id)

return user.is_confirmed


class UserWithoutEmailFilter(filters.MessageFilter):
def filter(self, message: HumanMessage) -> bool:
user = get_user_instance(message.from_user, message.chat_id)

return user.email is None


class NonConfirmedUserFilter(filters.MessageFilter):
class ConfirmedUserFilter(filters.MessageFilter):
def filter(self, message: HumanMessage) -> bool:
user = get_user_instance(message.from_user, message.chat_id)

return user.email is not None and user.is_confirmed is False
return user.email is not None and user.is_confirmed is True


def bot_app() -> Application:
Expand All @@ -151,18 +143,18 @@ def bot_app() -> Application:
) # looks like email, so send confirmation to it
application.add_handler(
MessageHandler(
NonConfirmedUserFilter() & filters.TEXT & filters.Regex("Change email"),
~ConfirmedUserFilter() & filters.TEXT & filters.Regex("Change email"),
reset_email,
)
) # change email
application.add_handler(
MessageHandler(
NonConfirmedUserFilter() & filters.TEXT & filters.Regex(r"\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12}"),
~ConfirmedUserFilter() & filters.TEXT & filters.Regex(r"\w{8}\-\w{4}\-\w{4}\-\w{4}\-\w{12}"),
confirm_email,
)
) # confirm email
application.add_handler(MessageHandler(UserWithoutEmailFilter(), prompt_for_setting_email))
application.add_handler(MessageHandler(NonConfirmedUserFilter(), prompt_for_confirm))
application.add_handler(MessageHandler(~ConfirmedUserFilter(), prompt_for_confirm))
application.add_handler(MessageHandler(ConfirmedUserFilter() & filters.TEXT, send_text_message))
application.add_handler(MessageHandler(ConfirmedUserFilter() & filters.PHOTO, send_photo))

Expand Down
2 changes: 1 addition & 1 deletion src/templates/messages/message_is_sent.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Ok, sending
Ok, sending{% if invite_to_change_email %}. Going to change your email? Hit /reset{% endif %}
5 changes: 2 additions & 3 deletions src/tpl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from pathlib import Path
from typing import Any

from jinja2 import Environment, FileSystemLoader, Template

from .models import User

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


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

Expand Down

0 comments on commit c2b14ec

Please sign in to comment.