• Blog
  • Docs
  • Pricing
  • We’re hiring!
Log inSign up
charmaine

charmaine

sendMessage

Public
Like
sendMessage
Home
Code
3
README.md
sendMessage.ts
H
telegram_sender.ts
Branches
2
Pull requests
Remixes
History
Environment variables
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.
Sign up now
Code
/
Code
/
Search
telegram_sender.ts
https://charmaine--6e78b52e425b11f0809a76b3cceeab13.web.val.run
README.md

Universal Message Sender

A unified function to send messages across multiple platforms including Discord, Slack, OneSignal, and Twilio.

Usage

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 });

Environment Variables

Discord

  • DISCORD_WEBHOOK_URL - Discord webhook URL (can also be passed in message body)

Slack

Choose one of these configurations:

  • SLACK_WEBHOOK_URL - Simple webhook URL (recommended for basic usage)
  • OR both:
    • SLACK_BOT_TOKEN - Bot token for advanced features
    • SLACK_CHANNEL_ID - Channel to post to

OneSignal

  • ONESIGNAL_TOKEN - OneSignal API token
  • SEGMENT_APPID - OneSignal app ID
  • DEFAULT_ONESIGNAL_SEGMENT - Default segment to send to (optional)

Twilio

  • TWILIO_ACCOUNT_SID - Twilio account SID
  • TWILIO_AUTH_TOKEN - Twilio auth token
  • TWILIO_FROM_NUMBER - Default from number (can be overridden in message body)

Adding New Platforms

To add a new platform:

  1. Add the platform name to the Platform type
  2. Create a new interface for the message body type
  3. Add the new interface to the MessageBody union type
  4. Add a new case to the switch statement in sendMessage
  5. 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);

Error Handling

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); }
Code
README.mdsendMessage.ts
H
telegram_sender.ts
FeaturesVersion controlCode intelligenceCLIMCP
Use cases
TeamsAI agentsSlackGTM
DocsShowcaseTemplatesNewestTrendingAPI examplesNPM packages
PricingNewsletterBlogAboutCareers
We’re hiring!
Brandhi@val.townStatus
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Open Source Pledge
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.