├── rasa_audiocodes ├── version.py ├── __init__.py └── audiocodes.py ├── .gitignore ├── doc └── pnc.png ├── docker-compose.yml ├── requirements.txt ├── pyproject.toml ├── README.md └── LICENSE.txt /rasa_audiocodes/version.py: -------------------------------------------------------------------------------- 1 | __version__ = "1.0.0" 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /dist/ 3 | /rasa_audiocodes.egg-info/ 4 | __pycache__/ 5 | -------------------------------------------------------------------------------- /doc/pnc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ac-voice-ai/rasa-audiocodes/HEAD/doc/pnc.png -------------------------------------------------------------------------------- /rasa_audiocodes/__init__.py: -------------------------------------------------------------------------------- 1 | from rasa_audiocodes import version 2 | from rasa_audiocodes.audiocodes import AudiocodesInput 3 | 4 | __version__ = version.__version__ 5 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | rasa: 5 | image: rasa/rasa:3.4.0a3 6 | command: run 7 | ports: 8 | - 5005:5005 9 | volumes: 10 | - ./app:/app 11 | - ./rasa_audiocodes:/opt/venv/lib/python3.10/site-packages/rasa_audiocodes 12 | 13 | actions: 14 | image: rasa/rasa:3.4.0a3 15 | command: run actions 16 | volumes: 17 | - ./app:/app 18 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # 2 | # This file is autogenerated by pip-compile with Python 3.10 3 | # by the following command: 4 | # 5 | # pip-compile pyproject.toml 6 | # 7 | aiofiles==22.1.0 8 | # via sanic 9 | coloredlogs==15.0.1 10 | # via rasa-sdk 11 | httptools==0.5.0 12 | # via sanic 13 | humanfriendly==10.0 14 | # via coloredlogs 15 | multidict==5.2.0 16 | # via sanic 17 | packaging==23.0 18 | # via sanic-cors 19 | prompt-toolkit==3.0.28 20 | # via rasa-sdk 21 | rasa-sdk==3.4.0 22 | # via rasa-audiocodes (pyproject.toml) 23 | ruamel-yaml==0.17.21 24 | # via rasa-sdk 25 | ruamel-yaml-clib==0.2.7 26 | # via ruamel-yaml 27 | sanic==21.12.2 28 | # via 29 | # rasa-sdk 30 | # sanic-cors 31 | sanic-cors==2.2.0 32 | # via rasa-sdk 33 | sanic-routing==0.7.2 34 | # via sanic 35 | typing-extensions==4.4.0 36 | # via rasa-sdk 37 | ujson==5.7.0 38 | # via sanic 39 | uvloop==0.17.0 40 | # via sanic 41 | wcwidth==0.2.6 42 | # via prompt-toolkit 43 | websockets==10.4 44 | # via sanic 45 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=61.2"] 3 | build-backend = "setuptools.build_meta" 4 | 5 | [project] 6 | name = "rasa_audiocodes" 7 | version = "1.0.0" 8 | authors = [{name = "AudioCodes Ltd.", email = "yaakov.bivas@audiocodes.com"}] 9 | description = "Integrate Rasa with AudioCodes VoiceAI Connect" 10 | classifiers = [ 11 | "Programming Language :: Python", 12 | "Programming Language :: Python :: 3.6", 13 | "Programming Language :: Python :: 3.7", 14 | "Programming Language :: Python :: 3.8", 15 | "Programming Language :: Python :: 3.9", 16 | "Programming Language :: Python :: 3.10", 17 | "License :: OSI Approved :: Apache Software License", 18 | ] 19 | dependencies = ["rasa-sdk>=1.3.2"] 20 | 21 | [project.readme] 22 | file = "README.md" 23 | content-type = "text/markdown" 24 | 25 | [project.urls] 26 | Homepage = "https://github.com/ac-voice-ai/rasa-audiocodes" 27 | "Bug Reports" = "https://github.com/ac-voice-ai/rasa-audiocodes/issues" 28 | Source = "https://github.com/ac-voice-ai/rasa-audiocodes" 29 | 30 | [tool.setuptools] 31 | include-package-data = true 32 | 33 | [tool.setuptools.packages] 34 | find = {namespaces = false} 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AudioCodes VoiceAI Connect Channel for Rasa 2 | 3 | AudioCodes VoiceAI Connect is an application that enables telephony access 4 | for chatbots. 5 | 6 | The Rasa integration is using a REST protocol named 7 | [AC-Bot-API](https://techdocs.audiocodes.com/voice-ai-gateway/api/version-180/#VAIG_API/API_1.htm). 8 | 9 | ## Installation 10 | 11 | The easiest way to install the package is through [PyPI](https://pypi.org/project/rasa-audiocodes). 12 | 13 | ```sh 14 | pip install rasa-audiocodes 15 | ``` 16 | 17 | ## Rasa Configuration 18 | 19 | Add the following content to `credentials.yml`: 20 | 21 | ```yaml 22 | rasa_audiocodes.AudiocodesInput: 23 | token: "CHOOSE_YOUR_TOKEN" 24 | ``` 25 | 26 | ## Docker 27 | 28 | If you're using Rasa on Docker, you can copy rasa_audiocodes and docker-compose.yml 29 | to your application directory, and edit the volumes section to match your app directory. 30 | 31 | If you already have a docker-compose file, just add this to the rasa volumes: 32 | 33 | ```yaml 34 | - ./rasa_audiocodes:/opt/venv/lib/python3.8/site-packages/rasa_audiocodes 35 | ``` 36 | ## VoiceAI Connect Cloud Setup 37 | 38 | The easiest way to start using VAIC is by visiting https://voiceaiconnect.audiocodes.io. 39 | 40 | You can get a phone number, and route it to your bot. 41 | 42 | Configuration is straight-forward. Click the + sign, then enter the name 43 | of the bot, URL and the token. 44 | 45 | You can now click Validate to verify that the bot is accessible and the 46 | audiocodes channel is configured correctly. 47 | 48 | ![PNC Configuration](doc/pnc.png) 49 | 50 | Click Next, then choose a country and a city to get a phone number. 51 | 52 | After a minute, the number should be routed to your bot. 53 | 54 | ## VoiceAI Connect Configuration 55 | 56 | If you maintain a VAIC instance, configure a provider with the following attributes: 57 | 58 | ```json 59 | { 60 | "name": "rasa", 61 | "type": "ac-bot-api", 62 | "botURL": "https:///webhooks/audiocodes/webhook", 63 | "credentials": { 64 | "token": "CHOOSE_YOUR_TOKEN" 65 | } 66 | } 67 | ``` 68 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2019 Magnet S Coop. 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /rasa_audiocodes/audiocodes.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import json 3 | import logging 4 | import uuid 5 | from rasa.core import jobs 6 | from sanic import Blueprint, response 7 | from sanic.exceptions import SanicException 8 | from sanic.request import Request 9 | from sanic.response import HTTPResponse 10 | from typing import Text, Dict, Any, Callable, Awaitable, Optional 11 | 12 | try: 13 | from rasa.shared.constants import INTENT_MESSAGE_PREFIX 14 | except: 15 | from rasa.core.constants import INTENT_MESSAGE_PREFIX 16 | from rasa.core.channels.channel import UserMessage, OutputChannel, InputChannel 17 | 18 | logger = logging.getLogger(__name__) 19 | 20 | CHANNEL_NAME = "audiocodes" 21 | KEEP_ALIVE_SECONDS = "120" 22 | 23 | 24 | class AudiocodesInput(InputChannel): 25 | class Conversation: 26 | def __init__(self, cid): 27 | self.activityIds = [] 28 | self.ws = None 29 | self.cid = cid 30 | self.update() 31 | 32 | def update(self): 33 | self.lastActivity = datetime.datetime.utcnow() 34 | 35 | @staticmethod 36 | def get_metadata(activity: Dict[Text, Any]) -> Optional[Dict[Text, Any]]: 37 | return activity.get("parameters") 38 | 39 | @staticmethod 40 | def _handle_event(event: Dict[Text, Any]) -> Text: 41 | text = f'{INTENT_MESSAGE_PREFIX}vaig_event_{event["name"]}' 42 | event_params = {} 43 | if "parameters" in event: 44 | event_params.update(event["parameters"]) 45 | if "value" in event: 46 | event_params.update({"value": event["value"]}) 47 | if len(event_params) > 0: 48 | text += json.dumps(event_params) 49 | return text 50 | 51 | def is_valid(self, now, delta) -> bool: 52 | if now - self.lastActivity > delta: 53 | logger.warning(f"Conversation {self.cid} is invalid due to inactivity") 54 | return False 55 | return True 56 | 57 | async def handle_activities( 58 | self, 59 | message: Dict[Text, Any], 60 | output_channel: OutputChannel, 61 | on_new_message: Callable[[UserMessage], Awaitable[Any]], 62 | ) -> None: 63 | logger.debug(f"(handle_activities) --- Activities:") 64 | logger.debug(message) 65 | for activity in message["activities"]: 66 | text = None 67 | if activity["id"] in self.activityIds: 68 | logger.warning( 69 | f'Got activity that already handled. Activity ID: {activity["id"]}' 70 | ) 71 | continue 72 | self.activityIds.append(activity["id"]) 73 | if activity["type"] == "message": 74 | text = activity["text"] 75 | elif activity["type"] == "event": 76 | text = self._handle_event(activity) 77 | else: 78 | logger.warning( 79 | "Received an activity from audiocodes that we can not " 80 | f"handle. Activity: {activity}" 81 | ) 82 | if not text: 83 | continue 84 | metadata = self.get_metadata(activity) 85 | user_msg = UserMessage( 86 | text=text, 87 | output_channel=output_channel, 88 | sender_id=self.cid, 89 | metadata=metadata, 90 | ) 91 | try: 92 | await on_new_message(user_msg) 93 | except Exception as e: # skipcq: PYL-W0703 94 | logger.exception( 95 | f"Exception occurred during handled audiocodes message: {user_msg}. {e}" 96 | ) 97 | logger.debug(e, exc_info=True) 98 | await output_channel.send_custom_json( 99 | self.cid, 100 | { 101 | "type": "event", 102 | "name": "hangup", 103 | "text": "Exception occurred during handled last message", 104 | }, 105 | ) 106 | 107 | @classmethod 108 | def name(cls) -> Text: 109 | return CHANNEL_NAME 110 | 111 | @classmethod 112 | def from_credentials( 113 | cls, credentials: Optional[Dict[Text, Any]] 114 | ) -> Optional[InputChannel]: 115 | if not credentials: 116 | cls.raise_missing_credentials_exception() 117 | return None 118 | return cls(credentials.get("token"), credentials.get("websocket_is_on"), credentials.get("keep_alive")) 119 | 120 | def __init__(self, token: Text, websocket_is_on: bool, keep_alive: Optional[Text]) -> None: 121 | self.conversations = {} 122 | self.token = token 123 | self.websocket_is_on = websocket_is_on 124 | self.scheduler_job = None 125 | self.keep_alive = int(keep_alive or KEEP_ALIVE_SECONDS) 126 | 127 | async def _set_scheduler_job(self) -> None: 128 | if self.scheduler_job: 129 | self.scheduler_job.remove() 130 | self.scheduler_job = (await jobs.scheduler()).add_job( 131 | self.clean_old_conversations, "interval", minutes=10 132 | ) 133 | 134 | def _check_token(self, token: Optional[Text]) -> None: 135 | if not token or not token.replace("Bearer ", "") == self.token: 136 | raise SanicException(status_code=401) 137 | 138 | def _get_conversation( 139 | self, token: Optional["Text"], cid: Text 140 | ) -> Optional["Conversation"]: 141 | self._check_token(token) 142 | conversation = self.conversations.get(cid) 143 | if not conversation: 144 | raise SanicException("Not found", status_code=404) 145 | conversation.update() 146 | return conversation 147 | 148 | def clean_old_conversations(self) -> None: 149 | logger.debug( 150 | f"Performing clean old conversations, current number: {len(self.conversations)}" 151 | ) 152 | now = datetime.datetime.utcnow() 153 | delta = datetime.timedelta(seconds=self.keep_alive * 1.5) 154 | self.conversations = { 155 | k: v for k, v in self.conversations.items() if v.is_valid(now, delta) 156 | } 157 | 158 | def handle_start_conversation(self, body: Dict[Text, Any]) -> Dict[Text, Any]: 159 | cid = body["conversation"] 160 | if cid in self.conversations: 161 | raise SanicException("Conversation already exists", status_code=500) 162 | logger.debug(f"(handle_start_conversation) --- New Conversation has arrived. Conversation: {cid}") 163 | self.conversations[cid] = AudiocodesInput.Conversation(cid) 164 | urls = { 165 | "activitiesURL": f"conversation/{cid}/activities", 166 | "disconnectURL": f"conversation/{cid}/disconnect", 167 | "refreshURL": f"conversation/{cid}/keepalive", 168 | "expiresSeconds": self.keep_alive, 169 | } 170 | if self.websocket_is_on: 171 | urls.update({"websocketURL": f"conversation/{cid}/websocket"}) 172 | return urls 173 | 174 | 175 | def blueprint( 176 | self, on_new_message: Callable[[UserMessage], Awaitable[Any]] 177 | ) -> Blueprint: 178 | ac_webhook = Blueprint("ac_webhook", __name__) 179 | 180 | @ac_webhook.websocket('/conversation//websocket') 181 | async def new_client_connection(request, ws, cid: Text): 182 | if self.websocket_is_on is False: 183 | raise ConnectionRefusedError('websocket is unavailable') 184 | logger.debug(f"(new_client_connection) --- New client is trying to connect. Conversation: {cid}") 185 | conversation = self._get_conversation(request.headers.get("Authorization"), cid) 186 | if conversation.ws is not None: 187 | logger.debug(f"(new_client_connection) --- The client was already connected. Conversation: {cid}") 188 | return 189 | conversation.ws = ws 190 | 191 | try: 192 | await ws.recv() 193 | except: 194 | logger.debug(f"(new_client_connection) --- Websocket was closed by client: {cid}") 195 | conversation.ws = None 196 | 197 | 198 | @ac_webhook.route("/", methods=["GET"]) 199 | async def health(request: Request) -> HTTPResponse: 200 | return response.json({"status": "ok"}) 201 | 202 | # {"conversation": , id, timestamp} 203 | @ac_webhook.route("/webhook", methods=["GET", "POST"]) 204 | async def receive(request: Request) -> HTTPResponse: 205 | if not self.scheduler_job: 206 | await self._set_scheduler_job() 207 | self._check_token(request.headers.get("Authorization")) 208 | if request.method == 'GET': 209 | return response.json({ 210 | "type": "ac-bot-api", 211 | "success": True 212 | }) 213 | return response.json(self.handle_start_conversation(request.json)) 214 | 215 | # {"conversation": , "activities": List[Activity]} 216 | @ac_webhook.route("/conversation//activities", methods=["POST"]) 217 | async def on_activities(request: Request, cid: Text) -> HTTPResponse: 218 | logger.debug(f"(on_activities) --- New activities from the user. Conversation: {cid}") 219 | conversation = self._get_conversation( 220 | request.headers.get("Authorization"), cid 221 | ) 222 | ac_output = WebsocketOutput(conversation.ws, cid) if conversation.ws is not None else AudiocodesOutput() 223 | await conversation.handle_activities( 224 | request.json, output_channel=ac_output, on_new_message=on_new_message, 225 | ) 226 | if conversation.ws is None: 227 | return response.json( 228 | {"conversation": cid, "activities": ac_output.messages} 229 | ) 230 | 231 | return response.json({}) 232 | 233 | # {"conversation": , "reason": Optional[Text]} 234 | @ac_webhook.route("/conversation//disconnect", methods=["POST"]) 235 | async def disconnect(request: Request, cid: Text) -> HTTPResponse: 236 | self._get_conversation(request.headers.get("Authorization"), cid) 237 | reason = json.dumps({"reason": request.json.get("reason")}) 238 | await on_new_message( 239 | UserMessage(text=f"{INTENT_MESSAGE_PREFIX}vaig_event_end{reason}", output_channel=None, sender_id=cid) 240 | ) 241 | del self.conversations[cid] 242 | logger.debug(f"(disconnect) --- Conversation was deleted") 243 | return response.json({}) 244 | 245 | # {"conversation": } 246 | @ac_webhook.route("/conversation//keepalive", methods=["POST"]) 247 | async def keepalive(request: Request, cid: Text) -> HTTPResponse: 248 | self._get_conversation(request.headers.get("Authorization"), cid) 249 | return response.json({}) 250 | 251 | return ac_webhook 252 | 253 | 254 | class AudiocodesOutput(OutputChannel): 255 | @classmethod 256 | def name(cls) -> Text: 257 | return CHANNEL_NAME 258 | 259 | def __init__(self) -> None: 260 | self.messages = [] 261 | 262 | async def add_message(self, message) -> None: 263 | logger.debug(f"{self.__class__.__name__}::add_message --- message: {message}") 264 | message.update( 265 | { 266 | "id": str(uuid.uuid4()), 267 | "timestamp": datetime.datetime.utcnow().isoformat("T")[:-3] + "Z", 268 | } 269 | ) 270 | await self.do_add_message(message) 271 | 272 | async def do_add_message(self, message) -> None: 273 | self.messages.append(message) 274 | 275 | async def send_text_message( 276 | self, recipient_id: Text, text: Text, **kwargs: Any 277 | ) -> None: 278 | await self.add_message({ "type": "message", "text": text }) 279 | 280 | async def send_image_url( 281 | self, recipient_id: Text, image: Text, **kwargs: Any 282 | ) -> None: 283 | raise NotImplementedError() 284 | 285 | async def send_attachment( 286 | self, recipient_id: Text, attachment: Text, **kwargs: Any 287 | ) -> None: 288 | raise NotImplementedError() 289 | 290 | async def send_custom_json( 291 | self, recipient_id: Text, json_message: Dict[Text, Any], **kwargs: Any 292 | ) -> None: 293 | await self.add_message(json_message) 294 | 295 | 296 | class WebsocketOutput(AudiocodesOutput): 297 | def __init__(self, ws, cid) -> None: 298 | self.ws = ws 299 | self.cid = cid 300 | 301 | async def do_add_message(self, message) -> None: 302 | await self.ws.send(json.dumps({ "conversation": self.cid, "activities": [message] })) 303 | --------------------------------------------------------------------------------