quarta-feira, 9 de outubro de 2019

Google Translate integration with SAP CAI Chatbot

Hello Everyone,
Welcome to my another blog, In this blog i am going to discuss how language barrier can be removed from Enterprise Chat bot so that people can get system insight by interacting with chat bot in their own language. For example my mother tongue is Bengali definitely i would be happy to interact with AI based Chat bot in Bengali and get the S/4HANA system insight in Bengali. Like Russian is most widely speaking language followed by German and french in Europe as per google and people would love to interact with S/4HANA system in Russian Language.

Current Situation and Challenges
India have 29 States and in 29 states we have different regional culture and every states have different language and among all the language most common languages are Hindi, English, Telugu, Tamil, Bengali, Kannada, Malayalam, Oriya, Punjabi, Urdu, Sanskrit. Except English most of language Indians use as their mother tongue and there are many places in the world where people prefer to do all the business operation in their own language not in English. Now a days Enterprise Chat bot is a game changer stuff for a organization. But its a challenge to build chat bot and train chat bot in these language except English. SAP CAI is also not an exception, Except English, German, French and Spanish other languages are only standard support available means sentiment analysis, Gold Entities, Enrichment type those are not possible. Full details is available here. At the same time finding developer who knows how to design bot in every languages is a pretty big challenge.

How to overcome this Challenge
To answer this question lets ask question ourselves how many of us  have used Google Translate to convert and understand language. I am sure number will be pretty high, at least i have used and still using in many situation. In this situation also we will be using google translator engine in between a human and SAP CAI Bot. So we don’t have to train our bot into different languages. Translator is going to manipulate the text it receives from user and translate into English then pass it to SAP CAI and the response from SAP CAI is again translator will translate in the Language user asked question.
Look at this two minute coolest demo video i have recorded, Then we will go to Implementation part. I hope you liked the background positive music too 🙂
Well if you are reading this line that means you got interest and you want to continue the reading. lets get into the main business How to implement. As you see i have hosted my SAP CAI Bot in telegram many people might started thinking this guy have used standard channel connector available in SAP CAI, but hold on my friends in standard connector you cant add translator 😛
Then How ?
We have to rebuild the Telegram connector from scratch and add the translation logic. Lets have a look at the technical architecture which will definitely clear many things.
One thing i would like to mention in most of the cases when we build connector normally the hosting platform (Alexa, Slack, Google Assistant etc) where we host the bot make web hook call where it pass the message to SAP CAI. But in telegram case it is different here it doesn’t make any web hook call here we have to make a polling program which will poll the messages from telegram bot using Telegram sdk.
Lets break this whole integration into smaller piece, so that it would be easy to make sense.
1. CDS and Odata service creation for querying order status. (In this example Purchase Requisition and Purchase order Status will be queried). Check this blog.
2. Exposing the odata service through cloud connector and creating proxy api using SAP Api management. Check this blog.
3. Designing the skill of chatbot in SAP Cai. Check this amazing tutorial
4. Creating  node.js app which will be interacting with S/4HANA and provide json response the way SAP CAI  understand. Check out my github repository.
5. Deploying the node.js app we created in last step into SAP Cloud Platform CF account.
6. Use the deployed application URL with proper path as Webhook of skill in SAP CAI.
7. Test the bot using SAP CAI.
for 5, 6, 7 check my previous blog.
8. Create telegram bot by chatting with Godfather and get the access token for your bot keep it with you which we will be using in our Node.js program. Check this awesome documentation
9. Develop node.js based connector or polling program which will be used as translator between SAP CAI and Human.
const translate = require('@k3rn31p4nic/google-translate-api');
var TelegramBot = require('node-telegram-bot-api'),

    // Be sure to replace YOUR_BOT_TOKEN with your actual bot token on this line.
    telegram = new TelegramBot(<Your telegram Token>, { polling: true });
    var sapcai = require('sapcai');
    var build = new sapcai.build(<Your SAP CAI Token>, 'en')
    var detectlang , transtext, cairesponse, chatid;

telegram.on("text", (message) => {
  chatid = message.chat.id;

  //google translate code start.
  translate(message.text, { to: 'en' }).then(res => {

    detectlang = res.from.language.iso;
    transtext = res.text;
    build.dialog({ type: 'text', content: transtext}, { conversationId: chatid })
     .then(res => {
      cairesponse = res.messages[0].content;

      translate(cairesponse, { to: detectlang }).then(res => {
        telegram.sendMessage(chatid, res.text);
        console.log(res.text); 
      }).catch(err => {
        console.error(err);
      });

     
  }).catch(err => {
    console.error(err);
  });

 
  })
  .catch(err => console.error('Something went wrong', err))

});
Now in this code replace the SAP CAI and Telegram token with your token.
10. Deploy and run this node.js application into AWS EC2 or any local machine. If you want to deploy and run into AWS then checkout this awesome step by step blog.

In my previous blog i have shown how to implement translation capability for google Assistant Integration with SAP S/4HANA, you may refer if you are looking for
I hope everyone enjoyed this blog, Don’t forget to like comment and share. Your likes, comment and share definitely helps me to write more such content for you :-). I wish all the readers have a nice weekend ahead.

Regards,
Sudip

2 comentários: