Beispiel #1
0
                data_type=str)
sticker.add_custom_handler(sticker_handler, "This is not a sticker!")

image = State("image", "This is an *image* handler", data_type=str)
image.add_custom_handler(image_handler, "This is not an image!")

video = State("video", "This is a *video* handler", data_type=str)
video.add_custom_handler(video_handler, "This is not a video!")

end = State("end",
            "This is the *end* with a *recap*\n\n@@@",
            back_button=False)
end.add_action(print_recap)

# ---- CONVERSATION
conv = Conversation(sticker, end_state=end)
conv.set_defaults(params={"parse_mode": "Markdown"}, back_button="Back")
conv.add_routes(sticker, default=image)
conv.add_routes(image, default=video, back=sticker)
conv.add_routes(video, default=end, back=image)

# ---- HANDLER
autoconv = AutoConvHandler(conv, STATE)


def autoconv_command(update, context):
    return autoconv.manage_conversation(update, context)


# MAIN --------------------------------------------------------------------------------
Beispiel #2
0
log = State("log",
            "In this state there is a *hidden task* with a terminal log")
log.add_action(log_action)
log.add_keyboard(["Next"])
# this will send a message while the main task is running
log.set_long_task("Computing...")

force = State("force",
              "This state has an *advanced features*, check out the code!")
force.add_keyboard(["Next", "Go to a random state"])
force.add_operation_buttons({1: go_to_random})

end = State("end", "This is the *end*.")

# ---- CONVERSATION
conv = Conversation(simple, end_state=end)
conv.set_defaults(params={"parse_mode": "Markdown"}, back_button="Back")
conv.add_routes(simple, default=riddle)
conv.add_routes(riddle, default=dynamic, back=simple)
conv.add_routes(dynamic, default=log, back=riddle)
conv.add_routes(log, default=force, back=dynamic)
conv.add_routes(force, default=end, back=log)

# ---- HANDLER
autoconv = AutoConvHandler(conv, STATE)


def autoconv_command(update, context):
    return autoconv.manage_conversation(update, context)

gender = State("gender", "Select your *gender*", back_button="< Another back")
gender.add_keyboard(["Male", "Female", "Other"])
age = State("age", "Enter your <b>age</b>", parse_mode="html")
age.add_text(r"\d{1,2}", "Enter a *valid* age")
underage = State("consent", "Drop the _responsibility_ on your parents?")
underage.add_keyboard(["Yes", "Abort"])
comment = State("comment",
                "Do you want to enter additional comment?",
                data_type=comment_type)
comment.add_keyboard(["Nope"])
comment.add_text()
end = State("end", "@@@", back_button=False)
end.add_action(recap)

# ---- CONVERSATION
conv = Conversation(name, end_state=end)
conv.set_defaults(
    params={
        "parse_mode": "Markdown",
        "disable_web_page_preview": True
    },
    func=add_timestamp,
    back_button="< Back",
)
conv.add_routes(name, default=gender)
conv.add_routes(gender, default=age, back=name)
conv.add_routes(age,
                routes={i: underage
                        for i in range(19)},
                default=comment,
                back=gender)
second.add_keyboard(["Next"])

third = State("third")
third.add_keyboard(["Next"])

end = State("end")

# ---- CONVERSATION
texts = {
    "first": "This is the message for the *first* state!",
    "second": "Another text specified in the *dictionary*..",
    "third": "You can define texts in an _external file_\nFormat supported: *JSON*, *YAML* or *TOML*.",
    "end": "This is the *end*.",
}
# it can be also a file (specified via filename) in json, yaml or toml
conv = Conversation(first, end_state=end, state_messages=texts)
conv.set_defaults(params={"parse_mode": "Markdown"})
conv.add_routes(first, default=second)
conv.add_routes(second, default=third, back=first)
conv.add_routes(third, default=end, back=second)


# ---- HANDLER
autoconv = AutoConvHandler(conv, STATE)


def autoconv_command(update, context):
    return autoconv.manage_conversation(update, context)


# MAIN --------------------------------------------------------------------------------
Beispiel #5
0
operation.add_action(show_counter)
operation.add_operation_buttons([add_counter, sub_counter])
# same as a dict -> operation.add_button_operations({0: add_counter, 1: sub_counter})

end = State("end", "This is the *end*.")


# ---- DYNAMIC
def keyboard_dynamic_routes(tdata):
    # need to return a tuple: Routes, Default, back
    state_value = tdata.sdata.get("dynamic")
    return (None, static_dict if state_value is None else custom, None)


# ---- CONVERSATION
conv = Conversation(static, end_state=end)
conv.set_defaults(params={"parse_mode": "Markdown"})
conv.add_routes(static, default=static_dict)
# same as -> conv.add_routes(static, routes={0: static_dict})
conv.add_routes(static_dict, routes={42: static, 99: dynamic}, back=static)
dynamic.add_dynamic_routes(keyboard_dynamic_routes)
conv.add_routes(custom, default=operation)
conv.add_routes(operation, routes={2: end}, back=custom)

# ---- HANDLER
autoconv = AutoConvHandler(conv, STATE)


def autoconv_command(update, context):
    return autoconv.manage_conversation(update, context)
Beispiel #6
0
age.add_text(r'\d{1,2}', 'Enter a *valid* age')
underage = State(
    'consent',
    'Do you know that the responsibility will fall on your parents?',
    back=True)
underage.add_keyboard(['Yes', 'Abort'])
comment = State('comment',
                'Do you want to enter additional comment?',
                type=comment_type,
                back=True)
comment.add_keyboard(['Nope'])
comment.add_text()
end = State('end', '@@@', parse_mode=MARKDOWN)
end.add_action(recap)
# Conversation
example = Conversation(name, end_state=end)
example.add_state([gender, age, underage, comment])
example.add_routes(name, default=gender)
example.add_routes(gender, default=age, back=name)
example.add_routes(age,
                   routes={i: underage
                           for i in range(19)},
                   default=comment,
                   back=gender)
example.add_routes(underage, routes={0: comment, 1: end}, back=age)
example.add_routes(comment, default=end, back=age)
# Handler
autoconv = AutoConvHandler(example, STATE, back_button='Back')


def autoconv_command(update, context):
errors.add_keyboard(["Menu"])
errors.add_action(error_handler)

menu = State("menu",
             "Choose carefully, something is _broken_...",
             back_button=False)
menu.add_keyboard(["Safe state", "Sus state"])

divisonbyzero = State("division", "I want to divide 42 by 0... result=@@@")
divisonbyzero.add_action(lambda _: 42 / 0)
divisonbyzero.add_keyboard(["Next"])

end = State("end", "This is the *end*.", back_button=False)

# ---- CONVERSATION
conv = Conversation(menu, end_state=end, fallback_state=errors)
# a Conversation will run forever, except if it has an end State
# error state is a State that catches all the exceptions in autoconv
conv.set_defaults(params={"parse_mode": "Markdown"}, back_button="Back")
conv.add_routes(errors, default=menu)
conv.add_routes(menu, routes={0: end, 1: divisonbyzero})
conv.add_routes(divisonbyzero, default=end, back=menu)

# ---- HANDLER
autoconv = AutoConvHandler(conv, STATE)


def autoconv_command(update, context):
    return autoconv.manage_conversation(update, context)