01/09/2026

Primeros pasos con Microsoft Bot Framework

Microsoft Bot Framework
MIcrosoft Bot Framework

En este tutorial, vamos a aprender a crear un bot conversacional sencillo utilizando Microsoft Bot Framework. Para ello, instalaremos el emulador Bot Framework Emulator y desarrollaremos una API en NodeJS a la que se conectará el emulador para intercambiar mensajes. La página oficial de Microsoft Bot Framework se encuentra en:

https://dev.botframework.com/

Leer más: Primeros pasos con Microsoft Bot Framework

¿Qué es?

Microsoft Bot Framework es un conjunto de herramientas y servicios de Microsoft para crear, probar y desplegar bots conversacionales. Permite integrar bots con múltiples canales como Teams, Slack y Facebook Messenger. Se integra con servicios de IA como Azure Cognitive Services, incluyendo Conversational Language Understanding (CLU).

¿Qué necesito?

El bot se puede desarrollar en diferentes lenguajes, como C#, Java, NodeJS o Python. En este tutorial trabajaremos con NodeJS. Usaremos también un emulador en local para poder hacer pruebas sin registrarnos en la plataforma.

¿Cómo funciona Bot Framework?

[Emulator] --- POST /api/messages --> [Tu Bot]
                           |
                           v
         Bot llama a context.sendActivity("Hola humano")
                           |
                           v
[Tu Bot] --- POST /v3/conversations/{id}/activities --> [Emulator]
  1. Bot Framework envía un POST al endpoint /api/messages del BOT:
    {
       "type": "message",
       "from": { "id": "user1", "name": "User" },
       "conversation": { "id": "conv1" },
       "recipient": { "id": "bot", "name": "Bot" },
       "text": "Hola",
       "channelId": "emulator",
       "id": "activity-id",
       "timestamp": "2025-04-12T12:34:56Z",
       "serviceUrl": "http://localhost:35039"
    }
    
  2. El BOT procesa el mensaje y envía una solicitud POST el enpoint que aparece en serviceUrl del cliente (/${serviceUrl}/v3/conversations/{id}/activities) con el mensaje de respuesta:
    {
    "type": "message",
    "from": {
       "id": "bot",
       "name": "Bot"
    },
    "conversation": {
       "id": "conv1"
    },
    "recipient": {
       "id": "user1",
       "name": "User"
    },
    "text": "Hola humano",
    "replyToId": "original-activity-id"
    }
    

¿Cómo creo mi primer bot?

  1. Instalar NodeJS.
  2. Crear el proyecto:
    npm init -y
    
  3. Instalar dependencias:
    npm install --save botbuilder restify
    npm install --save-dev nodemon
    
  4. Añadir a scripts del package.json:
    "start": "node index.js",
    
  5. Crear el bot básico en index.js:
     const restify = require('restify');
     const { BotFrameworkAdapter } = require('botbuilder');
    
     const server = restify.createServer();
     server.listen(3978, () => {
         console.log(`\nBot escuchando en http://localhost:3978`);
     });
    
     const adapter = new BotFrameworkAdapter({
         appId: process.env.MicrosoftAppId,
         appPassword: process.env.MicrosoftAppPassword
     });
    
     adapter.onTurnError = async (context, error) => {
         console.error(`\n[onTurnError]: ${error}`);
         await context.sendActivity('Oops. Algo salió mal.');
     };
    
     server.post('/api/messages', async (req, res) => {
         await adapter.processActivity(req, res, async (context) => {
             console.log(`\n[Usr] <- : ${context.activity.type} | ${context.activity.text}`);
             if (context.activity.type === 'message') {
                 const botMessage = `Repito: "${context.activity.text}"`;
                 console.log(`\n[Bot] -> : ${context.activity.type} | ${botMessage}`);
                 await context.sendActivity(botMessage);
             }
         });
     });
    
  6. Ejecutar el bot:
    npm start
    
    Aparecerá el mensaje:
    Bot escuchando en http://localhost:3978
    

¿Cómo configuro el entorno local?

Instalar Bot Framework Emulator

  1. Instalar dependencias:
     sudo apt update
     sudo apt install libsecret-1-dev
     sudo apt install fuse
     sudo apt install libfuse2
    
  2. Descargar y hacer ejecutable el archivo BotFramework-Emulator-4.15.1-linux-x86_64.AppImage:
    sudo chmod +x BotFramework-Emulator-4.15.1-linux-x86_64.AppImage
    
  3. Iniciar el emulador:
    ./BotFramework-Emulator-4.15.1-linux-x86_64.AppImage
    
  4. Conectar con el bot local. Para ello, en Open Bot escribir la URL del bot que está ejecutando y hacer clic en Connect: Open Bot
    http://localhost:3978/api/messages
    
    Open Bot URL
  5. Si todo funciona, el resultado será algo similar a esto: Conversación con el bot

En próximas entradas, veremos cómo autenticar al usuario, cómo conectar al bot con servicos NLU y de generación de respuestas o cómo hacer que le bot envíe mensajes de forma proactiva mediante Direct Line.

Deja una respuesta