Last active
November 20, 2020 14:57
-
-
Save pgagnidze/e3901d1614c84dfcc29d75aad431bd90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
}); |
Author
pgagnidze
commented
May 3, 2019
•
- 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
- In Dialogflow, add prebuilt translate agent to create default intents
- In Dialogflow, create cloud functions fulfillment and paste above code in inline editor
- Update package.json with follownig code:
- 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