@sinch/functions-runtime 0.1.0-beta.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +207 -0
- package/dist/bin/sinch-runtime.js +812 -0
- package/dist/bin/sinch-runtime.js.map +1 -0
- package/dist/index.d.ts +1359 -0
- package/dist/index.js +1946 -0
- package/dist/index.js.map +1 -0
- package/package.json +80 -0
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# @sinch/functions-runtime
|
|
2
|
+
|
|
3
|
+
Development runtime for Sinch Functions - build serverless voice applications with ease.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @sinch/functions-runtime
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
Create a `function.js` file:
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { IceSvamlBuilder, MenuBuilder, MenuTemplates } from '@sinch/functions-runtime';
|
|
17
|
+
|
|
18
|
+
// Handle incoming calls
|
|
19
|
+
export async function ice(context, event) {
|
|
20
|
+
return new IceSvamlBuilder()
|
|
21
|
+
.say('Welcome to our service! Press 1 for sales, 2 for support.')
|
|
22
|
+
.runMenu(MenuTemplates.business('Acme Corp').menus)
|
|
23
|
+
.build();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Handle menu selections
|
|
27
|
+
export async function pie(context, event) {
|
|
28
|
+
const selection = event.menuResult?.value;
|
|
29
|
+
|
|
30
|
+
if (selection === 'sales') {
|
|
31
|
+
return new IceSvamlBuilder()
|
|
32
|
+
.say('Connecting you to sales.')
|
|
33
|
+
.connectPstn('+15551234567')
|
|
34
|
+
.build();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return new IceSvamlBuilder()
|
|
38
|
+
.say('Goodbye!')
|
|
39
|
+
.hangup()
|
|
40
|
+
.build();
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Run locally:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npx sinch-runtime
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Features
|
|
51
|
+
|
|
52
|
+
### SVAML Builders
|
|
53
|
+
|
|
54
|
+
Build voice responses with a fluent API:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
import { IceSvamlBuilder, AceSvamlBuilder, PieSvamlBuilder } from '@sinch/functions-runtime';
|
|
58
|
+
|
|
59
|
+
// ICE (Incoming Call Event) - handle incoming calls
|
|
60
|
+
const iceResponse = new IceSvamlBuilder()
|
|
61
|
+
.say('Hello, welcome!')
|
|
62
|
+
.connectPstn('+15551234567', {
|
|
63
|
+
cli: '+15559999999',
|
|
64
|
+
enableAce: true,
|
|
65
|
+
enableDice: true
|
|
66
|
+
})
|
|
67
|
+
.build();
|
|
68
|
+
|
|
69
|
+
// ACE (Answered Call Event) - handle answered outbound calls
|
|
70
|
+
const aceResponse = new AceSvamlBuilder()
|
|
71
|
+
.say('The call has been answered.')
|
|
72
|
+
.continue()
|
|
73
|
+
.build();
|
|
74
|
+
|
|
75
|
+
// PIE (Prompt Input Event) - handle user input
|
|
76
|
+
const pieResponse = new PieSvamlBuilder()
|
|
77
|
+
.say('You pressed ' + selection)
|
|
78
|
+
.hangup()
|
|
79
|
+
.build();
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Menu Builder
|
|
83
|
+
|
|
84
|
+
Create IVR menus with validation:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { MenuBuilder, MenuTemplates, createMenu } from '@sinch/functions-runtime';
|
|
88
|
+
|
|
89
|
+
// Use pre-built templates
|
|
90
|
+
const businessMenu = MenuTemplates.business('Acme Corp');
|
|
91
|
+
const yesNoMenu = MenuTemplates.yesNo('Do you want to continue?');
|
|
92
|
+
|
|
93
|
+
// Or build custom menus
|
|
94
|
+
const customMenu = new MenuBuilder()
|
|
95
|
+
.prompt('Press 1 for English, 2 for Spanish')
|
|
96
|
+
.option('1', 'menu(english)')
|
|
97
|
+
.option('2', 'menu(spanish)')
|
|
98
|
+
.addSubmenu('english')
|
|
99
|
+
.prompt('Press 1 for sales, 2 for support')
|
|
100
|
+
.option('1', 'return(en-sales)')
|
|
101
|
+
.option('2', 'return(en-support)')
|
|
102
|
+
.build();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Cache
|
|
106
|
+
|
|
107
|
+
Store and retrieve data across function invocations:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
import { LocalCache } from '@sinch/functions-runtime';
|
|
111
|
+
|
|
112
|
+
// In your function
|
|
113
|
+
export async function ice(context, event) {
|
|
114
|
+
const cache = context.cache;
|
|
115
|
+
|
|
116
|
+
// Store data
|
|
117
|
+
await cache.set('call-count', 1, 3600); // TTL in seconds
|
|
118
|
+
|
|
119
|
+
// Retrieve data
|
|
120
|
+
const count = await cache.get('call-count');
|
|
121
|
+
|
|
122
|
+
// Check existence
|
|
123
|
+
if (await cache.has('call-count')) {
|
|
124
|
+
// ...
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Configuration
|
|
130
|
+
|
|
131
|
+
Access environment variables and secrets:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
import { createConfig } from '@sinch/functions-runtime';
|
|
135
|
+
|
|
136
|
+
export async function ice(context, event) {
|
|
137
|
+
const config = createConfig(context);
|
|
138
|
+
|
|
139
|
+
// Get variables
|
|
140
|
+
const companyName = config.getVariable('COMPANY_NAME', 'Default');
|
|
141
|
+
|
|
142
|
+
// Get secrets
|
|
143
|
+
const apiKey = config.getSecret('API_KEY');
|
|
144
|
+
|
|
145
|
+
// Require variables (throws if missing)
|
|
146
|
+
const required = config.requireVariable('IMPORTANT_VAR');
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Voice Callbacks
|
|
151
|
+
|
|
152
|
+
| Callback | Description | Returns |
|
|
153
|
+
|----------|-------------|---------|
|
|
154
|
+
| `ice` | Incoming Call Event - first callback for incoming calls | SVAML |
|
|
155
|
+
| `ace` | Answered Call Event - when outbound call is answered | SVAML |
|
|
156
|
+
| `pie` | Prompt Input Event - user input (DTMF/voice) | SVAML |
|
|
157
|
+
| `dice` | Disconnected Call Event - call ended | None |
|
|
158
|
+
| `notify` | Notification events | None |
|
|
159
|
+
|
|
160
|
+
## TypeScript Support
|
|
161
|
+
|
|
162
|
+
Full TypeScript support with comprehensive types:
|
|
163
|
+
|
|
164
|
+
```typescript
|
|
165
|
+
import type {
|
|
166
|
+
FunctionContext,
|
|
167
|
+
IceCallbackData,
|
|
168
|
+
AceCallbackData,
|
|
169
|
+
PieCallbackData,
|
|
170
|
+
SvamletResponse
|
|
171
|
+
} from '@sinch/functions-runtime';
|
|
172
|
+
|
|
173
|
+
export async function ice(
|
|
174
|
+
context: FunctionContext,
|
|
175
|
+
event: IceCallbackData
|
|
176
|
+
): Promise<SvamletResponse> {
|
|
177
|
+
// Full type safety and IntelliSense!
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## API Reference
|
|
182
|
+
|
|
183
|
+
### Builders
|
|
184
|
+
|
|
185
|
+
- `IceSvamlBuilder` - Build ICE responses
|
|
186
|
+
- `AceSvamlBuilder` - Build ACE responses
|
|
187
|
+
- `PieSvamlBuilder` - Build PIE responses
|
|
188
|
+
- `MenuBuilder` - Build IVR menus
|
|
189
|
+
- `MenuTemplates` - Pre-built menu templates
|
|
190
|
+
|
|
191
|
+
### Cache
|
|
192
|
+
|
|
193
|
+
- `IFunctionCache` - Cache interface
|
|
194
|
+
- `LocalCache` - In-memory cache (development)
|
|
195
|
+
|
|
196
|
+
### Configuration
|
|
197
|
+
|
|
198
|
+
- `UniversalConfig` - Configuration utility
|
|
199
|
+
- `createConfig()` - Create config instance
|
|
200
|
+
|
|
201
|
+
### Types
|
|
202
|
+
|
|
203
|
+
All callback data types, SVAML types, and handler types are exported for TypeScript users.
|
|
204
|
+
|
|
205
|
+
## License
|
|
206
|
+
|
|
207
|
+
MIT
|