我是 Angular 的新手,我想将 Watson 对话机器人集成到我的 Angular 模块中,但我不能在 Angular 中包含库。我正在使用 botkit-middleware-watson 来获得 Watson 的答案。
使用下面的代码,我设法让 slackBot 运行。
require('dotenv').load();
var Botkit = require('botkit');
var express = require('express');
var middleware = require('botkit-middleware-watson')({
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
workspace_id: process.env.WORKSPACE_ID,
version_date: '2016-09-20'
});
// Configure your bot.
var slackController = Botkit.slackbot();
var slackBot = slackController.spawn({
token: process.env.SLACK_TOKEN
});
slackController.hears(['.*'], ['direct_message', 'direct_mention', 'mention'], function(bot, message) {
slackController.log('Slack message received');
middleware.interpret(bot, message, function(err) {
if (!err) {
console.log(message.watsonData.output.text);
bot.reply(message.watsonData.output.text);
}
}
});
slackBot.startRTM();
// Create an Express app
var app = express();
var port = process.env.PORT || 5000;
app.set('port', port);
app.listen(port, function() {
console.log('Client server listening on port ' + port);
});
现在我想使用这段代码构建一个 Angular 应用程序,但是我不知道如何在 Angular 中导入一个库。我使用以下 index.html 和 app.js
索引.html
<!DOCTYPE html>
<!--[if lt IE 7]> <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html lang="en" ng-app="myApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html lang="en" ng-app="myApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="en" ng-app="Watson" class="no-js"> <!--<![endif]-->
<head>
<!--<script type="text/javascript" src ="js/dependencies.js"></script>-->
<!--Import Google Icon Font-->
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!--Import materialize.css-->
<link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/>
<!--Import jQuery before materialize.js-->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="js/materialize.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
<script type="text/javascript" src ="js/app.js"></script>
</head>
<h1 align="center">Assistant Watson </h1>
<body>
<div>
<i class="material-icons prefix">forum</i>
<input ng-model="inputMessage" type="text" class="validate">
<div ng-controller="controllerMessage">
<a ng-click="sendMessage()" ng-init="Bonjour" class="waves-effect waves-light btn">Send message</a>
<p>Votre message est : {{message}}</p>
</div>
</div>
<!-- Chat -->
</body>
</html>
应用程序.js
'use strict';
/*
var connector = require('./connector.js');
var Botkit = require('botkit');
var express = require('express');
var middleware = require('botkit-middleware-watson')({
username: process.env.CONVERSATION_USERNAME,
password: process.env.CONVERSATION_PASSWORD,
workspace_id: process.env.WORKSPACE_ID,
version_date: '2016-09-20'
});
*/
// declare a module
var myModule = angular.module('Watson', [])
.controller('controllerMessage', ['$scope', function($scope, $http) {
$scope.message="Bonjour";
$scope.sendMessage = function() {
console.log($scope.inputMessage);
$scope.message = $scope.inputMessage;
//use middleware here
}}]);
但是当我做一个 require(something) 我有错误:“require is not defined”。
有没有办法获得类似要求或让库工作以使这个角度应用程序工作?
谢谢,亚历克西
编辑 :
你说我可以使用 Requirejs,但它绕过了 Angular 模型,对吗?有没有一种“更清洁”的方式来做到这一点?