Skip to content

Example handler

The following files are placed in the command_handlers folder and shows the main building blocks of a command handler on the server.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# limepkg-sendsms/limepkg_sendsms/command_handlers/sendsms.py
import phonenumbers
import transactional_message_library.traml as traml

from limepkg_server_commands.commands import (
    Command,
    CommandContext,
    CommandResult,
    CommandResultErrorCode,
    commandhandler,
)


@commandhandler("send-sms")
def handle_send_sms(ctx: CommandContext, command: Command) -> CommandResult:

    if not command.has_parameters("to", "text"):
        return CommandResult.requires_parameter(command, "to", "text")

    text = command.parameters.text
    to = command.parameters.to
    try:
        to = phonenumbers.format_number(
            phonenumbers.parse(to, None), phonenumbers.PhoneNumberFormat.E164
        )
    except phonenumbers.phonenumberutil.NumberParseException:
        return parameter_error(command, f"Invalid phonenumber: {to}")

    traml.notify(
        ctx.application,
        "commandhandler.sendsms",
        traml.Sms(text=text, from_number="LimeCRM", destination_number=to),
    )
    return CommandResult.ok(command)


def parameter_error(command: Command, message: str):
    return CommandResult.fail(command, message, CommandResultErrorCode.PARAMETER)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# limepkg-sendsms/limepkg_sendsms/command_handlers/sendsms_test.py
from typing import List

import pytest
from lime_application.application import LimeApplication

from limepkg_server_commands.commands import Command, CommandDispatcher


def test_sendsms(execute_commands):
    commands = [
        Command(
            "send-sms",
            {"to": "555-not-a-number", "text": "You have to report time in Sublime!"},
        ),
        Command(
            "send-sms",
            {"to": "+46123456789", "text": "You have to report time in Sublime!"},
        ),
    ]
    batchresult = execute_commands(commands)
    cr = batchresult["commandresults"]

    some_failed = any([not command.successful for command in cr])
    assert some_failed, "Batch succeeded"
    assert batchresult["errors"][0] == "Invalid phonenumber: 555-not-a-number"


@pytest.fixture
def execute_commands(limeapp: LimeApplication):
    def execute_batch(commands: List[Command]):
        dispatcher = CommandDispatcher.default()
        ctx = dispatcher.create_context(limeapp)
        commandresults = dispatcher.execute_batch(ctx, commands)

        return {
            "successful": all(
                [commandresult.successful for commandresult in commandresults]
            ),
            "commandresults": commandresults,
            "errors": [r.error.message for r in commandresults if r.error is not None],
        }

    return execute_batch
Back to top