Created
October 1, 2020 20:22
-
-
Save JosXa/5f23e71e71b96162e3da1b2698e0dd48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from haps import Inject | |
from pydantic.dataclasses import dataclass | |
from app.abstractions.base_module import BaseModule | |
from app.clients.telegram.botclients import BotClient | |
from app.clients.telegram.userclients import UserClient | |
from botkit.builders import ViewBuilder | |
from botkit.core.modules import Module, module | |
from botkit.models import IGatherer | |
from botkit.routing.pipelines.executionplan import RemoveTrigger, SendTo | |
from botkit.routing.route_builder.builder import RouteBuilder | |
from botkit.tghelpers.names import display_name | |
from botkit.views.botkit_context import Context | |
from pyrogram import filters | |
@module | |
class PeopleDemoModule(BaseModule): | |
def register(self, routes: RouteBuilder): | |
with routes.using(self.user_client): | |
( | |
routes.on(filters.command("hi") & filters.reply) | |
.remove_trigger() | |
.gather(PersonModel) | |
.then_send(render_person, via_bot=self.bot_client, to="same_chat_quote_replied_to") | |
) | |
(routes.on(filters.reply).remove_trigger(RemoveTrigger.aggressively).send()) | |
with routes.using(self.bot_client): | |
routes.on_action("set_rocks").mutate(PersonModel.set_rocks).then_update(render_person) | |
@dataclass | |
class PersonModel(IGatherer): | |
name: str | |
rocks: bool = False | |
@classmethod | |
def create_from_context(cls, ctx: Context) -> "PersonModel": | |
return PersonModel(name=display_name(ctx.replied_to_message.from_user)) | |
def set_rocks(self, ctx: Context): | |
self.rocks = ctx.payload | |
def render_person(state: PersonModel, builder: ViewBuilder): | |
( | |
builder.html.h1("Evaluating...") | |
.text(f"Does {state.name} currently rock?") | |
.emojize(":microphone2:") | |
.br() | |
.bold(str(state.rocks)) | |
) | |
if state.rocks: | |
builder.menu.rows[0].action_button("Unrock him", "set_rocks", False) | |
else: | |
builder.menu.rows[0].action_button("Make him rock", "set_rocks", True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment