Serverless Code Commands

This feature allows developers to write, host, and run in-chat commands that have custom logic/code. The code is is hosted and managed directly within DailyBot to be used in chat platforms such as Slack, MS Teams, G Chat, Discord, and Telegram. This feature is ideal for automating workflows, generating reports, interacting with APIs, and enhancing team productivity through chatbots.

Key Concepts

  1. Serverless Execution: The code is hosted and run by DailyBot in a serverless environment, ensuring scalability and ease of maintenance.
  2. Triggering Commands: The code executes in response to chat commands or scheduled events.
  3. Dynamic Responses: The program can return text messages or JSON objects with interactive elements like buttons for chat.
  4. Event Data and Parameters: Commands context can include parameters for dynamic execution.
  5. HTTP Requests: The Code supports HTTP requests to external APIs or URLs.
  6. DailyBotSDK: We provide a convenient interface for storage operations, scheduling tasks, and sending messages.

Running a command

  • In-Chat Command Trigger: Initiate code execution via chat commands. On DM or channels.
  • Workflow Trigger: Run the code in response to specific events or schedules via DailyBot Workflows.
  • Command Scheduler: Use the DailyBot SDK to schedule a future run of a command.

Command input

A user can simply run the command by typing the command intent, users can also type the intent and additional words.

Command intent query string

The additional words and query string are captured and can be accessed in the event.query variable.

For example, your command name is "growth_rate", and a user types "growth_rate July, August". In this case, the event.query will have the string "July, August".

Capture user input and context:

  
  let query = event.query;
  let month = null;

  if (query) {
      let parts = query.split(" ");
      if (parts[0]) {
          month = parts[0];
      }
  }
  

Context variables for the command

event.data.intent: The actual command name being triggered
event.data.user_full_name: The name of user triggering the command
event.data.targetChannel: The chat channel object where the command was run

Making HTTP requests to internet services

Utilize request for API interactions. The request variable is a wrapper of the JS superagent library:

  
  request.get('https://api.example.com/data')
  

Returning the result of the command

Your command can respond with a string:

  
  return "Hello world!"
  

Or your command can return with a JSON special object that provides text responses and interactive buttons:

  
  return {
    "message": "Hi [user] — last week sales increased by [percentage]% 📈",
    "buttons": [
      {
        "label": "Last month",
        "label_after_click": "Getting sales from last month...",
        "value": "sales last month",
        "button_type": "Command"
      }
      // Additional buttons...
    ]
  }
  

When you utilize the JSON response with interactive buttons, the "value" can be a reference to the same command intent with different query parameters. This way you can build more complex flows using one single code command.

DailyBotSDK

We provide a simple SDK that you can use to interact with the DailyBot platform.

Import and Initialization

  
  DailyBotSDK.setAPIKey("your_key")
  

Key-value storage

You can store JSON {} objects in item keys that are associated with the user making the request/interacting with the chat command.

  • Write: Store data associated with a key.
  • Read: Retrieve data by key.
  • Delete: Remove data by key.
  
    // Writing data to the storage
  await DailyBotSDK.Storage.write('item_key', data);

  // Reading the data back from the storage
  let data = await DailyBotSDK.Storage.read('item_key');

  // Deleting the data from the storage
  await DailyBotSDK.Storage.delete('item_key');
  

Example

Booking a room

Users can book a meeting room by typing book_room [room_name] [date] [time]:

  
  async function bookMeetingRoom(roomName, date, time) {
    // Code to book a meeting room in the internal system
  }

  // Splitting the event query into parts
  const parts = event.query.split(" ");
  const roomName = parts[0];
  const date = parts[1];
  const time = parts[2];

  // Booking the meeting room with provided details
  await bookMeetingRoom(roomName, date, time);

  // Returning confirmation message
  return `Booked ${roomName} on ${date} at ${time}.`;
  

Tech news update

Users can get the latest tech news by typing tech_news:

  
  // Asynchronous function to fetch technology news
  async function getTechNews() {
    try {
      const response = await request.get('https://newsapi.org/v2/top-headlines?category=technology&apiKey=your_api_key');
      return response.body.articles;
    } catch (error) {
      throw error;
    }
  }

  // Fetching the technology news articles
  const newsArticles = await getTechNews();

  // Converting the news articles to a JSON string for output
  return JSON.stringify(newsArticles);
  

Need support?
Contact Us