Skip to content

Commit e204d35

Browse files
authored
Merge pull request #45 from felipecoppola/DEVP-13-CRUD-transcriptions
DEVP-13 - Add methods for list, view, download and create transcription
2 parents 6894f95 + 235b422 commit e204d35

8 files changed

+436
-0
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
7+
parser.add_argument('--callID', help='identifier for the call', type=str, required=True)
8+
parser.add_argument('--legID', help='identifier for the leg object you wish to list the recordings for', type=str, required=True)
9+
parser.add_argument('--recordingID', help='identifier for the recording', type=str, required=True)
10+
parser.add_argument('--language', help='language parameter', type=str, required=True)
11+
args = vars(parser.parse_args())
12+
13+
try:
14+
client = messagebird.Client(args['accessKey'])
15+
16+
voiceTranscriptions = client.voice_transcription_create(
17+
args['callID'],
18+
args['legID'],
19+
args['recordingID'],
20+
args['language']
21+
)
22+
23+
print('\nThe following information was returned as a %s object:\n' % voiceTranscriptions.__class__)
24+
if voiceTranscriptions.items is not None:
25+
print(' Containing the the following items:')
26+
for item in voiceTranscriptions.items:
27+
print(' {')
28+
print(' id : %s' % item.id)
29+
print(' recording id : %s' % item.recordingId)
30+
print(' leg id : %s' % item.legId)
31+
print(' status : %s' % item.status)
32+
print(' createdAt : %s' % item.createdAt)
33+
print(' updatedAt : %s' % item.updatedAt)
34+
print(' },')
35+
else:
36+
print(' With an empty response.')
37+
38+
except messagebird.client.ErrorException as e:
39+
print('An error occured while requesting a Voice Transcriptions object list object:')
40+
41+
for error in e.errors:
42+
print(' code : %d' % error.code)
43+
print(' description : %s' % error.description)
44+
print(' parameter : %s\n' % error.parameter)
45+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
from messagebird import client, Client
4+
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
7+
parser.add_argument('--callID', help='identifier for the call', type=str, required=True)
8+
parser.add_argument('--legID', help='identifier for the leg object you wish to list the recordings for', type=str, required=True)
9+
parser.add_argument('--recordingID', help='identifier for the recording', type=str, required=True)
10+
parser.add_argument('--transcriptionFile', help='identifier for the transcription file', type=str, required=True)
11+
args = vars(parser.parse_args())
12+
13+
try:
14+
client = Client(args['accessKey'])
15+
client.voice_transcription_download(
16+
args['callID'],
17+
args['legID'],
18+
args['recordingID'],
19+
args['transcriptionFile']
20+
)
21+
22+
except client.ErrorException as e:
23+
print('An error occured while requesting a Voice Transcriptions object list object:')
24+
25+
for error in e.errors:
26+
print(' code : %d' % error.code)
27+
print(' description : %s' % error.description)
28+
print(' parameter : %s\n' % error.parameter)

examples/voice_transcription_list.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env python
2+
import argparse
3+
import messagebird
4+
5+
parser = argparse.ArgumentParser()
6+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
7+
parser.add_argument('--callID', help='identifier for the call', type=str, required=True)
8+
parser.add_argument('--legID', help='identifier for the leg object you wish to list the recordings for', type=str, required=True)
9+
parser.add_argument('--recordingID', help='identifier for the recording', type=str, required=True)
10+
args = vars(parser.parse_args())
11+
12+
try:
13+
client = messagebird.Client(args['accessKey'])
14+
15+
voiceTranscriptions = client.voice_transcription_list(
16+
args['callID'],
17+
args['legID'],
18+
args['recordingID']
19+
)
20+
21+
print('\nThe following information was returned as a %s object:\n' % voiceTranscriptions.__class__)
22+
if voiceTranscriptions.items is not None:
23+
print(' Containing the the following items:')
24+
for item in voiceTranscriptions.items:
25+
print(' {')
26+
print(' id : %s' % item.id)
27+
print(' recording id : %s' % item.recordingId)
28+
print(' leg id : %s' % item.legId)
29+
print(' status : %s' % item.status)
30+
print(' createdAt : %s' % item.createdAt)
31+
print(' updatedAt : %s' % item.updatedAt)
32+
print(' },')
33+
else:
34+
print(' With an empty response.')
35+
36+
except messagebird.client.ErrorException as e:
37+
print('An error occured while requesting a Voice Transcriptions object list object:')
38+
39+
for error in e.errors:
40+
print(' code : %d' % error.code)
41+
print(' description : %s' % error.description)
42+
print(' parameter : %s\n' % error.parameter)

