Serverless Code Commands - DailyBot Developers

Write and run serverless JavaScript code directly in DailyBot. Build custom commands without deploying your own infrastructure.

Serverless Code Commands

Serverless code commands let you write and execute JavaScript code directly within DailyBot — no server infrastructure required. Your code runs in DailyBot's secure runtime environment and can make HTTP requests, access persistent storage, and return interactive responses.

Running a Command

Serverless commands can be triggered in three ways:

  1. In-Chat Trigger — Users type the command directly in DMs or channels
  2. Workflow Trigger — Code runs in response to events or schedules via DailyBot Workflows
  3. Command Scheduler — Use the DailyBot SDK to schedule a future run of the command

Command Input

When a command is triggered, your code receives an event object with context about the execution.

Query String

Additional text typed after the command name is available as event.query. For example, typing /growth_rate July, August makes "July, August" available via event.query.

Context Variables

  • event.data.intent — The triggered command name
  • event.data.user_full_name — Name of the triggering user
  • event.data.targetChannel — Chat channel object where the command executed
Parsing command input
// Parse query string input
let query = event.query;
let month = null;
if (query) {
  let parts = query.split(" ");
  if (parts[0]) {
    month = parts[0];
  }
}

return `Generating report for ${month || 'this month'}...`;

Making HTTP Requests

The request variable is available in your code and wraps the superagent library for making HTTP requests.

HTTP requests with superagent
// GET request
const response = await request.get('https://api.example.com/data');
const data = response.body;

// POST request with JSON body
const response = await request
  .post('https://api.example.com/submit')
  .send({ name: 'DailyBot', type: 'automation' })
  .set('Authorization', 'Bearer your-token');

Returning Results

String Response

Return a simple string to display as the command response:

javascript
return "Hello world!";

JSON Response with Interactive Buttons

Return a JSON object with a message and interactive buttons:

Interactive button response
return {
  "message": "Last week sales increased by 15% 📈",
  "buttons": [
    {
      "label": "Last month",
      "label_after_click": "Getting sales from last month...",
      "value": "sales last month",
      "button_type": "Command"
    },
    {
      "label": "Last quarter",
      "label_after_click": "Getting quarterly report...",
      "value": "sales last quarter",
      "button_type": "Command"
    }
  ]
};

Tip

Button values can reference the same command with different query parameters, enabling multi-step interactive workflows from a single command.

DailyBot SDK

The DailyBotSDK object is available in your code and provides access to persistent key-value storage.

Initialization

SDK initialization
DailyBotSDK.setAPIKey("your_api_key");

Key-Value Storage

Store JSON objects in user-scoped keys. Data is associated with the user making the request.

Key-value storage operations
// Write data
await DailyBotSDK.Storage.write('preferences', {
  theme: 'dark',
  notifications: true
});

// Read data
let prefs = await DailyBotSDK.Storage.read('preferences');
// prefs = { theme: 'dark', notifications: true }

// Delete data
await DailyBotSDK.Storage.delete('preferences');

Examples

Meeting Room Booking

Meeting room booking command
// Usage: /book-room conference-a 2026-02-15 14:00
const parts = event.query.split(" ");
const roomName = parts[0];
const date = parts[1];
const time = parts[2];

if (!roomName || !date || !time) {
  return "Usage: /book-room <room-name> <date> <time>";
}

// Book the room via external API
const response = await request
  .post('https://rooms.company.com/api/book')
  .send({ room: roomName, date, time });

return `Booked ${roomName} on ${date} at ${time}. Confirmation: ${response.body.id}`;

Tech News Fetcher

Tech news fetcher command
// Fetch latest tech headlines
try {
  const response = await request
    .get('https://newsapi.org/v2/top-headlines')
    .query({ category: 'technology', pageSize: 5, apiKey: 'your_api_key' });

  const headlines = response.body.articles
    .map((a, i) => `${i + 1}. ${a.title}`)
    .join('\n');

  return `Today's Tech Headlines:\n${headlines}`;
} catch (error) {
  return "Could not fetch news. Please try again later.";
}

Advanced Patterns

  • Use SDK storage to build stateful workflows across multiple command invocations
  • Parse event.query to accept subcommands (e.g., /tool help, /tool status)
  • Chain commands using interactive buttons for multi-step processes
  • Handle errors gracefully and return helpful messages to users
  • Use the scheduler to set up recurring data fetches or reports