arslan
arslan

Reputation: 2224

How can I make the bot work?

I am trying to send messages to a bot based on Microsoft bot framework.

Here is my code.

   const builder = require("botbuilder");

   \\I have id and password, did not show them here
   const config = {
    appId: "**********************",  
    appPassword: "********************"
   };

   const connector = new builder.ChatConnector(config);
   const bot = new builder.UniversalBot(connector);

   // respond to bot messages     
  app.post("/bot", () => console.log('being called') ,connector.listen());



   // define bot dialog routes
   bot.dialog("/", session => { 

   console.log('++++++++++++++>', session.message.text)

  });

It is printing the "being called ", but not printing "++++++++++++++>". I got no error message.

How can I check the problem and fix this?

Note: this is not emulator, i ma trying this within an app locally.

Upvotes: 1

Views: 78

Answers (1)

tombraider
tombraider

Reputation: 1167

Whilst there appears to be missing code (e.g. app isn't defined), I'm going to presume you're using Express and that side of things is fine. Your root dialog will not be triggered until it has an input from the user.

Take this super simple ConsoleConnector example:

const builder = require('botbuilder')

let connector = new builder.ConsoleConnector().listen()
let bot = new builder.UniversalBot(connector)

bot.dialog('/', (session) => {
   console.log('Testing')
})

Paste that into a file and run it. You'll notice that you won't get any error messages, but you won't see the console log either. Now type anything in and press return. This will trigger the root dialog and the console.log will fire.

If you want to send a proactive message when the conversation starts, check out the 'Greet a user' example in the Bot Framework documentation.

Edit:

Additional Echo Bot example using Express:

const express = require('express')
const builder = require('botbuilder')
const app = express()
const port = 8080

const connector = new builder.ChatConnector({
  appId: process.env.MICROSOFT_APP_ID,
  appPassword: process.env.MICROSOFT_APP_PASSWORD
})
const bot = new builder.UniversalBot(connector)

bot.dialog('/', (session) => {
  console.log('Testing')
  session.send(session.message.text)
})

app.post('/api/messages', connector.listen())

app.listen(port)

I realise you're not using the Emulator at this point, but for the sake of testing, point it at localhost:8080/api/messages and type something. You'll see the console.log message appear and the message you entered will be echoed back to you.

Upvotes: 2

Related Questions