|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "id": "undefined-customs", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [ |
| 9 | + { |
| 10 | + "name": "stdout", |
| 11 | + "output_type": "stream", |
| 12 | + "text": [ |
| 13 | + "IPython autoawait is `on`, and set to use `asyncio`\n" |
| 14 | + ] |
| 15 | + } |
| 16 | + ], |
| 17 | + "source": [ |
| 18 | + "%autoawait\n", |
| 19 | + "import time\n", |
| 20 | + "import asyncio\n", |
| 21 | + "from aries_basic_controller.aries_controller import AriesAgentController\n", |
| 22 | + " \n", |
| 23 | + "WEBHOOK_HOST = \"0.0.0.0\"\n", |
| 24 | + "WEBHOOK_PORT = 8052\n", |
| 25 | + "WEBHOOK_BASE = \"\"\n", |
| 26 | + "ADMIN_URL = \"http://hospital1-agent:8051\"\n", |
| 27 | + "\n", |
| 28 | + "agent_controller = AriesAgentController(webhook_host=WEBHOOK_HOST, webhook_port=WEBHOOK_PORT,\n", |
| 29 | + " webhook_base=WEBHOOK_BASE, admin_url=ADMIN_URL)" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "code", |
| 34 | + "execution_count": 4, |
| 35 | + "id": "leading-madness", |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "\n", |
| 40 | + "loop = asyncio.get_event_loop()\n", |
| 41 | + "loop.create_task(agent_controller.listen_webhooks())\n", |
| 42 | + "\n", |
| 43 | + "def messages_handler(payload):\n", |
| 44 | + " connection_id = payload[\"connection_id\"]\n", |
| 45 | + " asyncio.get_event_loop().create_task(agent_controller.messaging.send_message(connection_id, \"This is a response from Bob\"))\n", |
| 46 | + " print(\"Handle message\", payload, connection_id)\n", |
| 47 | + "\n", |
| 48 | + "\n", |
| 49 | + "message_listener = {\n", |
| 50 | + " \"handler\": messages_handler,\n", |
| 51 | + " \"topic\": \"basicmessages\"\n", |
| 52 | + "}\n", |
| 53 | + "\n", |
| 54 | + "def connection_handler(payload):\n", |
| 55 | + " print(\"Connection Handler Called\")\n", |
| 56 | + " connection_id = payload[\"connection_id\"]\n", |
| 57 | + " state = payload[\"state\"]\n", |
| 58 | + " print(f\"Connection {connection_id} in State {state}\")\n", |
| 59 | + " \n", |
| 60 | + "connection_listener = {\n", |
| 61 | + " \"handler\": connection_handler,\n", |
| 62 | + " \"topic\": \"connections\"\n", |
| 63 | + "}\n", |
| 64 | + "\n", |
| 65 | + "agent_controller.register_listeners([connection_listener, message_listener], defaults=True)" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "id": "periodic-hepatitis", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "## Copy Invite from Researcher" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 5, |
| 79 | + "id": "right-guatemala", |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "#Paste in invitation from researcher agent\n", |
| 84 | + "invitation = {'@type': 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation', '@id': '31d22c96-b531-467a-8637-867f0e9199ac', 'label': 'Health Researcher', 'serviceEndpoint': 'http://172.17.0.1:8040', 'recipientKeys': ['Hu8va1kwBxycxKa731q6FeenAmhk5s3x2X3MXXR7CckE']}" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": 6, |
| 90 | + "id": "future-desert", |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [ |
| 93 | + { |
| 94 | + "name": "stdout", |
| 95 | + "output_type": "stream", |
| 96 | + "text": [ |
| 97 | + "Connection Handler Called\n", |
| 98 | + "Connection ec35bfc5-68ef-45c4-87c6-5d9f63819c77 in State invitation\n", |
| 99 | + "Connection Handler Called\n", |
| 100 | + "Connection ec35bfc5-68ef-45c4-87c6-5d9f63819c77 in State request\n", |
| 101 | + "Connection {'invitation_key': 'Hu8va1kwBxycxKa731q6FeenAmhk5s3x2X3MXXR7CckE', 'state': 'request', 'connection_id': 'ec35bfc5-68ef-45c4-87c6-5d9f63819c77', 'routing_state': 'none', 'updated_at': '2021-01-23 14:22:15.871577Z', 'initiator': 'external', 'invitation_mode': 'once', 'created_at': '2021-01-23 14:22:15.851490Z', 'accept': 'manual', 'their_label': 'Health Researcher', 'my_did': 'ALrPXCqEuqa5wYFUDDkfeo', 'request_id': 'b9560178-dc00-4496-9a8c-2da146166b4d'}\n", |
| 102 | + "Connection Handler Called\n", |
| 103 | + "Connection ec35bfc5-68ef-45c4-87c6-5d9f63819c77 in State response\n", |
| 104 | + "Connection Handler Called\n", |
| 105 | + "Connection ec35bfc5-68ef-45c4-87c6-5d9f63819c77 in State active\n" |
| 106 | + ] |
| 107 | + } |
| 108 | + ], |
| 109 | + "source": [ |
| 110 | + "# Receive Invitation\n", |
| 111 | + "response = await agent_controller.connections.accept_connection(invitation)\n", |
| 112 | + "# Print out accepted Invite and Alice's connection ID\n", |
| 113 | + "print(\"Connection\", response)\n", |
| 114 | + "alice_id = response[\"connection_id\"]\n" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "id": "narrow-signature", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "# Print connection list\n", |
| 125 | + "connection = await agent_controller.connections.get_connection(alice_id)\n", |
| 126 | + "print(\"Alice AGENT CONNECTION\")\n", |
| 127 | + "print(connection)\n", |
| 128 | + "print(\"State:\", connection[\"state\"])" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": null, |
| 134 | + "id": "bearing-tuition", |
| 135 | + "metadata": {}, |
| 136 | + "outputs": [], |
| 137 | + "source": [] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "id": "encouraging-guard", |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "response = await agent_controller.terminate()\n", |
| 147 | + "print(response)" |
| 148 | + ] |
| 149 | + } |
| 150 | + ], |
| 151 | + "metadata": { |
| 152 | + "kernelspec": { |
| 153 | + "display_name": "Python 3", |
| 154 | + "language": "python", |
| 155 | + "name": "python3" |
| 156 | + }, |
| 157 | + "language_info": { |
| 158 | + "codemirror_mode": { |
| 159 | + "name": "ipython", |
| 160 | + "version": 3 |
| 161 | + }, |
| 162 | + "file_extension": ".py", |
| 163 | + "mimetype": "text/x-python", |
| 164 | + "name": "python", |
| 165 | + "nbconvert_exporter": "python", |
| 166 | + "pygments_lexer": "ipython3", |
| 167 | + "version": "3.7.6" |
| 168 | + } |
| 169 | + }, |
| 170 | + "nbformat": 4, |
| 171 | + "nbformat_minor": 5 |
| 172 | +} |
0 commit comments