Skip to content

Commit c4b31f4

Browse files
authored
Merge pull request #45 from wwmoraes/ON-99
Add voice transcriptions
2 parents c2c9fd0 + 1235eba commit c4b31f4

File tree

6 files changed

+306
-2
lines changed

6 files changed

+306
-2
lines changed

examples/transcription.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
var messagebird = require('messagebird')('<YOUR_ACCESS_KEY>');
3+
4+
messagebird.transcriptions.create('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<LANGUAGE>', function (err, data) {
5+
if (err) {
6+
return console.log(err);
7+
}
8+
9+
console.log(data);
10+
});
11+
12+
messagebird.transcriptions.list('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', function (err, data) {
13+
if (err) {
14+
return console.log(err);
15+
}
16+
17+
console.log(data);
18+
});
19+
20+
messagebird.transcriptions.read('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<TRANSCRIPTION_ID>', function (err, data) {
21+
if (err) {
22+
return console.log(err);
23+
}
24+
25+
console.log(data);
26+
});
27+
28+
messagebird.transcriptions.download('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<TRANSCRIPTION_ID>', function (err, data) {
29+
if (err) {
30+
return console.log(err);
31+
}
32+
33+
console.log(data);
34+
});

lib/messagebird.js

+87-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = function (accessKey, timeout, features) {
3030
if (features && 'indexOf' in features && features.indexOf('ENABLE_CONVERSATIONSAPI_WHATSAPP_SANDBOX') !== -1) {
3131
CONVERSATIONSENDPOINT = "whatsapp-sandbox.messagebird.com";
3232
}
33-
33+
3434
/**
3535
* httpRequest does the API call and process the response.
3636
* requestParams.hostname is optional and defaults back to
@@ -1180,5 +1180,91 @@ module.exports = function (accessKey, timeout, features) {
11801180
);
11811181
},
11821182
},
1183+
transcriptions: {
1184+
/**
1185+
* Creates a new transcription.
1186+
* @param {String} callId
1187+
* @param {String} legId
1188+
* @param {String} recordingId
1189+
* @param {String} language
1190+
* @param {Function} callback
1191+
* @return void
1192+
*/
1193+
create: function (callId, legId, recordingId, language, callback) {
1194+
var params = {
1195+
language: language,
1196+
};
1197+
1198+
httpRequest(
1199+
{
1200+
hostname: VOICE_ENDPOINT,
1201+
method: 'POST',
1202+
path: `/calls/${callId}/legs/${legId}/recordings/${recordingId}/transcriptions`,
1203+
params: params,
1204+
},
1205+
callback,
1206+
);
1207+
},
1208+
1209+
/**
1210+
* List transcriptions.
1211+
* @param {String} callId
1212+
* @param {String} legId
1213+
* @param {String} recordingId
1214+
* @param {Function} callback
1215+
* @return void
1216+
*/
1217+
list: function (callId, legId, recordingId, callback) {
1218+
httpRequest(
1219+
{
1220+
hostname: VOICE_ENDPOINT,
1221+
method: 'GET',
1222+
path: `/calls/${callId}/legs/${legId}/recordings/${recordingId}/transcriptions`,
1223+
},
1224+
callback,
1225+
);
1226+
},
1227+
1228+
/**
1229+
* View an existing transcription.
1230+
*
1231+
* @param {String} callId
1232+
* @param {String} legId
1233+
* @param {String} recordingId
1234+
* @param {String} language
1235+
* @param {Function} callback
1236+
* @return void
1237+
*/
1238+
read: function (callId, legId, recordingId, transcriptionId, callback) {
1239+
httpRequest(
1240+
{
1241+
hostname: VOICE_ENDPOINT,
1242+
method: 'GET',
1243+
path: `/calls/${callId}/legs/${legId}/recordings/${recordingId}/transcriptions/${transcriptionId}`
1244+
},
1245+
callback,
1246+
);
1247+
},
1248+
1249+
/**
1250+
* Downloads an existing transcription.
1251+
* @param {String} callId
1252+
* @param {String} legId
1253+
* @param {String} recordingId
1254+
* @param {String} transcriptionId
1255+
* @param {Function} callback
1256+
* @return void
1257+
*/
1258+
download: function (callId, legId, recordingId, transcriptionId, callback) {
1259+
httpRequest(
1260+
{
1261+
hostname: VOICE_ENDPOINT,
1262+
method: 'GET',
1263+
path: `/calls/${callId}/legs/${legId}/recordings/${recordingId}/transcriptions/${transcriptionId}.txt`
1264+
},
1265+
callback,
1266+
);
1267+
},
1268+
},
11831269
};
11841270
};

lib/test.js

