Trying to send Twilio webhook to Kommo crm

21 views Asked by At

A bit about the problem. I use Kommo Crm (Amo Crm) for work. There's integration inside that allows me to accept and send texts via Twilio, but that thing doesn't show me income images. So i need to do a custom solution. I decided to process images on my google app.

So I needed twilio to send 2 webhooks. 1 to kommo (to keep getting messages) and 1 to my app.

Right now 1 webhook to kommo works. Here's the settings (pic1settings) It's done through "Messaging Services" - "integration" - "webhook url"

So i changed the "webhook url" to my app url and tried to forward the hook to Kommo. My app received the hook, then hook were forwarded to kommo but It didnt go through. The message doesnt show in Kommo. Tried different ways. It says response 200 (success) but the message didn't appear.

Then i found this twilio function that sends 2 webhooks via axios.

const axios = require('axios');
const qs = require('querystring');

exports.handler = function(context, event, callback) {

  let twiml = new Twilio.twiml.MessagingResponse();

  let { 
      ApiVersion,
      SmsSid,
      SmsStatus,
      SmsMessageSid,
      NumSegments,
      ToState,
      From,
      MessageSid,
      AccountSid,
      ToCity,
      FromCountry,
      ToZip,
      FromCity,
      To,
      FromZip,
      ToCountry,
      Body,
      NumMedia,
      FromState 
  } = event;

  let requestBody = {
      ApiVersion,
      SmsSid,
      SmsStatus,
      SmsMessageSid,
      NumSegments,
      ToState,
      From,
      MessageSid,
      AccountSid,
      ToCity,
      FromCountry,
      ToZip,
      FromCity,
      To,
      FromZip,
      ToCountry,
      Body,
      NumMedia,
      FromState 
  };

console.log('Test '+ Body);

    let url1 = "https://chatstwilio.amocrm.com/twilio_sms/webhook/twilio_message/incoming";
    let url2 = "https://script.google.com/macros/s/AKfycbyrFMmuBDpjBIV05vD2utzL9v5au_hlcHbIA4uj7D09JkKW4XiDW9FoW/exec?fn=twiliomedia";

   const config = {
     headers: {
     'Content-Type': 'application/x-www-form-urlencoded'
     }};

  Promise.all([
       axios.post(url1, qs.stringify(requestBody), config),
       axios.post(url2, qs.stringify(requestBody), config)
       ]).then(result => {
           callback(null, twiml);
       }).catch(err => {
           console.log(err);
           callback(err);
       });
};

It sends hooks, my app receives it, BUT kommo doesnt.

I assumed that the problems in headers. X-Twilio-Signature. Or maybe something else. Idk.

What would you suggest? How to make twilio to send 2 original webhooks?

In total:

  1. Twilio original webhook that made via "Messaging Services" - "integration" - "webhook URL" delivers the messages to Kommo CRM perfectly.
  2. If i change the "webhook URL" to my custom "twilio function" it stops deliver to Kommo.

Please help. In perfect scenario i need to get messages in Kommo and work on images in my google webapp.

1

There are 1 answers

0
Kolya Smith On

So i didnt sleep 1 night and made a solution of how to send 2 or more original webhooks from twilio. Search Keywords: how to forward twilio webhook, how to send custom twilio webhook

Dont forget to enter your own authToken, https://your.site/webhook.

const webhooks = require('twilio/lib/webhooks/webhooks');
const { default: axios } = require('axios');

async function makeTwiMLBinRequest(url, data) {
  //console.log('inside');
  const authToken = 'XXXX'; // YOUR authToken 

  let eventData = {};
  //console.log('Data received: '+data);

  //delete request.headers from the webhook.
  delete data.request;
  
  eventData = data;
  //console.log('eventData received: '+eventData.toString());

  // Construct a valid application/x-www-form-urlencoded POST body
  const params = new URLSearchParams();
  for (const [key, value] of Object.entries(eventData)) {
    params.append(key, value);
  }
  data = params.toString();

  //console.log('Data to send: '+data);

  // Generate the X-Twilio-Signature
  const signature = webhooks.getExpectedTwilioSignature(
    authToken,
    url,
    eventData
  );
  
  //console.log('Sign: '+signature);

  const headers = {};
  headers['X-Twilio-Signature'] = signature;
  headers["user-agent"] = "TwilioProxy/1.1";
  headers["accept"] = "*/*";
  headers["x-home-region"] = "us1";
  headers["content-type"] = "application/x-www-form-urlencoded";
  
  //console.log(headers.toString());

  // Make the HTTP request to the passed URL
  const response = await axios.request({
    method: 'POST',
    headers,
    url,
    data
  })
  
  //console.log('Resp: '+response.data);
  return response.data;
}

exports.handler = async (context, event, callback) => {
// Make an HTTP request to your TwiML Bin
//console.log("Invoked with: ", event);
//forward twilio hook to kommo
const webhook1 = await makeTwiMLBinRequest('https://chatstwilio.amocrm.com/twilio_sms/webhook/twilio_message/incoming', event);

const webhook2 = await makeTwiMLBinRequest('https://your.site/webhook', event);

return callback();
}