Codementor Events

Cloud Bitrix24 and Post Tracking - SOAP Integration via AWS Lambda

Published Sep 21, 2019
Cloud Bitrix24 and Post Tracking - SOAP Integration via AWS Lambda

I like to use Serverless AWS technology: Lambda and API Gateway to customize the cloud Bitrix24.
It is comfortable, simple, elegant and very cheap.

For example, AWS Lambda gives us the possibility to check post tracking numbers for free for the first year through the Post SOAP service. After a year, if my measurements and calculations are correct, checking 100,000 post tracking numbers will cost about $12.

What do we need for this:

  1. Get the login and the password for integration on the Russian Post website: https://www.pochta.ru/support/business/api

  2. Prepare a Lambda Layer with a library for working with SOAP. I chose the open-source Zeep library. How to pack Python3 Zeep library in a Lambda Layer, I wrote earlier on my blog: http://bedrosovayulia.blogspot.com/2019/07/how-to-reate-aws-lambda-layer-with-zeep.html

  3. On the side of Bitrix24, we need to create an inbound webhook with the right to send messages and with access to the universal lists (in my case) or, perhaps, to CRM - depending on where you need to save the checking result.

  4. Write a business process in Bitrix24, which will call the outbound webhook, giving it the post tracking number, as well as the ID of the element and information block, in which the result of the check must be saved.

  5. Write an AWS Lambda function that gets data from Bitrix24, requests the status of the tracking number from Post SOAP service and return the result to Bitrix24 via the inbound webhook.

  6. Configure the API Gateway for our Lambda function. I described this process in detail earlier: http://bedrosovayulia.blogspot.com/2019/07/aws-lambda-and-api-gateway-for-bitrix24.html

I want to talk about the integration scenarios of post tracking that I implemented in the cloudy Bitrix24 for my company.

Usually, I scan all incoming and outgoing correspondence and put them on a list that I created specifically for this in Bitrix24 - Сorrespondence list. Among other data, I also store the post tracking number there:

1.png

For this list I implemented 2 business processes: The first one is a simple one that allows me to quickly check the tracking number manually and receive data in private messages in the portal:

2.png

The only action that this business process performs is calling a webhook.

3.png

The handler of this webhook on the AWS Lambda side looks like this:

import json
from zeep import Client
from botocore.vendored import requests
from urllib.parse import parse_qs

def lambda_handler(event, context):
    
    url = 'https://tracking.russianpost.ru/rtm34?wsdl'
    barcode = event['barcode']
    my_login = '************'
    my_password = '**********'
    
    client = Client(url)
    
    OperationHistoryRequest= {
        "Barcode":barcode,
        "MessageType":0,
        "Language":"RUS"
        }
                
    AuthorizationHeader= {
        "login":my_login,
        "password":my_password
        }
        
    with client.settings(strict=False):       
        result = client.service.getOperationHistory(OperationHistoryRequest,AuthorizationHeader)
    
    info='\n'
    FinalStatus=''
    
    for item in result:
        try:
            info=info+' '+str(item['OperationParameters']['OperDate'])[:10]+' '
        except:
            pass
        
        try:
            info=info+' '+str(item['AddressParameters']['OperationAddress']['Index'])+' '
        except:
            pass
        
        try:
            info=info+' '+str(item['OperationParameters']['OperAttr']['Name'])+' '
        except:
            pass
        
        try:
            FinalStatus=str(item['OperationParameters']['OperAttr']['Name'])
        except:
            pass
        
        info=info+'\n'
        
    data = {
            "user_id": 1,
            "message": barcode+' '+info+'\n '+FinalStatus
        }

    response = requests.get('https://bedrosova.bitrix24.ru/rest/1/************/im.message.add.json',data)
    
    return {
        'statusCode': 200,
    }

I run this business process manually:

4.png

After that, I receive a personal message with the entire history of moving the letter and its current status:

5.png

The second - a large business process starts automatically when the list entry is changed:

6.png

The business process checks are in this letter, incoming or outgoing. For incoming, it does nothing. Tracking number for an outgoing letter doesn’t appear immediately because I enter an entry in the list when I pack documents into an envelope, and I fill the tracking number later when I receive mail receipts, so the business process checks to see if tracking number already exists and if not yet - it waits for 3 days. If the tracking number already exists, the business process calls the webhook.

7.png

Its handler is similar to the one I have already shown, but also the status is being recorded in the "Status" field of the list. How to update the list from the AWS Lambda function, I also wrote earlier in the blog: http://bedrosovayulia.blogspot.com/2019/07/how-to-update-bitrix24-list-item-in.html

After calling the webhook, I inserted into the business process a pause of 5 minutes to give the handler on the AWS Lambda side time to process. After this, the business process checks whether the tracking number has moved to the final status "Delivered" - if yes, then the cycle stops spinning. If the status is not reached, then after 3 days the process is repeated. This process works on its own and frees me from routine work.

8.png

All I need in order to see the status of my sent letters are to go to my list and set the necessary filters. Similarly, it is possible to integrate the cloud Bitrix24 with any external system that can give data via SOAP or provides API in any form.

Discover and read more posts from Yuliya Bedrosova
get started
post commentsBe the first to share your opinion
Show more replies