commit 1258613f4b2c1224325a48255b3bbfe7976499fa Author: jgerstbe Date: Thu Feb 13 23:56:07 2020 +0100 init: sync with deployed state of 2017 diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..bba4b27 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 jan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/inline.js b/inline.js new file mode 100644 index 0000000..f0e62a5 --- /dev/null +++ b/inline.js @@ -0,0 +1,93 @@ +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); + }; + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..8b2fb1f --- /dev/null +++ b/readme.md @@ -0,0 +1,41 @@ +# vong-inline bot +🤖 Telegram Bot which translates german sentences into "vong"-speech. +Try it [online](https://bot.dialogflow.com/vong-translate). + +Can also send pictures with the translated text, try: + +- "vong _wie geht es dir_" +- "zeig mir das bild _von wie geht es dir_" +- "erzeuge mir ein zeichnis von _du ungezogenes kind_" + +# versions +- inline bot [@VongInlineBot](https://t.me/VongInlineBot) +- chat bot [@TranslateVongBot](https://t.me/TranslateVongBot) + +# uses +- [vong-generator](https://vong-generator.com/) +- [webtask](https://webtask.io) serverless host +- telegram bot functionality + +# license +MIT License + +Copyright (c) 2017 jan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/translate.js b/translate.js new file mode 100644 index 0000000..3b651ca --- /dev/null +++ b/translate.js @@ -0,0 +1,71 @@ +module.exports = function(context, cb) { + // console.log(context); + // console.log(context.body.originalRequest.data); + var request = require('request'); + var url = 'https://vong-generator.com/generate'; + var IMGURL = "https://img.vong-generator.com/?message="; + var bildpls = false; + var formData = { + text: context.body.result.parameters.message, + }; + if ((formData.text === undefined) || (formData.text === "")) { + return cb("Missing parameter text."); + } + if ((context.body.result.parameters.bild) || (context.body.result.parameters.display)) { + bildpls = true; + } + request.post({ + url: url, + formData: formData + }, function(error, response, body) { + if (error) { + return cb(error); + } else { + body = JSON.parse(body); + var data = { + "speech": body.vong, + "displayText": body.vong, + "source": "vong-generator" + } + if (bildpls) { + data = { + "messages": [ + { + "speech": body.vong, + "type": 0 + }, + { + "imageUrl": IMGURL+encodeURI(body.vong), + "platform": "telegram", + "type": 3 + }, + { + "platform": "google", + "displayText": "Hier ist dein Bild", + "textToSpeech": "Hier ist dein Bild", + "type": "simple_response" + }, + { + "buttons": [ + { + "openUrlAction": { + "url": IMGURL+encodeURI(body.vong) + }, + "title": "IMAGE-URL" + } + ], + "image": { + "url": IMGURL+encodeURI(body.vong), + "accessibility_text": body.vong + }, + "platform": "google", + "title": body.vong, + "type": "basic_card" + } + ] + } + } + return cb(null, data); + } + }); + } \ No newline at end of file