Skip to content

Instantly share code, notes, and snippets.

@pgagnidze
Last active November 20, 2020 14:57
Show Gist options
  • Save pgagnidze/e3901d1614c84dfcc29d75aad431bd90 to your computer and use it in GitHub Desktop.
Save pgagnidze/e3901d1614c84dfcc29d75aad431bd90 to your computer and use it in GitHub Desktop.
'use strict';
const functions = require('firebase-functions');
const {
WebhookClient
} = require('dialogflow-fulfillment');
const {
Translate
} = require('@google-cloud/translate').v2;
const translate = new Translate({
projectID: 'translate-1f431',
});
const lang = {
'Afrikaans': 'af',
'Albanian': 'sq',
'Amharic': 'am',
'Arabic': 'ar',
'Armenian': 'hy',
'Azeerbaijani': 'az',
'Basque': 'eu',
'Belarusian': 'be',
'Bengali': 'bn',
'Bosnian': 'bs',
'Bulgarian': 'bg',
'Catalan': 'ca',
'Cebuano': 'ceb',
'Chichewa': 'ny',
'Simplified Chinese': 'zh-CN',
'Traditional Chinese': 'zh-TW',
'Corsican': 'co',
'Croatian': 'hr',
'Czech': 'cs',
'Danish': 'da',
'Dutch': 'nl',
'English': 'en',
'Esperanto': 'eo',
'Estonian': 'et',
'Filipino': 'tl',
'Finnish': 'fi',
'French': 'fr',
'Frisian': 'fy',
'Galician': 'gl',
'Georgian': 'ka',
'German': 'de',
'Greek': 'el',
'Gujarati': 'gu',
'Haitian Creole': 'ht',
'Hausa': 'ha',
'Hawaiian': 'haw',
'Hebrew': 'iw',
'Hindi': 'hi',
'Hmong': 'hmn',
'Hungarian': 'hu',
'Icelandic': 'is',
'Igbo': 'ig',
'Indonesian': 'id',
'Irish': 'ga',
'Italian': 'it',
'Japanese': 'ja',
'Javanese': 'jw',
'Kannada': 'kn',
'Kazakh': 'kk',
'Khmer': 'km',
'Korean': 'ko',
'Kurdish': 'ku',
'Kyrgyz': 'ky',
'Lao': 'lo',
'Latin': 'la',
'Latvian': 'lv',
'Lithuanian': 'lt',
'Luxembourgish': 'lb',
'Macedonian': 'mk',
'Malagasy': 'mg',
'Malay': 'ms',
'Malayalam': 'ml',
'Maltese': 'mt',
'Maori': 'mi',
'Marathi': 'mr',
'Mongolian': 'mn',
'Burmese': 'my',
'Nepali': 'ne',
'Norwegian': 'no',
'Pashto': 'ps',
'Persian': 'fa',
'Polish': 'pl',
'Portuguese': 'pt',
'Punjabi': 'pa',
'Romanian': 'ro',
'Russian': 'ru',
'Samoan': 'sm',
'Scots Gaelic': 'gd',
'Serbian': 'sr',
'Sesotho': 'st',
'Shona': 'sn',
'Sindhi': 'sd',
'Sinhala': 'si',
'Slovak': 'sk',
'Slovenian': 'sl',
'Somali': 'so',
'Spanish': 'es',
'Sundanese': 'su',
'Swahili': 'sw',
'Swedish': 'sv',
'Tajik': 'tg',
'Tamil': 'ta',
'Telugu': 'te',
'Thai': 'th',
'Turkish': 'tr',
'Ukrainian': 'uk',
'Urdu': 'ur',
'Uzbek': 'uz',
'Vietnamese': 'vi',
'Welsh': 'cy',
'Xhosa': 'xh',
'Yiddish': 'yi',
'Yoruba': 'yo',
'Zulu': 'zu'
};
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({
request,
response
});
async function translateText(agent) {
let text = agent.parameters.text || agent.add('გთხოვ მითხარი რის გადათარგმნა გინდა');
let debug = await translate.translate(agent.parameters['lang-to'], 'en');
let to = agent.parameters['lang-to'] ? !/^[a-zA-Z]+$/.test(agent.parameters['lang-to']) ? (await translate.translate(agent.parameters['lang-to'], 'en'))[0].split(' ') > 0 ? (await translate.translate(agent.parameters['lang-to'], 'en'))[0].split(' ').find(word => word !== 'In') : (await translate.translate(agent.parameters['lang-to'], 'en'))[0].split(' ')[0] : agent.parameters['lang-to'] : 'Georgian';
let [translations] = await translate.translate(text, lang[to]);
translations = Array.isArray(translations) ? translations : [translations];
return translations[0] ? agent.add(translations[0]) : agent.add("ბოდიში, ვერ გადავთარგმნე");
};
let intentMap = new Map();
intentMap.set('translate.text', translateText);
agent.handleRequest(intentMap);
});
@pgagnidze
Copy link
Author

pgagnidze commented May 3, 2019

  1. Follow https://hackernoon.com/build-a-personal-facebook-messenger-bot-in-10-minutes-a7a237f3f018 messenger bot guide, in order to setup Facebook and Dialogflow integration
  2. In Dialogflow, add prebuilt translate agent to create default intents
  3. In Dialogflow, create cloud functions fulfillment and paste above code in inline editor
  4. Update package.json with follownig code:
{
    "name": "dialogflowFirebaseFulfillment",
    "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
    "version": "0.0.1",
    "private": true,
    "license": "Apache Version 2.0",
    "author": "Google Inc.",
    "engines": {
        "node": "8"
    },
    "scripts": {
        "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
        "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
    },
    "dependencies": {
        "actions-on-google": "^2.2.0",
        "firebase-admin": "^5.13.1",
        "firebase-functions": "^2.0.2",
        "dialogflow": "^0.6.0",
        "dialogflow-fulfillment": "^0.5.0",
        "@google-cloud/translate": "^6.0.2"
    }
}
  1. Test interaction with bot via messenger, It should be able to translate any language to Georgian

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment