GUIDE

Build a reusable chat notification function

Updated September 09, 2026 By microfn 2 min read
notifications slack discord telegram

A dedicated notification function keeps provider credentials and request details in one place. Other functions pass a channel and message through @microfn/fn instead of copying integration code.

Create the notifier

This Slack example uses an incoming webhook URL stored as SLACK_WEBHOOK_URL:

import secret from "@microfn/secret";

type NotificationInput = { text?: string };
type FunctionInput = NotificationInput | { input: NotificationInput };

export async function main(input: FunctionInput) {
  const actualInput = "input" in input ? input.input : input;
  const text = actualInput.text?.trim();
  if (!text) throw new Error("text is required");

  const webhookUrl = await secret.getRequired("SLACK_WEBHOOK_URL");
  const response = await fetch(webhookUrl, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ text }),
  });

  if (!response.ok) {
    throw new Error(`Slack returned ${response.status}`);
  }

  return { delivered: true };
}

Deploy it, add the secret in the function’s Secrets panel, and send a test message.

Call it from another function

import fn from "@microfn/fn";

export async function main(input: { orderId: string }) {
  await fn.executeFunction("your-username/send-notification", {
    text: `Order ${input.orderId} needs review`,
  });

  return { notified: true };
}

Use the same boundary for Discord or Telegram, but keep each provider’s credential inside the notifier that needs it. The caller should pass message content and safe routing identifiers, never tokens.

Connect this tool to a GitHub webhook, a cron schedule, or an incoming email.

Related guides

All guides