简体   繁体   English

如何启动从Node.js客户端到Microsoft Bot的对话

[英]How to start a conversation from nodejs client to microsoft bot

I'm trying to build an app that would send messages to a chat on Microsoft bot framework based on Directline lib, and reply with answer. 我正在尝试构建一个应用程序,该应用程序可以将消息发送到基于Directline lib的Microsoft bot框架上的聊天室,然后进行答复。 The app is a web application where should send a POST request that should forward the message to the bot client, and reply with response of the bot from Microsoft bot framework. 该应用程序是一个Web应用程序,应在其中发送POST请求,该请求应将消息转发到bot客户端,并以来自Microsoft bot框架的bot响应进行回复。

An example of HTTP POST request: HTTP POST请求的示例:

http://host:port/api/message BODY: {message:"Hi"} http:// host:port / api / message正文 {message:“ Hi”}

It should send the "Hi" as a text to the relevant chat bot in the Microsoft Bot framework and reply with what ever the framework replys with. 它应将“ Hi”作为文本发送到Microsoft Bot框架中的相关聊天机器人,并以该框架答复的内容进行答复。

I have put the secret and done all i think i should have done in order it to be working but, i'm having problem generating a conversation that should talk with the chat bot. 我已经提出了秘密,并做了我认为应该做的所有事情,以便使其正常工作,但是,在生成应与聊天机器人进行对话的对话时遇到了问题。

This is how i did it: 这是我的方法:

"use strict";

require('dotenv').config();

var express = require('express');
var app = express();
var fs = require("fs");
var bodyParser = require("body-parser");
var http = require('http');
var postLib = require("./lib");

var messageFilePath="message.out";

var cors = require('cors');
var uuid = require('uuid');

//new botclient
var client = require('directline-api');

// config items

var pollInterval = 1000;
var directLineSecret = 'secret';
var directLineClientName = 'DirectLineClient';
var directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';

///bot client end


var sendmail = require('sendmail')({silent: true})

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

app.options('*', cors()); // include before other routes
var corsOptions = {
  origin: '*',
  optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};

    app.post('/interact/message', cors(corsOptions), function(req, res) {
                var uuid1 = uuid.v1();
                var bodyMessage = JSON.stringify(req.body);
                var log = uuid1 + ', ' + new Date().getTime() + ", " + bodyMessage;
                if (req.query.botId == 1) {
                        emailMessage(log);
                        res.send(postLib.reply.reply);
                }
                if (req.query.botId == 2) {
                        botMessage(bodyMessage.message);
                        res.send(postLib.reply.reply);
                }

});




function emailMessage(log){

                sendmail({
                        from: postLib.mail.from,
                        to: postLib.mail.to,
                        subject: postLib.mail.subject,
                        html: 'this is the log: [' + log + ']',
                        }, function(err, reply) {
                        console.log(err && err.stack);
                        console.dir(reply);
                });


                fs.appendFile(messageFilePath, "\n" + log, function(error){
                        if (error) throw error;
                });
}


function botMessage(message){

var token = client.getToken(directLineSecret);

// create a conversation
var conversationId = client.createConversation(token);

// post a message in a conversation
client.postMessage(token, conversationId, {
                text: message
            });
return client.getMessage(token, conversationId, 0);
}

var server = app.listen(8082, function () {

  var host = server.address().address
  var port = server.address().port

  console.log("interact post server listening at http://%s:%s", host, port)

})

目前尚不清楚您遇到的问题,但我建议您检查一下Node.js Direct Line示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft Bot Framework,它可以启动对话 - Microsoft Bot Framework, can it start the conversation Microsoft Bot:如何记录每个对话步骤? - Microsoft bot: How to log each conversation step? BotBuilder for Node.js v3和Microsoft Bot Framework:如何远程触发与用户的新对话的开始? - BotBuilder for Node.js v3 & Microsoft Bot Framework: How do I remotely trigger the start of a new conversation with a user? 如何在Skype的Microsoft Bot框架中获取对话详细信息? - How to get conversation details in microsoft bot framework for skype? 如何使用Microsoft Bot框架使Bot与Slack中的用户在群组中发起1:1对话? - How to make the bot initiate 1:1 conversation in a group with the user in Slack using Microsoft bot-framework? Nodejs和microsoft bot框架 - Nodejs and microsoft bot framework 瀑布对话中的Microsoft Bot Framework LUIS - Microsoft Bot Framework LUIS in waterfall conversation 如何使用 Microsoft Graph 客户端作为其他用户从 Microsoft Teams Bot 发送电子邮件? - How to send email from Microsoft Teams Bot using Microsoft Graph Client as a different user? 如何在Microsoft Bot Builder NodeJs中保存来自builder.Prompts.confirm()的响应 - How do I save a response from builder.Prompts.confirm() in Microsoft Bot Builder NodeJs 如果Microsoft漫游器闲置5分钟并在Node.JS中通知用户,如何重置对话? - How to reset the conversation if the Microsoft bot is idle for 5 mins and inform user in Node.JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM