0

This year, SAP CAI started with a new Policy and the form to connect TWILIO with SAP CAI has changed definitely. I've tried for more than 4 days to do it but I haven't managed to do it yet.

I would like to know the process to send and return an answer from SAP CAI to TWILIO using webhook. The code you find on the intenet, is not working anymore:

 exports.handler = function(context, event, callback) {
    const twiml = new Twilio.twiml.MessagingResponse();
    var sapcai = require('sapcai');
    var build = new sapcai.build('API_TOKEN', 'LANGUAGE');
    build.dialog({
       type: 'text',
       content: event.Body
   }, {
       conversationId: '+NUMBER_'
   }).then(function(res) {
        data = res.messages[0].content;
        twiml.message(data);
        callback(null, twiml);
   })
}

Anybody has managed to code this to know how to do it?

It would be awesome to use an example in Postman to return an answer from SAP CAI to Twilio using this webhook.

philnash
  • 70,667
  • 10
  • 60
  • 88
Prócer
  • 21
  • 3
  • It seems that [the npm module you are trying to use was sunset in January 2021](https://github.com/SAPConversationalAI/SDK-NodeJs). It looks like you might need to write code directly against the [SAP CAI APIs](https://api.sap.com/package/SAPConversationalAI/rest). – philnash Aug 15 '22 at 02:49
  • The point is I have a BOT created in SAP CAI and I have an account in Twilio. The big question is How do I do to send the answers of my bot to Twilio if a Know my webhook URL. – Prócer Aug 15 '22 at 13:51
  • I have no problem using those APIs. In fact I managed to interact using Postman. I use an URL with PARAM, HEADER and BODY to do it. The issue is sending each BOT answer to TWILIO using a webhook which was created when I started to use the new environment of Function. i.e. If I have an answer from SAP CAI BOT, this answer How can resend it to Twilio using the Twilio webhook URL? – Prócer Aug 15 '22 at 14:32
  • New news. To pull out answers from SAP CAI, I have to call 2 APIs. The first one to obtain a TOKEN id (this lasts 43199 seconds) and 2nd, I have to use another API which use this token to obtain an answer. A new question.... how can I save this token id only once in twilio function? I mean, I dont want to call the first API every time an user sends a message. How can control this? Thanks. – Prócer Aug 15 '22 at 19:02
  • The Twilio part of the code you used above looks right to me. When you receive an incoming message from a user, you need to set up the Twilio number to make a request to your Function. Once you have the response from SAP CAI, you return a TwiML response with a `` containing the text from CAI. If something isn't working, can you describe what is not working? Are you getting an error somewhere? – philnash Aug 15 '22 at 23:33
  • As for storing the token, Functions do have hot memory that you can keep variables in, but after a while that memory is lost. So you could store the token in a variable that is outside of the scope of the function, and reuse it as long as it is available and fetch a new token if it is gone. Or you can store it in a database or key value store of your choice. – philnash Aug 15 '22 at 23:35
  • Hi Philnash, (1/2) Thanks for your answer. I've could read some articules specially "Announcing the Assets Plugin for the Twilio CLI". Now, the above code was OK until Febrary 21th, after this day, you have to use a additional token (OAuth) which to obtain it you have to call an extra API Rest. After this step, you are able to call the 2nd API which has to use the Token has been generated in the 1st API. This is a new SAP CAI Policy. It's really recent policy and I think this is the reason why you hadn't received any email about it. – Prócer Aug 16 '22 at 14:53
  • (2/2) In function of this, I think a possible option is to create an asset to save this 1st token and to save in it (maybe in a JSON format{ token: '123', valid_to: today + 43299 sec}) and create a JS logic to read every time the twilio function is called to link through SAP CAI. – Prócer Aug 16 '22 at 14:53
  • 1
    I didn't receive an email about SAP CAI policies because this is the first time I've ever heard of it! In answer to your question though, Assets aren't a good place to store a temporary token like that. To save and make an Asset available, you need to redeploy the entire function service, which is not viable within the execution time of a single Function. I would recommend either fetching a token each time, using the persistent memory in a Function and refetching the token when it is gone, or using a database or key/value store to keep the token. – philnash Aug 16 '22 at 23:52

0 Answers0