93 lines
No EOL
2.7 KiB
JavaScript
93 lines
No EOL
2.7 KiB
JavaScript
module.exports = function(context, cb) {
|
|
'use strict';
|
|
//telegram
|
|
const TOKEN = context.secrets.bot_token;
|
|
const TelegramBot = require('node-telegram-bot-api');
|
|
const bot = new TelegramBot(TOKEN);
|
|
//bot.setWebHook('https://wt-fb95a52ccc1baaa2f9876a683e6b1325-0.sandbox.auth0-extend.com/vong_inline');
|
|
//apiai
|
|
const apiai = require('apiai');
|
|
const app = apiai(context.secrets.Dialogflow_APIKEY);
|
|
|
|
//messages
|
|
bot.on('message', function onMessage(msg) {
|
|
let bild = msg.text.match(/\/bild (.+)/);
|
|
let vong = msg.text.match(/\/vong (.+)/);
|
|
if (bild !== null) {
|
|
var request = app.textRequest('zeig mir das bild von ' + bild[1], {
|
|
sessionId: Math.floor(Math.random() * 100000000)
|
|
});
|
|
} else if (vong !== null) {
|
|
var request = app.textRequest('übersetze ' + vong[1], {
|
|
sessionId: Math.floor(Math.random() * 100000000)
|
|
});
|
|
} else {
|
|
//Dialoglfow
|
|
var request = app.textRequest(msg.text, {
|
|
sessionId: msg.chat.id
|
|
});
|
|
}
|
|
|
|
request.on('response', function(response) {
|
|
console.log(response);
|
|
let txt = response.result.fulfillment.messages[0].speech,
|
|
m = response.result.fulfillment.messages;
|
|
if (m.length > 1) {
|
|
return bot.sendPhoto(msg.chat.id, m[1].imageUrl);
|
|
} else {
|
|
if (txt !== '') {
|
|
return bot.sendMessage(msg.chat.id, txt);
|
|
}
|
|
}
|
|
});
|
|
|
|
request.on('error', function(error) {
|
|
console.error(error);
|
|
});
|
|
|
|
request.end();
|
|
});
|
|
|
|
//inline
|
|
bot.on('inline_query', function(msg) {
|
|
let answers = [];
|
|
let query = msg.query;
|
|
|
|
//Dialogflow - vong-bild
|
|
const vong_bild = app.textRequest("zeige mir ein bild von " + query, {
|
|
sessionId: Math.floor(Math.random() * 100000000)
|
|
});
|
|
vong_bild.on('response', dfSuccess);
|
|
vong_bild.on('error', dfError);
|
|
vong_bild.end();
|
|
|
|
function dfSuccess(response) {
|
|
let txt = response.result.fulfillment.messages[0].speech,
|
|
m = response.result.fulfillment.messages;
|
|
if (m.length > 1) {
|
|
answers.push({
|
|
type: 'photo',
|
|
id: 'photo',
|
|
photo_url: m[1].imageUrl,
|
|
thumb_url: m[1].imageUrl
|
|
});
|
|
}
|
|
answers.push({
|
|
type: 'article',
|
|
id: 'query',
|
|
title: 'Übersetzung',
|
|
description: txt,
|
|
message_text: txt
|
|
});
|
|
return bot.answerInlineQuery(msg.id, answers);
|
|
}
|
|
|
|
function dfError(error) {
|
|
console.error(error);
|
|
}
|
|
});
|
|
|
|
bot.processUpdate(context.body);
|
|
cb(null, null);
|
|
};
|
|
|