examples/voice_transcription_view.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
import messagebird, argparse
3+
4+
parser = argparse.ArgumentParser()
5+
parser.add_argument('--accessKey', help='access key for MessageBird API', type=str, required=True)
6+
parser.add_argument('--callID', help='identifier for the call', type=str, required=True)
7+
parser.add_argument('--legID', help='identifier for the leg object you wish to list the recordings for', type=str, required=True)
8+
parser.add_argument('--recordingID', help='identifier for the recording', type=str, required=True)
9+
parser.add_argument('--transcriptionID', help='identifier for the transcription', type=str, required=True)
10+
args = vars(parser.parse_args())
11+
12+
try:
13+
client = messagebird.Client(args['accessKey'])
14+
15+
voiceTranscriptions = client.voice_transcription_view(
16+
args['callID'],
17+
args['legID'],
18+
args['recordingID'],
19+
args['transcriptionID']
20+
)
21+
22+
print('\nThe following information was returned as a %s object:\n' % voiceTranscriptions.__class__)
23+
if voiceTranscriptions.items is not None:
24+
print(' Containing the the following items:')
25+
for item in voiceTranscriptions.items:
26+
print(' {')
27+
print(' id : %s' % item.id)
28+
print(' recording id : %s' % item.recordingId)
29+
print(' leg id : %s' % item.legId)
30+
print(' status : %s' % item.status)
31+
print(' createdAt : %s' % item.createdAt)
32+
print(' updatedAt : %s' % item.updatedAt)
33+
print(' },')
34+
else:
35+
print(' With an empty response.')
36+
37+
except messagebird.client.ErrorException as e:
38+
print('An error occured while requesting a Voice Transcriptions object list object:')
39+
40+
for error in e.errors:
41+
print(' code : %d' % error.code)
42+
print(' description : %s' % error.description)
43+
print(' parameter : %s\n' % error.parameter)

messagebird/client.py

+23
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from messagebird.conversation import Conversation, ConversationList
2121
from messagebird.conversation_webhook import ConversationWebhook, ConversationWebhookList
2222
from messagebird.voice_recording import VoiceRecordingsList, VoiceRecording
23+
from messagebird.voice_transcription import VoiceTranscriptionsList, VoiceTranscriptionsView
2324
from messagebird.call_flow import CallFlow, CallFlowList, CallFlowNumberList
2425

2526

@@ -44,6 +45,7 @@
4445
VOICE_PATH = 'calls'
4546
VOICE_LEGS_PATH = 'legs'
4647
VOICE_RECORDINGS_PATH = 'recordings'
48+
VOICE_TRANSCRIPTIONS_PATH = 'transcriptions'
4749

4850

4951
class ErrorException(Exception):
@@ -382,6 +384,27 @@ def voice_recording_list_recordings(self, call_id, leg_id):
382384
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH
383385
return VoiceRecordingsList().load(self.request(uri, 'GET'))
384386

