@listeningkit/telnyx 0.1.0
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/LICENSE +202 -0
- package/README.md +263 -0
- package/banner.png +0 -0
- package/client.ts +98 -0
- package/convex.config.ts +11 -0
- package/dist/_generated/api.d.ts +40 -0
- package/dist/_generated/api.d.ts.map +1 -0
- package/dist/_generated/api.js +31 -0
- package/dist/_generated/api.js.map +1 -0
- package/dist/_generated/component.d.ts +68 -0
- package/dist/_generated/component.d.ts.map +1 -0
- package/dist/_generated/component.js +11 -0
- package/dist/_generated/component.js.map +1 -0
- package/dist/_generated/dataModel.d.ts +46 -0
- package/dist/_generated/dataModel.d.ts.map +1 -0
- package/dist/_generated/dataModel.js +11 -0
- package/dist/_generated/dataModel.js.map +1 -0
- package/dist/_generated/server.d.ts +137 -0
- package/dist/_generated/server.d.ts.map +1 -0
- package/dist/_generated/server.js +80 -0
- package/dist/_generated/server.js.map +1 -0
- package/dist/client.d.ts +93 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +29 -0
- package/dist/client.js.map +1 -0
- package/dist/convex.config.d.ts +8 -0
- package/dist/convex.config.d.ts.map +1 -0
- package/dist/convex.config.js +11 -0
- package/dist/convex.config.js.map +1 -0
- package/dist/lib/telnyx.d.ts +63 -0
- package/dist/lib/telnyx.d.ts.map +1 -0
- package/dist/lib/telnyx.js +102 -0
- package/dist/lib/telnyx.js.map +1 -0
- package/dist/schema.d.ts +38 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +24 -0
- package/dist/schema.js.map +1 -0
- package/dist/telnyx.d.ts +71 -0
- package/dist/telnyx.d.ts.map +1 -0
- package/dist/telnyx.js +170 -0
- package/dist/telnyx.js.map +1 -0
- package/dist/test.d.ts +60 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +28 -0
- package/dist/test.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/lib/telnyx.ts +143 -0
- package/package.json +104 -0
- package/schema.ts +25 -0
- package/telnyx.ts +182 -0
- package/test.ts +35 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="./banner.png" alt="@listeningkit/telnyx — Convex component for Telnyx" width="100%" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# @listeningkit/telnyx (Convex Telnyx Component)
|
|
6
|
+
|
|
7
|
+
> A production-ready [Convex Component](https://docs.convex.dev/components) for [Telnyx](https://telnyx.com) messaging: send SMS from a Convex action, verify Ed25519-signed webhooks, and keep every send and every event idempotent by default.
|
|
8
|
+
|
|
9
|
+
[](https://docs.convex.dev/components)
|
|
10
|
+
[](https://www.npmjs.com/package/@listeningkit/telnyx)
|
|
11
|
+
[](https://opensource.org/licenses/Apache-2.0)
|
|
12
|
+
|
|
13
|
+
| | |
|
|
14
|
+
|---|---|
|
|
15
|
+
| **Provider** | [Telnyx](https://telnyx.com) messaging API (`POST /v2/messages`) |
|
|
16
|
+
| **Component** | `@listeningkit/telnyx` on [npm](https://www.npmjs.com/package/@listeningkit/telnyx) |
|
|
17
|
+
| **Source** | [github.com/matthewdonsemail-lab/convex-telnyx](https://github.com/matthewdonsemail-lab/convex-telnyx) |
|
|
18
|
+
| **Auth model** | Your app authenticates; the component receives an `owner` string |
|
|
19
|
+
| **License** | Apache-2.0 |
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Overview
|
|
24
|
+
|
|
25
|
+
Talking to Telnyx from a Convex backend means handling three things well: keeping the API key off the client, verifying webhook signatures correctly against the **raw** request body, and not double-sending or double-processing when Telnyx retries.
|
|
26
|
+
|
|
27
|
+
`@listeningkit/telnyx` does all three inside a Convex component:
|
|
28
|
+
|
|
29
|
+
- **Secrets stay server-side.** The API key, public key, and default sender number are declared in the component's environment, so callers never pass a credential as an argument.
|
|
30
|
+
- **Webhooks are verified properly.** Ed25519 verification over `<telnyx-timestamp>|<raw body>`, with a five-minute replay window.
|
|
31
|
+
- **Everything is idempotent.** Sends are keyed by Telnyx message id, webhook events by Telnyx event id, so retries and replays never duplicate rows.
|
|
32
|
+
- **The data model is isolated.** Messages and events live in the component's own schema partition and never pollute your app's tables.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Convex Component Architecture
|
|
37
|
+
|
|
38
|
+
This package follows the [Convex Component Specification](https://docs.convex.dev/components/authoring):
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
telnyx/
|
|
42
|
+
├── banner.png # Visual component header banner
|
|
43
|
+
├── convex.config.ts # Component definition & typed environment variables
|
|
44
|
+
├── schema.ts # Encapsulated schema (messages, webhookEvents)
|
|
45
|
+
├── telnyx.ts # Component actions, mutations, and queries
|
|
46
|
+
├── client.ts # Type-safe client wrapper for host applications
|
|
47
|
+
├── test.ts # convex-test registration helper
|
|
48
|
+
├── lib/
|
|
49
|
+
│ └── telnyx.ts # Pure helpers (E.164 validation, send, Ed25519 verify)
|
|
50
|
+
├── _generated/ # Generated component API bindings
|
|
51
|
+
├── dist/ # Compiled JavaScript and TypeScript declarations
|
|
52
|
+
└── package.json # Component exports, peerDependencies, and scripts
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### 1. Sandboxed Database Isolation
|
|
56
|
+
|
|
57
|
+
The component owns its schema. `messages` and `webhookEvents` live inside the component's isolated partition, so they cannot collide with your application's tables and are not visible to your app's clients.
|
|
58
|
+
|
|
59
|
+
### 2. Typed Environment Injection
|
|
60
|
+
|
|
61
|
+
Environment variables are declared in [`convex.config.ts`](./convex.config.ts):
|
|
62
|
+
|
|
63
|
+
| Variable | Required | Purpose |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `TELNYX_API_KEY` | yes | Telnyx v2 API key, sent as a bearer token |
|
|
66
|
+
| `TELNYX_PUBLIC_KEY` | for webhooks | Base64 Ed25519 public key from the Telnyx portal |
|
|
67
|
+
| `TELNYX_FROM_NUMBER` | yes, unless passed per call | Default E.164 sender number |
|
|
68
|
+
| `TELNYX_API_BASE_URL` | no | Override the API base (defaults to `https://api.telnyx.com/v2`) |
|
|
69
|
+
|
|
70
|
+
All are declared optional so installing the package never blocks codegen; actions fail with a clear error when a required one is missing.
|
|
71
|
+
|
|
72
|
+
### 3. Auth Lives in Your App
|
|
73
|
+
|
|
74
|
+
Convex components have no `ctx.auth`. Your app authenticates the caller and passes the resulting `owner` string, which the component uses to scope reads. This component never sees your auth provider — Clerk, Auth0, or your own session logic all work the same way.
|
|
75
|
+
|
|
76
|
+
### 4. Ergonomic Client Wrapper
|
|
77
|
+
|
|
78
|
+
Rather than assembling `ctx.runAction(components.telnyx.telnyx.sendSms, ...)` by hand, the package exports a typed `Telnyx` client whose constructor takes the generated `ComponentApi`, so it can never drift from the component's real signatures.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Installation & Setup
|
|
83
|
+
|
|
84
|
+
### 1. Install the package
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
# npm
|
|
88
|
+
npm install @listeningkit/telnyx
|
|
89
|
+
|
|
90
|
+
# pnpm
|
|
91
|
+
pnpm add @listeningkit/telnyx
|
|
92
|
+
|
|
93
|
+
# yarn
|
|
94
|
+
yarn add @listeningkit/telnyx
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
*(Ensure `convex` `>=1.45.0` is installed as a peer dependency.)*
|
|
98
|
+
|
|
99
|
+
### 2. Register the component
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
// convex/convex.config.ts
|
|
103
|
+
import { defineApp } from "convex/server";
|
|
104
|
+
import telnyx from "@listeningkit/telnyx/convex.config";
|
|
105
|
+
|
|
106
|
+
const app = defineApp();
|
|
107
|
+
app.use(telnyx);
|
|
108
|
+
|
|
109
|
+
export default app;
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### 3. Configure environment variables
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
npx convex env set TELNYX_API_KEY="your-telnyx-api-key"
|
|
116
|
+
npx convex env set TELNYX_PUBLIC_KEY="base64-ed25519-public-key"
|
|
117
|
+
npx convex env set TELNYX_FROM_NUMBER="+15550000001"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 4. Regenerate types
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
npx convex dev --once
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## Usage
|
|
129
|
+
|
|
130
|
+
### Send an SMS from an authenticated action
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
// convex/messages.ts
|
|
134
|
+
import { v } from "convex/values";
|
|
135
|
+
import { action } from "./_generated/server";
|
|
136
|
+
import { components } from "./_generated/api";
|
|
137
|
+
import { Telnyx } from "@listeningkit/telnyx";
|
|
138
|
+
|
|
139
|
+
const telnyx = new Telnyx(components.telnyx);
|
|
140
|
+
|
|
141
|
+
export const sendText = action({
|
|
142
|
+
args: { to: v.string(), text: v.string() },
|
|
143
|
+
handler: async (ctx, args) => {
|
|
144
|
+
// 1. Authenticate in your app — the component has no ctx.auth.
|
|
145
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
146
|
+
if (!identity) throw new Error("Unauthorized");
|
|
147
|
+
|
|
148
|
+
// 2. Send. The API key and default sender come from the component env.
|
|
149
|
+
return await telnyx.sendSms(ctx, {
|
|
150
|
+
owner: identity.subject,
|
|
151
|
+
to: args.to,
|
|
152
|
+
text: args.text,
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Numbers are validated as E.164 **before** any network call, so a malformed recipient never costs an API request.
|
|
159
|
+
|
|
160
|
+
### Receive and verify webhooks
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
// convex/http.ts
|
|
164
|
+
import { httpRouter } from "convex/server";
|
|
165
|
+
import { httpAction } from "./_generated/server";
|
|
166
|
+
import { components } from "./_generated/api";
|
|
167
|
+
import { Telnyx } from "@listeningkit/telnyx";
|
|
168
|
+
|
|
169
|
+
const telnyx = new Telnyx(components.telnyx);
|
|
170
|
+
const http = httpRouter();
|
|
171
|
+
|
|
172
|
+
http.route({
|
|
173
|
+
path: "/telnyx/webhook",
|
|
174
|
+
method: "POST",
|
|
175
|
+
handler: httpAction(async (ctx, request) => {
|
|
176
|
+
// Verification needs the exact bytes Telnyx signed.
|
|
177
|
+
const rawBody = await request.text();
|
|
178
|
+
const event = await telnyx.verifyWebhook(ctx, {
|
|
179
|
+
rawBody,
|
|
180
|
+
headers: Object.fromEntries(request.headers.entries()),
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (!event) return new Response("invalid Telnyx signature", { status: 401 });
|
|
184
|
+
if (event.event_type === "message.received") {
|
|
185
|
+
// event.payload carries id, direction, from, to, and text.
|
|
186
|
+
console.log("inbound SMS", event.payload);
|
|
187
|
+
}
|
|
188
|
+
return Response.json({ ok: true });
|
|
189
|
+
}),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
export default http;
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
`verifyWebhook` returns `null` — never throws — for a bad signature, a stale timestamp, or an unparseable body, so you can answer `401` and let Telnyx retry. Verified events are recorded once, keyed by Telnyx event id.
|
|
196
|
+
|
|
197
|
+
### Read the component's records
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
import { query } from "./_generated/server";
|
|
201
|
+
import { components } from "./_generated/api";
|
|
202
|
+
import { Telnyx } from "@listeningkit/telnyx";
|
|
203
|
+
|
|
204
|
+
const telnyx = new Telnyx(components.telnyx);
|
|
205
|
+
|
|
206
|
+
export const recentMessages = query({
|
|
207
|
+
args: {},
|
|
208
|
+
handler: async (ctx) => {
|
|
209
|
+
const identity = await ctx.auth.getUserIdentity();
|
|
210
|
+
if (!identity) throw new Error("Unauthorized");
|
|
211
|
+
return await telnyx.listMessages(ctx, { owner: identity.subject, limit: 20 });
|
|
212
|
+
},
|
|
213
|
+
});
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Production Guardrails
|
|
219
|
+
|
|
220
|
+
| Guardrail | Mechanism | Benefit |
|
|
221
|
+
|---|---|---|
|
|
222
|
+
| **Server-side secrets** | Component `env` only | The API key is never a function argument, so it cannot leak through client calls or logs |
|
|
223
|
+
| **E.164 validation** | `lib/telnyx.ts` before fetch | Invalid numbers fail locally instead of burning an API request |
|
|
224
|
+
| **Signature verification** | Ed25519 over `<timestamp>\|<raw body>` | Forged or modified webhooks are rejected |
|
|
225
|
+
| **Replay window** | Five-minute timestamp tolerance | A captured webhook cannot be replayed later |
|
|
226
|
+
| **Idempotent sends** | `messages` indexed by `providerMessageId` | A retried send never double-logs |
|
|
227
|
+
| **Idempotent events** | `webhookEvents` indexed by `eventId` | Telnyx retries are free no-ops |
|
|
228
|
+
| **Bounded upstream** | `AbortSignal.timeout(15_000)` | A hung Telnyx call cannot consume the action budget |
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## Component Development & Publishing
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# install
|
|
236
|
+
pnpm install
|
|
237
|
+
|
|
238
|
+
# generate component bindings (run before building so dist/ is not scanned)
|
|
239
|
+
pnpm run build:codegen
|
|
240
|
+
|
|
241
|
+
# compile declarations and JavaScript
|
|
242
|
+
pnpm run build
|
|
243
|
+
|
|
244
|
+
# type check and test
|
|
245
|
+
pnpm run typecheck
|
|
246
|
+
pnpm test
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
> **Note:** run `build:codegen` before `build`. Convex scans the component directory for modules, so a stale `dist/` would be analyzed as if it were source.
|
|
250
|
+
|
|
251
|
+
### Testing your integration with `convex-test`
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import { convexTest } from "convex-test";
|
|
255
|
+
import { modules } from "@listeningkit/telnyx/test";
|
|
256
|
+
import schema from "./schema.js";
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
Apache 2.0
|
package/banner.png
ADDED
|
Binary file
|
package/client.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { GenericActionCtx, GenericQueryCtx } from "convex/server";
|
|
2
|
+
import type { ComponentApi } from "./_generated/component.js";
|
|
3
|
+
|
|
4
|
+
export type TelnyxMessageRow = {
|
|
5
|
+
_id: string;
|
|
6
|
+
_creationTime: number;
|
|
7
|
+
owner: string;
|
|
8
|
+
providerMessageId: string;
|
|
9
|
+
to: string;
|
|
10
|
+
text: string;
|
|
11
|
+
status: string;
|
|
12
|
+
createdAt: number;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type TelnyxWebhookEventRow = {
|
|
16
|
+
_id: string;
|
|
17
|
+
_creationTime: number;
|
|
18
|
+
eventId: string;
|
|
19
|
+
eventType: string;
|
|
20
|
+
payload: unknown;
|
|
21
|
+
occurredAt?: string;
|
|
22
|
+
receivedAt: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type TelnyxWebhookEvent = {
|
|
26
|
+
id: string;
|
|
27
|
+
event_type: string;
|
|
28
|
+
occurred_at?: string;
|
|
29
|
+
payload: unknown;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type TelnyxSendResult = { id: string; status: string };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The component API as seen by a host application.
|
|
36
|
+
*
|
|
37
|
+
* Aliased to the generated `ComponentApi` so the client can never drift from
|
|
38
|
+
* the component's real function signatures.
|
|
39
|
+
*/
|
|
40
|
+
export type TelnyxComponentApi = ComponentApi<"telnyx">;
|
|
41
|
+
|
|
42
|
+
export interface TelnyxSendArgs {
|
|
43
|
+
/**
|
|
44
|
+
* Owner identifier from your host application (e.g. Clerk `identity.subject`).
|
|
45
|
+
* Used to scope the component's internal `messages` table to a tenant.
|
|
46
|
+
*/
|
|
47
|
+
owner: string;
|
|
48
|
+
/**
|
|
49
|
+
* Sender number in E.164 format. Defaults to the component's `TELNYX_FROM_NUMBER`.
|
|
50
|
+
*/
|
|
51
|
+
from?: string;
|
|
52
|
+
/** Recipient number in E.164 format. */
|
|
53
|
+
to: string;
|
|
54
|
+
/** Message body. Must be non-empty after trimming. */
|
|
55
|
+
text: string;
|
|
56
|
+
/** Optional per-message status callback URL. */
|
|
57
|
+
webhookUrl?: string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface TelnyxVerifyWebhookArgs {
|
|
61
|
+
/** Raw, unparsed request body exactly as received from Telnyx. */
|
|
62
|
+
rawBody: string;
|
|
63
|
+
/** Lower-cased request headers, including `telnyx-signature-ed25519` and `telnyx-timestamp`. */
|
|
64
|
+
headers: Record<string, string>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class Telnyx {
|
|
68
|
+
constructor(public readonly component: TelnyxComponentApi) {}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Send an SMS through `POST /v2/messages`, then record it in the component's
|
|
72
|
+
* isolated `messages` table keyed by the Telnyx message id.
|
|
73
|
+
*/
|
|
74
|
+
sendSms(ctx: { runAction: GenericActionCtx<any>["runAction"] }, args: TelnyxSendArgs) {
|
|
75
|
+
return ctx.runAction(this.component.telnyx.sendSms, args) as Promise<TelnyxSendResult>;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Verify an Ed25519-signed Telnyx webhook against the raw body and record the
|
|
80
|
+
* event once. Returns `null` when the signature is invalid or replayed.
|
|
81
|
+
*/
|
|
82
|
+
verifyWebhook(ctx: { runAction: GenericActionCtx<any>["runAction"] }, args: TelnyxVerifyWebhookArgs) {
|
|
83
|
+
return ctx.runAction(this.component.telnyx.verifyWebhook, args) as Promise<TelnyxWebhookEvent | null>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** List the most recent messages recorded for an owner. */
|
|
87
|
+
listMessages(
|
|
88
|
+
ctx: { runQuery: GenericQueryCtx<any>["runQuery"] },
|
|
89
|
+
args: { owner: string; limit?: number },
|
|
90
|
+
) {
|
|
91
|
+
return ctx.runQuery(this.component.telnyx.listMessages, args) as Promise<TelnyxMessageRow[]>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** List the most recently received webhook events. */
|
|
95
|
+
listWebhookEvents(ctx: { runQuery: GenericQueryCtx<any>["runQuery"] }, args: { limit?: number } = {}) {
|
|
96
|
+
return ctx.runQuery(this.component.telnyx.listWebhookEvents, args) as Promise<TelnyxWebhookEventRow[]>;
|
|
97
|
+
}
|
|
98
|
+
}
|
package/convex.config.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { defineComponent } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
export default defineComponent("telnyx", {
|
|
5
|
+
env: {
|
|
6
|
+
TELNYX_API_KEY: v.optional(v.string()),
|
|
7
|
+
TELNYX_PUBLIC_KEY: v.optional(v.string()),
|
|
8
|
+
TELNYX_FROM_NUMBER: v.optional(v.string()),
|
|
9
|
+
TELNYX_API_BASE_URL: v.optional(v.string()),
|
|
10
|
+
},
|
|
11
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated `api` utility.
|
|
3
|
+
*
|
|
4
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
5
|
+
*
|
|
6
|
+
* To regenerate, run `npx convex dev`.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type * as client from "../client.js";
|
|
10
|
+
import type * as lib_telnyx from "../lib/telnyx.js";
|
|
11
|
+
import type * as telnyx from "../telnyx.js";
|
|
12
|
+
import type * as test from "../test.js";
|
|
13
|
+
import type { ApiFromModules, FilterApi, FunctionReference } from "convex/server";
|
|
14
|
+
declare const fullApi: ApiFromModules<{
|
|
15
|
+
client: typeof client;
|
|
16
|
+
"lib/telnyx": typeof lib_telnyx;
|
|
17
|
+
telnyx: typeof telnyx;
|
|
18
|
+
test: typeof test;
|
|
19
|
+
}>;
|
|
20
|
+
/**
|
|
21
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
22
|
+
*
|
|
23
|
+
* Usage:
|
|
24
|
+
* ```js
|
|
25
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const api: FilterApi<typeof fullApi, FunctionReference<any, "public">>;
|
|
29
|
+
/**
|
|
30
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
31
|
+
*
|
|
32
|
+
* Usage:
|
|
33
|
+
* ```js
|
|
34
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const internal: FilterApi<typeof fullApi, FunctionReference<any, "internal">>;
|
|
38
|
+
export declare const components: {};
|
|
39
|
+
export {};
|
|
40
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../_generated/api.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAEH,OAAO,KAAK,KAAK,MAAM,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,KAAK,UAAU,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,KAAK,MAAM,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,KAAK,IAAI,MAAM,YAAY,CAAC;AAExC,OAAO,KAAK,EACV,cAAc,EACd,SAAS,EACT,iBAAiB,EAClB,MAAM,eAAe,CAAC;AAGvB,QAAA,MAAM,OAAO,EAAE,cAAc,CAAC;IAC5B,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,YAAY,EAAE,OAAO,UAAU,CAAC;IAChC,MAAM,EAAE,OAAO,MAAM,CAAC;IACtB,IAAI,EAAE,OAAO,IAAI,CAAC;CACnB,CAAiB,CAAC;AAEnB;;;;;;;GAOG;AACH,eAAO,MAAM,GAAG,EAAE,SAAS,CACzB,OAAO,OAAO,EACd,iBAAiB,CAAC,GAAG,EAAE,QAAQ,CAAC,CACjB,CAAC;AAElB;;;;;;;GAOG;AACH,eAAO,MAAM,QAAQ,EAAE,SAAS,CAC9B,OAAO,OAAO,EACd,iBAAiB,CAAC,GAAG,EAAE,UAAU,CAAC,CACnB,CAAC;AAElB,eAAO,MAAM,UAAU,EAAqC,EAAE,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
11
|
+
const fullApi = anyApi;
|
|
12
|
+
/**
|
|
13
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
14
|
+
*
|
|
15
|
+
* Usage:
|
|
16
|
+
* ```js
|
|
17
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export const api = anyApi;
|
|
21
|
+
/**
|
|
22
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
23
|
+
*
|
|
24
|
+
* Usage:
|
|
25
|
+
* ```js
|
|
26
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export const internal = anyApi;
|
|
30
|
+
export const components = componentsGeneric();
|
|
31
|
+
//# sourceMappingURL=api.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../_generated/api.ts"],"names":[],"mappings":"AAAA,oBAAoB;AACpB;;;;;;;GAOG;AAYH,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAE1D,MAAM,OAAO,GAKR,MAAa,CAAC;AAEnB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,GAAG,GAGZ,MAAa,CAAC;AAElB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,QAAQ,GAGjB,MAAa,CAAC;AAElB,MAAM,CAAC,MAAM,UAAU,GAAG,iBAAiB,EAAmB,CAAC"}
|