Public
Like
sendMessage
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
telegram_sender.ts
https://charmaine--6e78b52e425b11f0809a76b3cceeab13.web.val.run
A unified function to send messages across multiple platforms including Discord, Slack, OneSignal, and Twilio.
import { sendMessage } from "./sendMessage.ts";
// Send to Discord
await sendMessage('discord', {
content: "Hello from Discord!",
url: "https://discord.com/api/webhooks/your-webhook-url" // optional if DISCORD_WEBHOOK_URL is set
});
// Send to Slack
await sendMessage('slack', {
text: "Hello from Slack!",
blocks: [/* optional blocks */],
thread_ts: "optional-thread-timestamp"
});
// Send OneSignal notification
await sendMessage('onesignal', {
heading: "Important Update",
message: "This is a push notification",
url: "https://example.com", // optional
segment: ["All"] // optional, uses DEFAULT_ONESIGNAL_SEGMENT if not provided
});
// Send SMS via Twilio
await sendMessage('twilio', {
toNumber: "+1234567890",
message: "Hello via SMS!",
fromNumber: "+0987654321" // optional if TWILIO_FROM_NUMBER is set
});
DISCORD_WEBHOOK_URL- Discord webhook URL (can also be passed in message body)
Choose one of these configurations:
SLACK_WEBHOOK_URL- Simple webhook URL (recommended for basic usage)- OR both:
SLACK_BOT_TOKEN- Bot token for advanced featuresSLACK_CHANNEL_ID- Channel to post to
ONESIGNAL_TOKEN- OneSignal API tokenSEGMENT_APPID- OneSignal app IDDEFAULT_ONESIGNAL_SEGMENT- Default segment to send to (optional)
TWILIO_ACCOUNT_SID- Twilio account SIDTWILIO_AUTH_TOKEN- Twilio auth tokenTWILIO_FROM_NUMBER- Default from number (can be overridden in message body)
To add a new platform:
- Add the platform name to the
Platformtype - Create a new interface for the message body type
- Add the new interface to the
MessageBodyunion type - Add a new case to the switch statement in
sendMessage - Implement the platform-specific function
Example for adding email support:
interface EmailMessage {
to: string;
subject: string;
text?: string;
html?: string;
}
type MessageBody = DiscordMessage | SlackMessage | OneSignalMessage | TwilioMessage | EmailMessage;
type Platform = 'discord' | 'slack' | 'onesignal' | 'twilio' | 'email';
// Add to switch statement
case 'email':
return await sendEmailMessage(body as EmailMessage);
The function throws descriptive errors for:
- Missing environment variables
- Invalid platform names
- API errors from each platform
- Missing required parameters
Always wrap calls in try-catch blocks for production use:
try {
await sendMessage('discord', { content: "Hello!" });
} catch (error) {
console.error("Failed to send message:", error.message);
}