Skip to content

Commit

Permalink
feat: v2 with its features
Browse files Browse the repository at this point in the history
  • Loading branch information
tgbv committed Oct 11, 2024
1 parent 7535cbc commit 97d6d55
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 182 deletions.
18 changes: 6 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# cognigy-vg

CLI to interact with <a href="https://www.cognigy.com/platform/cognigy-voice-gateway" target="_blank">Cognigy Voice Gateway</a> product. Not affiliated with Cognigy or its subsidiaries.
CLI to interact with <a href="https://www.cognigy.com/platform/cognigy-voice-gateway" target="_blank">Cognigy Voice Gateway</a> product. It is account-scoped. Not affiliated with Cognigy or its subsidiaries.

Features include and are not limited to:
- Creating local snapshots (.csnap like).
- Restoring local snapshots remotely.
- Pulling VG resources locally.
- Pushing VG resources remotely.
- Creating outbound calls.

<img src="https://raw.githubusercontent.com/tgbv/cognigy-vg/refs/heads/main/demos/create-snap.gif">

Expand All @@ -33,12 +34,11 @@ Options:

Commands:
init [options] Guided way to initialize new configuration file.
set [options] <token> Quickly set workspace API key/bearer token.
pull [options] <resoureType> <resourceIdentifier> Pull one resource from API to disk. Can be "app", "carrier", "speech", "phone",
"obroutes".
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.
snapshot [options] <action> [snapshotName] Create, restore a snapshot remotely, or inspect it locally.
create [options] <call> Guided way to create an outbound call.
help [command] display help for command
```
Expand All @@ -51,23 +51,17 @@ Usually the FQDN has the scheme: `api-{VG tenant FQDN}`.

Trial tenant FQDN is `vg-trial.cognigy.ai`. Therefore the trial API FQDN is: `api-vg-trial.cognigy.ai`.

### Using the right Bearer Token
### Using the right API Key

If Cognigy does not provide you an API key with permissions to both Account and BYO Services out of the box, you'll have to request one. Alternatively, you may fastly retrieve one via hacky way. Ensure your account has permissions to access the BYO Services, then do the following:

1. Login to Cognigy VG panel
2. Hit F12
3. Go to network tab
4. Navigate any page which fires a GET XHR to the API. Should be any of them.
5. Locate the XHR, locate its Authorization header. The value after 'Bearer' is the token you need.
Unlike v1, you will need to request a single ServiceProvider API key which has access to the account you want to work with, and the BYO Services linked to it. Please contact Cognigy Support to request one.

### Snapshots

A snapshot (.vgsnap) is a JSON file containing the entire dump of your VG Account & BYO Services configuration. To avoid unauthorized access, it is encrypted using AES-256-GCM algorithm provided by OpenSSL. If you do not have the OpenSSL bundle installed on your local machine, you cannot use this feature. Never change any byte of the vgsnap file manually, as there's a big chance you will not be able to decrypt it afterwards.

The encryption key of snapshots is generated at project initialization. If you plan on restoring the created snapshots, <b>never ever loose the key from configuration file: `snapEncryptionKey`</b>

Snapshots are cross-compatible remotely between different VG accounts / byo services.
Snapshots are cross-compatible remotely between different VG accounts / byo services, but may not be compatible locally between major CLI package versions. If there's incompatibility, you will be notified when executing 'snapshot restore' command.

### Speech services not working after restoration?

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "git",
"url": "https://github.com/tgbv/cognigy-vg.git"
},
"version": "1.2.1",
"version": "2.0.0",
"license": "MIT",
"description": "CLI to interact with Cognigy Voice Gateway.",
"keywords": [
Expand Down
8 changes: 8 additions & 0 deletions src/commands/create-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ export default async (_, options: any ) => {
}
});

let apiKeys = await api.getRemoteAccountApiKeys();
if(apiKeys.length === 0) {
console.log('WARN: No API key found for account', config.accountSid, '. Generating one...');
await api.createRemoteAccountApiKey();
apiKeys = await api.getRemoteAccountApiKeys();
}

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

console.log('Done.');
Expand Down
71 changes: 36 additions & 35 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default async ({ AU }) => {
{
type: 'text',
name: 'snapEncryptionKey',
message: 'Snapshots encryption key. Is base64.',
initial: randomBytes(32).toString('base64')
message: 'Snapshots encryption key. Must be base64.',
initial: randomBytes(32).toString('base64'),
validate: (value) => Buffer.from(value, 'base64').toString().length === 0 ?
'Please input valid base64 encoded data.' : true
},
{
type: 'text',
Expand All @@ -61,51 +63,43 @@ export default async ({ AU }) => {
},
], { onCancel: () => process.exit(0) });


const { bearerToken } = await prompts({
const { serviceProviderToken } = await prompts({
type: 'text',
name: 'bearerToken',
message: 'Bearer token for authentication. Please consult: https://github.com/tgbv/cognigy-vg/tree/dev#using-the-right-bearer-token',
hint: 'UUIDv4 or JWT',
validate: (value) => new Promise(accept => {
getAccounts(apiFqdn, value).then(res => {
accounts = res;
getServiceProviders(apiFqdn, value).then(res => {
if(res.length === 0) {
accept('This token cannot be used with any ServiceProvider.');
}
serviceProviders = res;
accept(true)
}).catch(() => accept('Token does not have privileges to retrieve ServiceProviders.'));
}).catch(() => accept('Token does not have privileges to retrieve own Accounts.'));
})
name: 'serviceProviderToken',
message: 'Service Provider API Key',
hint: 'UUIDv4',
validate: async (value) => {
try {
serviceProviders = await getServiceProviders(apiFqdn, value);
if(serviceProviders.length === 0) {
return 'This token cannot be used with any ServiceProvider.';
}
accounts = await getAccounts(apiFqdn, value);
return true;
} catch(e) {
return 'Token does not have privileges to retrieve own Service Providers / Accounts, or network error occurred.';
}
}
}, { onCancel: () => process.exit(0) });

const btSplit = bearerToken.split('.')
if(btSplit.length === 3) {
const payload = JSON.parse(Buffer.from(btSplit[1], 'base64').toString('utf8'));
console.log('WARNING:', 'supplied token will expire in', payload.exp - (Date.now() / 1000) , 'second(s)');
console.log("You will need to regenerate it. You can do so via 'cognigy-vg set token' command.");
}

const { accountSid, serviceProviderSid } = await prompts([
{
type: 'select',
name: 'accountSid',
message: 'Account you will be working with',
choices: accounts.map(({ name, account_sid }) => ({
name: 'serviceProviderSid',
message: 'Service provider you will be working with',
choices: serviceProviders.map(({ service_provider_sid, name }) => ({
title: name,
value: account_sid
value: service_provider_sid
})),
initial: 0,
},
{
type: 'select',
name: 'serviceProviderSid',
message: 'Service provider you will be working with',
choices: serviceProviders.map(({ service_provider_sid, name }) => ({
name: 'accountSid',
message: 'Account you will be working with',
choices: accounts.map(({ name, account_sid }) => ({
title: name,
value: service_provider_sid
value: account_sid
})),
initial: 0,
},
Expand All @@ -130,7 +124,14 @@ export default async ({ AU }) => {

writeFileSync(
`./${fileName}`,
JSON.stringify({vgSpacePath, apiFqdn, bearerToken, accountSid, serviceProviderSid, snapEncryptionKey }, null, 2)
JSON.stringify({
vgSpacePath,
apiFqdn,
serviceProviderToken,
accountSid,
serviceProviderSid,
snapEncryptionKey
}, null, 2)
);

console.log(`Configuration file generated: ./${fileName}`);
Expand Down
55 changes: 0 additions & 55 deletions src/commands/set-token.ts

This file was deleted.

Loading

0 comments on commit 97d6d55

Please sign in to comment.