-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·118 lines (107 loc) · 4.33 KB
/
Copy pathapp.js
File metadata and controls
executable file
·118 lines (107 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*-----------------------------------------------------------------------------
A simple echo bot for the Microsoft Bot Framework.
-----------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");
var co = require('co')
var request = require('request');
// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function() {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
openIdMetadata: process.env.BotOpenIdMetadata
});
// Listen for messages from users
server.post('/api/messages', connector.listen());
/*----------------------------------------------------------------------------------------
* Bot Storage: This is a great spot to register the private state storage for your bot.
* We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
* For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
* ---------------------------------------------------------------------------------------- */
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);
bot.dialog('/', function(session) {
function isJapanese(text) {
return new Promise(function(resolve, reject) {
var isJapanese = false;
for (var i = 0; i < text.length; i++) {
if (text.charCodeAt(i) >= 256) {
isJapanese = true;
break;
}
}
resolve(isJapanese);
});
}
function getCognitiveApiToken(key) {
return new Promise(function(resolve, reject) {
var options = {
url: "https://api.cognitive.microsoft.com/sts/v1.0/issueToken",
method: 'POST',
headers: {
'Content-Type': 'application/jwt',
'Ocp-Apim-Subscription-Key': key
},
json: true
};
request(options, function(error, response, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
});
});
}
function translateText(lang, text, token) {
return new Promise(function(resolve, reject) {
var options = {
url: "https://api.microsofttranslator.com/V2/Http.svc/Translate",
qs: {
'to': lang,
'text': text
},
method: 'GET',
headers: {
'Authorization': 'Bearer ' + token
},
json: true
};
request(options, function(error, response, body) {
if (error) {
reject(error);
} else {
// Translator Text Apiからのレスポンスはstringタグで囲まれているのでタグを除去する
text = body.replace(/<(.+?)>|<\/string>/g, '');
resolve(text);
}
});
});
}
co(function*() {
var text = session.message.text;
var api_key = "************************"; // Translator Text Api Key
if (yield isJapanese(text)) {
var to_lang = "en";
} else {
var to_lang = "ja";
}
try {
var token = yield getCognitiveApiToken(api_key);
var result = yield translateText(to_lang, text, token);
session.send(result);
} catch (error) {
context.log(error);
}
});
});