387+
def voice_transcription_list(self, call_id, leg_id, recording_id):
388+
"""List voice transcriptions."""
389+
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH + '/' + str(recording_id) + '/' + VOICE_TRANSCRIPTIONS_PATH
390+
return VoiceTranscriptionsList().load(self.request(uri, 'GET'))
391+
392+
def voice_transcription_download(self, call_id, leg_id, recording_id, transcriptions_file):
393+
"""Download voice transcription file."""
394+
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH + '/' + str(recording_id) + '/' + VOICE_TRANSCRIPTIONS_PATH + '/' + str(transcriptions_file)
395+
return self.request(uri, 'GET')
396+
397+
def voice_transcription_view(self, call_id, leg_id, recording_id, transcriptions_id):
398+
"""Get voice transcription data."""
399+
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH + '/' + str(recording_id) + '/' + VOICE_TRANSCRIPTIONS_PATH + '/' + str(transcriptions_id)
400+
return VoiceTranscriptionsView().load(self.request(uri, 'GET'))
401+
402+
def voice_transcription_create(self, call_id, leg_id, recording_id, language):
403+
"""Create a voice transcription."""
404+
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH + '/' + str(recording_id) + '/' + VOICE_TRANSCRIPTIONS_PATH
405+
params = {'language': str(language)}
406+
return VoiceTranscriptionsView().load(self.request(uri, 'POST', params, VOICE_TYPE))
407+
385408
def voice_recording_view(self, call_id, leg_id, recording_id):
386409
uri = VOICE_API_ROOT + '/' + VOICE_PATH + '/' + str(call_id) + '/' + VOICE_LEGS_PATH + '/' + str(leg_id) + '/' + VOICE_RECORDINGS_PATH + '/' + str(recording_id)
387410
recording_response = self.request(uri, 'GET')

messagebird/voice_transcription.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from messagebird.base_list import BaseList
2+
from messagebird.voice_transcription_data import VoiceTranscriptionData
3+
4+
5+
class VoiceTranscriptionsList(BaseList):
6+
def __init__(self):
7+
super(VoiceTranscriptionsList, self).__init__(VoiceTranscriptionData)
8+
self.perPage = None
9+
self.currentPage = None
10+
self.pageCount = None
11+
self._pagination = None
12+
13+
@property
14+
def data(self):
15+
return self.items
16+
17+
@property
18+
def pagination(self):
19+
return {
20+
"totalCount": self.totalCount,
21+
"pageCount": self.pageCount,
22+
"currentPage": self.currentPage,
23+
"perPage": self.perPage
24+
}
25+
26+
@pagination.setter
27+
def pagination(self, value):
28+
if isinstance(value, dict):
29+
self.totalCount = value['totalCount']
30+
self.pageCount = value['pageCount']
31+
self.currentPage = value['currentPage']
32+
self.perPage = value['perPage']
33+
self.limit = self.perPage * self.currentPage
34+
self.offset = self.perPage * (self.currentPage - 1)
35+
36+
@data.setter
37+
def data(self, value):
38+
if isinstance(value, list):
39+
self.count = len(value)
40+
self.items = value
41+
42+
43+
class VoiceTranscriptionsView(BaseList):
44+
def __init__(self):
45+
super(VoiceTranscriptionsView, self).__init__(VoiceTranscriptionData)
46+
47+
@property
48+
def data(self):
49+
return self.items
50+
51+
@data.setter
52+
def data(self, value):
53+
if isinstance(value, list):
54+
self.items = value
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from messagebird.base import Base
2+
3+
4+
class VoiceTranscriptionData(Base):
5+
6+
def __init__(self):
7+
self.id = None
8+
self.recordingId = None
9+
self.legId = None
10+
self.status = None
11+
self._createdDatetime = None
12+
self._updatedDatetime = None
13+
self._links = None
14+
15+
@property
16+
def createdDatetime(self):
17+
return self._createdDatetime
18+
19+
@createdDatetime.setter
20+
def createdAt(self, value):
21+
if value is not None:
22+
self._createdDatetime = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
23+
24+
@property
25+
def updatedDatetime(self):
26+
return self._updatedDatetime
27+
28+
@updatedDatetime.setter
29+
def updatedAt(self, value):
30+
if value is not None:
31+
self._updatedDatetime = self.value_to_time(value, '%Y-%m-%dT%H:%M:%SZ')
32+
33+
def __str__(self):
34+
return "\n".join([
35+
'id : %s' % self.id,
36+
'recording id : %s' % self.recordingId,
37+
'status : %s' % self.status,
38+
'leg id : %s' % self.legId,
39+
'created date time : %s' % self._createdDatetime,
40+
'updated date time : %s' % self._updatedDatetime,
41+
'links : %s' % self._links
42+
])

0 commit comments

Comments
 (0)