From 48ae1a79f1ec8d6d1fc922026b5256c82bb589b7 Mon Sep 17 00:00:00 2001 From: James Snow Date: Mon, 7 Feb 2022 14:49:27 -0600 Subject: [PATCH] Use a fixed context in the `invoke` cli command. (#55) * Use a fixed context in the `invoke` cli command. Context is immutable and is not able to be changed by the skill. The invoke command in the CLI however expected that on turns after the first the context would be provided by the skill response. This led to the context working on the first request but being empty on all subsequent requests. The context is now set to a default value before calling a skill and the requests reuse the same context value until exit. --- webex_skills/cli/skill.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/webex_skills/cli/skill.py b/webex_skills/cli/skill.py index 36ab970..23f2f88 100644 --- a/webex_skills/cli/skill.py +++ b/webex_skills/cli/skill.py @@ -85,29 +85,30 @@ def invoke_skill( device_id: str, verbose: bool = False, ): - challenge = os.urandom(32).hex() default_params = { 'time_zone': 'UTC', 'timestamp': datetime.utcnow().timestamp(), 'language': 'en', } - message = { - 'challenge': challenge, + + default_context = { + 'userId': user_id, + 'orgId': org_id, + 'developerDeviceId': device_id, + } + + req = { + 'challenge': os.urandom(32).hex(), 'text': query, - 'context': { - 'userId': user_id, - 'orgId': org_id, - 'developerDeviceId': device_id, - }, + 'context': default_context, 'params': default_params, 'frame': {}, 'history': [], } while True: - req = message if encrypted: - req = prepare_payload(json.dumps(message), public_key, secret) + req = prepare_payload(json.dumps(req), public_key, secret) resp = requests.post(url, json=req) @@ -123,17 +124,16 @@ def invoke_skill( typer.secho('Unable to deserialize JSON response') json_resp = {} - if not json_resp.get('challenge') == challenge: + if not json_resp.get('challenge') == req['challenge']: typer.secho('Skill did not respond with expected challenge value', fg=typer.colors.RED, err=True) typer.secho(pformat(json_resp, indent=2, width=120), fg=typer.colors.GREEN) query = typer.prompt('>>', prompt_suffix=' ') - challenge = os.urandom(32).hex() - message = { - 'challenge': challenge, + req = { + 'challenge': os.urandom(32).hex(), 'text': query, - 'context': json_resp.get('context', {}), + 'context': default_context, 'params': json_resp.get('params', default_params), 'frame': json_resp.get('frame', []), 'history': json_resp.get('history', []),