+104-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ var cache = {
4747

4848
lookup: {
4949
phoneNumber: number
50-
}
50+
},
51+
52+
transcription: {}
5153
};
5254

5355

@@ -1834,6 +1836,107 @@ queue.push(function () {
18341836
]);
18351837
});
18361838

1839+
// Transcription tests
1840+
const TRANSCRIPTION_EXAMPLE = {
1841+
id: 'transcription_id',
1842+
recordingId: 'recording_id',
1843+
error: null,
1844+
createdAt: 'timestamp',
1845+
updatedAt: 'timestamp',
1846+
_links: {
1847+
"self": "/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions/transcription_id",
1848+
"file": "/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions/transcription_id.txt"
1849+
}
1850+
};
1851+
1852+
// Transcription create
1853+
queue.push(function () {
1854+
nock(VOICE_ENDPOINT)
1855+
.post('/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions', '{"language":"language"}')
1856+
.reply(200, {
1857+
data: [
1858+
TRANSCRIPTION_EXAMPLE
1859+
]
1860+
});
1861+
1862+
messagebird.transcriptions.create('call_id', 'leg_id', 'recording_id', 'language', function (err, data) {
1863+
doTest(err, 'transcriptions.create', [
1864+
['type', data instanceof Object],
1865+
['.id', data.data[0].id === TRANSCRIPTION_EXAMPLE.id],
1866+
['.recordingId', data.data[0].recordingId === TRANSCRIPTION_EXAMPLE.recordingId],
1867+
['.error', data.data[0].error === TRANSCRIPTION_EXAMPLE.error],
1868+
['.createdAt', data.data[0].createdAt === TRANSCRIPTION_EXAMPLE.createdAt],
1869+
['.updatedAt', data.data[0].updatedAt === TRANSCRIPTION_EXAMPLE.updatedAt],
1870+
['._links', data.data[0]._links instanceof Object],
1871+
['._links.self', data.data[0]._links.self === TRANSCRIPTION_EXAMPLE._links.self],
1872+
['._links.file', data.data[0]._links.file === TRANSCRIPTION_EXAMPLE._links.file],
1873+
]);
1874+
});
1875+
});
1876+
1877+
// Transcription list
1878+
queue.push(function () {
1879+
nock(VOICE_ENDPOINT)
1880+
.get('/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions')
1881+
.reply(200, {
1882+
data: [
1883+
TRANSCRIPTION_EXAMPLE
1884+
]
1885+
});
1886+
1887+
messagebird.transcriptions.list('call_id', 'leg_id', 'recording_id', function (err, data) {
1888+
doTest(err, 'transcriptions.list', [
1889+
['type', data instanceof Object],
1890+
['.id', data.data[0].id === TRANSCRIPTION_EXAMPLE.id],
1891+
['.recordingId', data.data[0].recordingId === TRANSCRIPTION_EXAMPLE.recordingId],
1892+
['.error', data.data[0].error === TRANSCRIPTION_EXAMPLE.error],
1893+
['.createdAt', data.data[0].createdAt === TRANSCRIPTION_EXAMPLE.createdAt],
1894+
['.updatedAt', data.data[0].updatedAt === TRANSCRIPTION_EXAMPLE.updatedAt],
1895+
['._links', data.data[0]._links instanceof Object],
1896+
['._links.self', data.data[0]._links.self === TRANSCRIPTION_EXAMPLE._links.self],
1897+
['._links.file', data.data[0]._links.file === TRANSCRIPTION_EXAMPLE._links.file],
1898+
]);
1899+
});
1900+
});
1901+
1902+
// Transcription read
1903+
queue.push(function () {
1904+
nock(VOICE_ENDPOINT)
1905+
.get('/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions/transcription_id')
1906+
.reply(200, {
1907+
data: [
1908+
TRANSCRIPTION_EXAMPLE
1909+
]
1910+
});
1911+
1912+
messagebird.transcriptions.read('call_id', 'leg_id', 'recording_id', 'transcription_id', function (err, data) {
1913+
doTest(err, 'transcriptions.read', [
1914+
['type', data instanceof Object],
1915+
['.id', data.data[0].id == TRANSCRIPTION_EXAMPLE.id],
1916+
['.recordingId', data.data[0].recordingId == TRANSCRIPTION_EXAMPLE.recordingId],
1917+
['.error', data.data[0].error == TRANSCRIPTION_EXAMPLE.error],
1918+
['.createdAt', data.data[0].createdAt == TRANSCRIPTION_EXAMPLE.createdAt],
1919+
['.updatedAt', data.data[0].updatedAt == TRANSCRIPTION_EXAMPLE.updatedAt],
1920+
['._links', data.data[0]._links instanceof Object],
1921+
['._links.self', data.data[0]._links.self == TRANSCRIPTION_EXAMPLE._links.self],
1922+
['._links.file', data.data[0]._links.file == TRANSCRIPTION_EXAMPLE._links.file],
1923+
]);
1924+
});
1925+
});
1926+
1927+
// Transcription download
1928+
queue.push(function () {
1929+
nock(VOICE_ENDPOINT)
1930+
.get('/calls/call_id/legs/leg_id/recordings/recording_id/transcriptions/transcription_id.txt')
1931+
.reply(200, '', {
1932+
'Content-Disposition': 'attachment; filename="transcription_id.txt"',
1933+
});
1934+
1935+
messagebird.transcriptions.download('call_id', 'leg_id', 'recording_id', 'transcription_id', function (err, _) {
1936+
doTest(err, 'transcriptions.download', []);
1937+
});
1938+
});
1939+
18371940
// Start the tests
18381941
if (accessKey) {
18391942
accessType = accessKey.split('_') [0] .toUpperCase();

types/index.d.ts

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './conversations';
2525
import { MmsObject, MmsParameter } from './mms';
2626
import { Features } from './feature';
27+
import { TranscriptionData } from './transcriptions';
2728

2829
type CallbackFn<T = unknown> = (err: Error | null, res: T | null) => void;
2930

@@ -295,6 +296,27 @@ export interface MessageBird {
295296
*/
296297
download(callId: string, legId: string, recordingId: string, callback: CallbackFn): void;
297298
};
299+
/**
300+
* A transcription is a textual representation of a recording as text.
301+
*/
302+
transcriptions: {
303+
/**
304+
* Creates a new transcription
305+
*/
306+
create(callId: string, legId: string, recordingId: string, language: string, callback: CallbackFn<TranscriptionData>): void;
307+
/**
308+
* Lists all transcriptions
309+
*/
310+
list(callId: string, legId: string, recordingId: string, callback: CallbackFn<TranscriptionData>): void;
311+
/**
312+
* Retrieves a transcription
313+
*/
314+
read(callId: string, legId: string, recordingId: string, transcriptionId: string, callback: CallbackFn<TranscriptionData>): void;
315+
/**
316+
* Downloads a transcription
317+
*/
318+
download(callId: string, legId: string, recordingId: string, transcriptionId: string, callback: CallbackFn<boolean|string>): void;
319+
};
298320
}
299321

