Skip to content

Commit

Permalink
feat: added guided way to create outbound call
Browse files Browse the repository at this point in the history
  • Loading branch information
tgbv committed Oct 2, 2024
1 parent 5245f26 commit 60fd421
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Commands:
push [options] <resoureType> <resourceIdentifier> Push one resource from disk to API. Can be "app", "carrier", "speech", "phone".
clone [options] Clone locally VG app/service provider with all dependencies.
snapshot [options] <action> [snapshotName] Create or restore a snapshot remotely.
create [options] <call> Guided way to create an outbound call.
help [command] display help for command
```

Expand Down
28 changes: 26 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
"type": "git",
"url": "https://github.com/tgbv/cognigy-vg.git"
},
"version": "1.1.3",
"version": "1.2.0",
"license": "MIT",
"description": "CLI to interact with Cognigy Voice Gateway.",
"keywords": [
"cli", "cognigy", "cognigy-vg", "voice-gateway"
"cli",
"cognigy",
"cognigy-vg",
"voice-gateway"
],
"engines": {
"node": ">=18",
Expand All @@ -34,6 +37,7 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/google-libphonenumber": "^7.4.30",
"@types/lodash": "^4.17.7",
"@types/prompts": "^2.4.9",
"ts-node": "^10.9.2"
Expand All @@ -42,6 +46,7 @@
"axios": "^1.7.7",
"commander": "^12.1.0",
"fs-extra": "^11.2.0",
"google-libphonenumber": "^3.2.38",
"lodash": "^4.17.21",
"prompts": "^2.4.2"
}
Expand Down
70 changes: 70 additions & 0 deletions src/commands/create-call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import prompt from "prompts";
import { ILocalConfig, loadJsonFile } from "../lib";
import API from "../lib/api";
import { PhoneNumberUtil } from "google-libphonenumber";

/**
*
*/
export default async (_, options: any ) => {
const config = loadJsonFile(options.configFile) as ILocalConfig;
const api = new API(config, options.AU);

const phones = await api.getRemotePhones();

const { from } = await prompt({
type: 'select',
name: 'from',
message: 'Caller',
choices: phones.map(({ number }) => ({
title: number,
value: number
})),
initial: 0,
});

const { to } = await prompt({
type: 'text',
name: 'to',
message: 'Callee. Must contain the country code',
initial: '+1234567890',
validate: (value: string) => {
try {
const phoneUtil = PhoneNumberUtil.getInstance();

if(phoneUtil.isValidNumber(
phoneUtil.parse(value.trim().replace(' ', ''))
)) {
return true;
}
} catch(e) {}

return 'Invalid phone number';
}
});

const { tag } = await prompt({
type: 'text',
name: 'tag',
message: 'JSON data to send to bot when call initiates',
initial: '{"hello": "world"}',
validate: v => {
try {
JSON.parse(v);
} catch(e) {
return 'Please provide valid JSON.';
}

return true;
}
});

await api.createCall(
from[0] === '+' ? from : `+${from}`,
to[0] === '+' ? to : `+${to}`,
phones.find(o => o.number === from).application_sid,
JSON.parse(tag),
);

console.log('Done.');
}
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import cloneCommand from "./commands/clone";
import snapshotCommand from "./commands/snapshot";
import setToken from "./commands/set-token";
import { loadJsonFile } from "./lib";
import createCallCommand from "./commands/create-call";

const resourceTypesArg = new Argument('<resoureType>', 'Resource type to pull from API.')
.choices(['app', 'carrier', 'speech', 'phone', 'obroutes']);
Expand Down Expand Up @@ -75,5 +76,13 @@ program
.option('-AU', 'Allows unauthorized SSL certificates. Useful if your machine is behind VPN.')
.option('-y', 'Skip confirmations.')
.action(snapshotCommand);

program
.command('create call')
.description('Guided way to create an outbound call.')
.option('--configFile <string>', 'Configuration file path.', './config.json')
.option('-AU', 'Allows unauthorized SSL certificates. Useful if your machine is behind VPN.')
.action(createCallCommand)


program.parse();
23 changes: 23 additions & 0 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,30 @@ export default class API {
});
}

/**
*
* @param from
* @param to
* @param applicationSid
* @param tag
* @returns
*/
createCall = async (from: string, to: string, applicationSid: string, tag: Record<string, any>) => {
const payload: Record<string, any> = {
application_sid: applicationSid,
from,
to: {
type: "phone",
number: to
},
tag,
headers: {
'P-Asserted-Identity': `<sip:${from}@cognigy-vg>`
}
};

return this.axios.post(`/v1/Accounts/${this.accountSid}/Calls`, payload);
}

/**
*
Expand Down

0 comments on commit 60fd421

Please sign in to comment.