@@ -93,44 +93,44 @@ After the metadata, the next packets your websocket receives will be Transcripti
93
93
import asyncio
94
94
import json
95
95
import websockets
96
-
97
- from azure.communication.callautomation.streaming.models import (
98
- TranscriptionMetadata, TranscriptionData, WordData, TextFormat, ResultStatus
99
- )
100
- from azure.communication.callautomation.streaming.streaming_data_parser import StreamingDataParser
96
+ from azure.communication.callautomation._shared.models import identifier_from_raw_id
101
97
102
98
async def handle_client (websocket , path ):
103
99
print (" Client connected" )
104
100
try :
105
101
async for message in websocket:
106
- print (message)
107
- result = StreamingDataParser.parse(message)
108
-
109
- if isinstance (result, TranscriptionMetadata):
110
- print (" Parsed data is metadata" )
102
+ json_object = json.loads(message)
103
+ kind = json_object[' kind' ]
104
+ if kind == ' TranscriptionMetadata' :
105
+ print (" Transcription metadata" )
111
106
print (" -------------------------" )
112
- print (" Subscription ID:" , result.subscriptionId)
113
- print (" Locale:" , result.locale)
114
- print (" Call Connection ID:" , result.callConnectionId)
115
- print (" Correlation ID:" , result.correlationId)
116
-
117
- elif isinstance (result, TranscriptionData):
118
- print (" Parsed data is transcription data" )
107
+ print (" Subscription ID:" , json_object[' transcriptionMetadata' ][' subscriptionId' ])
108
+ print (" Locale:" , json_object[' transcriptionMetadata' ][' locale' ])
109
+ print (" Call Connection ID:" , json_object[' transcriptionMetadata' ][' callConnectionId' ])
110
+ print (" Correlation ID:" , json_object[' transcriptionMetadata' ][' correlationId' ])
111
+ if kind == ' TranscriptionData' :
112
+ participant = identifier_from_raw_id(json_object[' transcriptionData' ][' participantRawID' ])
113
+ word_data_list = json_object[' transcriptionData' ][' words' ]
114
+ print (" Transcription data" )
119
115
print (" -------------------------" )
120
- print (" Text:" , result. text)
121
- print (" Format:" , result. format)
122
- print (" Confidence:" , result. confidence)
123
- print (" Offset:" , result. offset)
124
- print (" Duration:" , result. duration)
125
- print (" Participant:" , result. participant.raw_id)
126
- print (" Result Status:" , result. resultStatus)
127
- for word in result.words :
128
- print (" Word:" , word. text)
129
- print (" Offset:" , word. offset)
130
- print (" Duration:" , word. duration)
131
-
116
+ print (" Text:" , json_object[ ' transcriptionData ' ][ ' text' ] )
117
+ print (" Format:" , json_object[ ' transcriptionData ' ][ ' format' ] )
118
+ print (" Confidence:" , json_object[ ' transcriptionData ' ][ ' confidence' ] )
119
+ print (" Offset:" , json_object[ ' transcriptionData ' ][ ' offset' ] )
120
+ print (" Duration:" , json_object[ ' transcriptionData ' ][ ' duration' ] )
121
+ print (" Participant:" , participant.raw_id)
122
+ print (" Result Status:" , json_object[ ' transcriptionData ' ][ ' resultStatus' ] )
123
+ for word in word_data_list :
124
+ print (" Word:" , word[ ' text' ] )
125
+ print (" Offset:" , word[ ' offset' ] )
126
+ print (" Duration:" , word[ ' duration' ] )
127
+
132
128
except websockets.exceptions.ConnectionClosedOK:
133
129
print (" Client disconnected" )
130
+ except websockets.exceptions.ConnectionClosedError as e:
131
+ print (" Connection closed with error: %s " , e)
132
+ except Exception as e:
133
+ print (" Unexpected error: %s " , e)
134
134
135
135
start_server = websockets.serve(handle_client, " localhost" , 8081 )
136
136
0 commit comments