300322
declare function messagebird(accessKey: string, timeout?: number, features?: ReadonlyArray<Features>): MessageBird;

types/tests/transcriptions.ts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import messagebird from 'messagebird';
2+
3+
const mbClient = messagebird('<AccessKey>');
4+
5+
mbClient.transcriptions.create('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<LANGUAGE>', (
6+
// $ExpectType Error | null
7+
err,
8+
// $ExpectType TranscriptionData | null
9+
transcriptionData
10+
) => {});
11+
12+
mbClient.transcriptions.list('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', (
13+
// $ExpectType Error | null
14+
err,
15+
// $ExpectType TranscriptionData | null
16+
transcriptionData
17+
) => {});
18+
19+
mbClient.transcriptions.read('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<TRANSCRIPTION_ID>', (
20+
// $ExpectType Error | null
21+
err,
22+
// $ExpectType TranscriptionData | null
23+
transcriptionData
24+
) => {});
25+
26+
mbClient.transcriptions.download('<CALL_ID>', '<LEG_ID>', '<RECORDING_ID>', '<TRANSCRIPTION_ID>', (
27+
// $ExpectType Error | null
28+
err,
29+
// $ExpectType string | boolean | null
30+
transcriptionContent
31+
) => {});

types/transcriptions.d.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { datetime } from './general';
2+
3+
export interface TranscriptionLinks {
4+
/** URI for reading the text transcription */
5+
self: string;
6+
/** URI for downloading the text transcription */
7+
file: string;
8+
}
9+
10+
export interface Transcription {
11+
/** The unique ID of the transcription. */
12+
id: string;
13+
/** The format of the recording. Supported formats are: wav. */
14+
recordingId: string;
15+
/** In case that an error was occurred while executing the transcription request, it appears here. */
16+
error: string;
17+
/** The date-time the call was created, in RFC 3339 format (e.g. 2017-03-06T13:34:14Z) */
18+
createdAt: datetime;
19+
/** The date-time the call was last updated, in RFC 3339 format (e.g. 2017-03-06T13:34:14Z). */
20+
updatedAt: datetime;
21+
/** A hash with HATEOAS links related to the object. This includes the file link that has the URI for downloading the text transcription of a recording. */
22+
_links: TranscriptionLinks;
23+
}
24+
25+
export interface TranscriptionData {
26+
/** Transcriptions */
27+
data: Transcription[];
28+
}

0 commit comments

Comments
 (0)