Skip to content

Commit

Permalink
Add support for specifying context values when invoking a skill. (#54)
Browse files Browse the repository at this point in the history
* Add support for specifying context values when invoking a skill.

This adds optional flags for org/user/device ids to the `invoke` cli command.
Previously we were passing in an empty context however it may be useful, or
even necessary for testing a skill for those ids to be present. If not specified
on the command line a random UUID will be generated.

* Update pylint config to only complain at 10 arguments.

Previous value of 6 is somewhat too low for CLI commands which may have a large
number of options. While any value is going to be essentially arbitrary 10 seems
as good a place to raise an eyebrow as any.
  • Loading branch information
snow0x2d0 committed Feb 2, 2022
1 parent fd140b9 commit 90c7bf4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ disable = [
]

[tool.pylint.DESIGN]
max-args = 6
max-args = 10
min-public-methods = 0

[tool.pylint.FORMAT]
Expand Down
44 changes: 39 additions & 5 deletions webex_skills/cli/skill.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pprint import pformat
import sys
from typing import Optional
import uuid

import requests
import typer
Expand All @@ -19,6 +20,10 @@
app = typer.Typer()


def uuid_str():
return str(uuid.uuid4())


@app.command()
def invoke(
name: Optional[str] = typer.Argument(
Expand All @@ -39,6 +44,21 @@ def invoke(
encrypted: Optional[bool] = typer.Option(
True, '--encrypt/--no-encrypt', is_flag=True, help="Flag to specify if the skill is using encryption."
),
org_id: Optional[str] = typer.Option(
uuid_str,
help='The organization id to be passed via context, if not provided a UUID will be generated.',
show_default=False,
),
user_id: Optional[str] = typer.Option(
uuid_str,
help='The user id to be passed via context, if not provided a UUID will be generated.',
show_default=False,
),
device_id: Optional[str] = typer.Option(
uuid_str,
help='The device id to passed via context, if not provided a UUID will be generated.',
show_default=False,
),
):
"""Invoke a skill running locally or remotely"""
if name:
Expand All @@ -51,10 +71,20 @@ def invoke(
public_key_text = public_key_path.read_text(encoding='utf-8')
typer.echo('Enter commands below (Ctl+C to exit)')
query = typer.prompt('>>', prompt_suffix=' ')
invoke_skill(query, url, encrypted, public_key_text, secret, verbose)


def invoke_skill(query, url, encrypted, public_key, secret, verbose=False):
invoke_skill(query, url, encrypted, public_key_text, secret, org_id, user_id, device_id, verbose)


def invoke_skill(
query: str,
url: str,
encrypted: bool,
public_key: str,
secret: str,
org_id: str,
user_id: str,
device_id: str,
verbose: bool = False,
):
challenge = os.urandom(32).hex()
default_params = {
'time_zone': 'UTC',
Expand All @@ -64,7 +94,11 @@ def invoke_skill(query, url, encrypted, public_key, secret, verbose=False):
message = {
'challenge': challenge,
'text': query,
'context': {},
'context': {
'userId': user_id,
'orgId': org_id,
'developerDeviceId': device_id,
},
'params': default_params,
'frame': {},
'history': [],
Expand Down

0 comments on commit 90c7bf4

Please sign in to comment.