Skip to main content

AWS Lambda and API Gateway for Bitrix24 webhook processing


Many of my clients use Bitrix24 cloud version as a CRM system and project management system.
The main advantage of the cloud version over the on-premise is that the client does not need to think about a server, it's support and security. However, this advantage is partially lost when the need arises to extend the functionality of Bitrix24 through custom applications.

Fortunately for the operation of simple non-replicable applications, those for which webhooks are usually used, do not need a server. Serverless service Lambda from AWS (Amazon Web Services) https://aws.amazon.com/ru/lambda/ and Amazon API Gateway https://aws.amazon.com/ru/api-gateway/ are enough.


Consider, for example, a simple task: when creating a new lead in one cloudy Bitrix24, we need to create the same lead in another Bitrix24.

1) Create 3 webhooks

In both Bitrix24, we create the same inbound webhook with access to CRM. When creating a webhook in Bitrix24, an address is created that is tied to a specific B24, by which we can get access to the B24 REST API methods of those modules, access to which is selected when creating the webhook.

Therefore, this address should be protected as a password or a secret key. We will need this address further when we write the AWS Lambda function:


On the screen we can see: https://b24-dawbtb.bitrix24.com/rest/1/******************/ - it is the webhook's address and /profile/ - it is default method name. We only need to copy the address.

On the first Bitrix24, we also need to create an outbound webhook for a lead creation event.




What we need to enter in the Handler address will become clear from paragraph 3 of this post.
The Authentication code is the key that is generated in Bitrix24 when creating an outbound webhook, and we will use it in the Lambda function to check is it really our Bitrix24 trying to access it.

2) Create Lambda-function

https://console.aws.amazon.com - Services - Lambda - Create function
To write a function, I prefer Python 3.


I give my function only the basic rights necessary to work with the CloudWatch logs:


Function code:

3) API Gateway setup

 Go to the Amazon API Gateway and push the button Create API


I choose ANY type for the method:


In the Integration Request settings, we need to select our Lambda function:


In the Mapping Templates, we must specify the Content-Type, which will come to us from Bitrix24. We will not parse it at the Gateway level - we will transfer it to Lambda as one line - it will be easier to debug it.



After deploying the method, we get the URL, which is exactly what we need to enter in the Handler address for the first Bitrix24 into the outbound webhook:





Done! Now when creating a lead in the first Bitrix24, the same lead is created in the second Bitrix24.



Logging and debugging Lambda functions and API Gateway is a very interesting separate topic, I will tell about it in more detail in one of the future posts.

If the theme of serverless applications for Bitrix24 is interesting to my readers, then in the future I will share my experience of integrating Bitrix24 with AWS DynamoDB and I'll tell you how to create an Amazon Lex-based chatbot for Bitrix24.

Comments

  1. Hello, I read your blog occasionally, and I own a similar one, and I was just wondering if you get a lot of spam remarks? If so how do you stop it, any plugin or anything you can advise? I get so much lately it’s driving me insane, so any assistance is very much appreciated. Project Management Apps

    ReplyDelete

Post a Comment

Popular posts from this blog

How to add a custom activity type in Bitrix24 CRM

During one of our recent self-hosted Bitrix24 implementations, we had the task of adding a new type of Activity to CRM - something between a Call Activity and a Visit Activity. The customer wanted this new Activity to be able to participate in CRM reports and filters. To begin with, it was necessary to add a link to a new Activity type in the CRM entity card. We found out that the crm.timeline component is responsible for the output of this part of the interface. In particular, its template.php contains a list of displayed links to case types. For example, for a call, something like: Accordingly, as we can see, this is a regular link with the data parameter item-item-id. So we can add ours, with our own unique data-item-id. As you can see, the href for the link is set as "#" - clicking on this link is processed by the JS code from the script.js of this component. Indeed, the BX.CrmTimelineMenuBar function is responsible for this.processItemSelection().

How to setup server for Bitrix24 on AWS EC2

How to сreate AWS Lambda Layer with Zeep Library

Python Zeep library designed to work with SOAP - services. Zeep is a pure-python module. However the lxml dependency does contain C code since it uses libxml2 and libxslt. So the Zeep library can not be deployed into the Lambda Layer without some tricks. In one of my Serverless projects, I needed to use this library, and I found such a way to deploy it into the Layer: