@telegraph-notify/backend-sdk
v1.0.2
Published
Telegraph Backend SDK
Downloads
6
Readme
Telegraph Node.js SDK
The Node.js SDK for Telegraph is used for sending notifications to users, and also for managing user-related data on the Telegraph database.
Table of Contents
- Quick Start
- Usage
- Send a Notification
- Generate Client HMAC
- Add User
- Edit User
- Delete User
- Get User
- Get All Users
- Get Notification Logs
Quick Start
Install the SDK from npm.
$ npm i @telegraph-notify/backend-sdkThen import the SDK for use in the application code.
import Telegraph from "@telegraph-notify/backend-sdk";
const telegraph = new Telegraph(secretKey, httpGateway);Enter your secretKey and httpGateway from the Telegraph CLI deployment.
Usage
All telegraph methods return a Promise that will resolve to the response.
Send a Notification
telegraph.send({
user_id,
channels: {
in_app?: {
message
},
email?: {
subject,
message,
},
slack?: {
message,
},
},
});user_id(string, required): The unique identifier of the user receiving the notification.channels.in_app.message(string, optional): The in_app message to be sent to the user.channels.email.subject(string, optional): The email subject to be sent to the user.channels.email.message(string, optional): The email message to be sent to the user.channels.slack.message(string, optional): The email message to be sent to the user.
Response:
- Success (200 OK): Returns a JSON object with the status code and response message.
- Example:
{ "status": 200, "body": [ { "channel": "in_app", "notification_id": "1865d4a8-94cb-4a4b-bfef-35942abadf18" }, { "channel": "email", "notification_id": "27149fcf-1e8d-47f7-b240-6c7db899f861" } ] }
- Example:
- Error (500 Internal Server Error): Returns an error message if the request fails.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Generate Client HMAC
telegraph.generateHMAC(user_id);user_id(string, required): The unique identifier of the user.
Response:
- Success: Returns the client HMAC as a string with base64 encoding.
- Example:
"BojdvcqEyK4CHwQ=";
- Example:
Add User
telegraph.addUser(id, name, email, slack);id(string, required): The unique identifier of the user.name(string, required): The full name of the user.email(string, required): The email address of the user.slack(string, optional): The Slack webhook of the user.
Response:
- Success (200 OK): Returns a JSON object with the status code and a success message.
- Example:
{ "status": 200, "body": "User added successfully" }
- Example:
- Error (500 Internal Server Error): Returns an error message if the request fails.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Edit User
telegraph.editUser(id, name, email, slack);id(string, required): The unique identifier of the user.name(string, required): The updated full name of the user.email(string, required): The updated email address of the user.slack(string, optional): The Slack webhook of the user.
Response:
- Success (200 OK): Returns a JSON object with the status code and a success message.
- Example:
{ "status": 200, "body": "User edited" }
- Example:
- Error (500 Internal Server Error): Returns an error message if the request fails.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Delete User
telegraph.deleteUser(id);id(string, required): The unique identifier of the user to be deleted.
Response:
- Success (200 OK): Returns a JSON object with the status code and a success message.
- Example:
{ "status": 200, "body": "User deleted successfully" }
- Example:
- Error (404 Not Found): Returns an error message if the user does not exist.
- Example:
{ "status": 404, "body": "User does not exist" }
- Example:
- Error (500 Internal Server Error): Returns an error message if the request fails.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Get User
telegraph.getUser(id);id(string, required): The unique identifier of the user to retrieve.
Response:
- Success (200 OK): Returns a JSON object containing the user's details.
- Example:
{ "status": 200, "body": { "id": "bob", "name": "bob smith", "email": "[email protected]", "last_seen": "2024-11-14T04:38:58.598Z", "created_at": "2024-11-14T04:38:55.278Z", "last_notified": "2024-11-14T04:52:26.662Z", "preferences": { "email": true, "in_app": true, "slack": false } } }
- Example:
- Error (404 Not Found): If the user is not found.
- Example:
{ "status": 404, "body": "User not found" }
- Example:
- Error (500 Internal Server Error): If there is an error with the request.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Get All Users
telegraph.getAllUsers();Response:
- Success (200 OK): Returns a JSON object with the status code and response message.
- Example:
{ "status": 200, "body": [ { "last_seen": "2024-11-14T04:38:58.598Z", "created_at": "2024-11-14T04:38:55.278Z", "last_notified": "2024-11-14T04:52:26.662Z", "email": "[email protected]", "id": "bob", "name": "bob", "preferences": { "email": true, "in_app": true, "slack": false } }, { "last_seen": "2024-11-14T04:30:55.046Z", "created_at": "2024-11-14T04:29:10.277Z", "last_notified": "2024-11-14T04:30:58.141Z", "email": "[email protected]", "id": "alice", "name": "alice", "preferences": { "email": true, "in_app": true, "slack": false } } ] }
- Example:
- Error (500 Internal Server Error): If there is an error with the request.
- Example:
{ "status": 500, "body": "Internal Server Error" }
- Example:
Get Notification Logs
telegraph.getNotificationLogs();Response:
- Success (200 OK): Returns an array of notification logs.
- Example:
[ { "user_id": "bob", "created_at": "2024-11-10T03:02:25.494Z", "status": "notification request received", "notification_id": "d19e2ff9-7dce-48ec-9368-f6344ac0e2d9", "log_id": "cc6a2f8c-3885-45d5-9800-a57f74dd9b16", "ttl": 1733799745, "message": "hi", "channel": "in_app" } ]
- Example:
- Error: If there is an error with the request.
- Example:
Error fetching notification logs: Error: HTTP error: 404
- Example:
