Connector to AmoCRM API v4 (https://www.amocrm.ru/)
Connector for connecting via api to your AmoCRM system. Allows you to easily receive data on deals and contacts, as well as update them.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
The project uses Poetry instead of pip. But if you do not want to install the Poetry, for this case, create a file "requirements.txt".
To get started, you need to import the main class AmoCRM and init class AmoCRMInit.
from amocrm.v4.amoCRM import AmoCRM, AmoCRMInit
Then you need to create a model object AmoCRMInit and set settings value. If you want to use environment variables, then the file .env.example is prepared for this.
init_amocrm_data = AmoCRMInit(
connect_email="[email protected]",
connect_domain="amocrm-api",
connect_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
connect_secret_key="xxXXXxxXxxXXx",
redirect_uri="https://xxxxxxxxxx.xxx",
init_code="xХxXxxXxxXXxХХХХxxХХxxХxХxХХххххХxxXXXxxXxxXXx"
)
To debug the project, set the debug variable to true
init_amocrm_data.debug = True
And now you can transfer the settings to the main model of the AmoCRM class.
amocrm = AmoCRM(init_amocrm_data)
So far, only two AmoCRM models are available for work:
- Leads
- Contacts
Further, their list will be replenished.
To get unprocessed data, you need to use the get method.
>>> response = amocrm.get(AmoCRM.ACTION.LEADS)
>>> response
AmoCRMFuncResponce(status=True, data=AmoResponceGet({'_page' : 1, '_links' : {'self': {'href': '', ...}}))
If you need to get only the data part, the get_date method can help you.
>>> response = amocrm.get_data(AmoCRM.ACTION.LEADS)
>>> response
AmoCRMFuncResponce(status=True, data=[Leads(id=12653276, name='Name', price=0, ... ), ...])
You can also get data in the form of a Dataframe (pandas).
>>> response = amocrm.get_data_df(AmoCRM.ACTION.LEADS)
>>> response
AmoCRMFuncResponce(status=True, data=Dataframe(columns=[id, name, price, group_id, ...], data=[...]))
This method has an additional boolean parameter "get_all_data" (default False), which allows you to go through all transactions in one function call and collect all this in Dataframe.
>>> response = amocrm.get_data_df(AmoCRM.ACTION.LEADS, get_all_data=True)
Also, to obtain specific data, you need to apply the AMoCRM filters. For these purposes, there is a separate class FilterAmoCRM.
>>> from amocrm.v4.amoCRM import FilterAmoCRM
>>> amocrm_filters = FilterAmoCRM(FilterAmoCRM.ACTIONS.LAST_DAY).get_filters()
>>> amocrm_filters
{'contacts': 'limit=10&filter[created_at][from]=1623170867&filter[created_at][to]=...', 'leads': '...'}
>>> amocrm.get(AmoCRM.ACTION.LEADS, filters=amocrm_filters.get(FilterAmoCRM.TABLE.LEADS))
...
How work with POST and PATCH data, you need use special models:
- AmoRequestPostLeads - How post new leads
- AmoRequestPostContacts - How post new contacts
- AmoRequestPatchLeads - How update leads
- AmoRequestPatchContacts - How update contacts
Import:
>>> from amocrm.v4.amoCRM import AmoRequestPostLeads, AmoRequestPostContacts, \
AmoRequestPatchLeads, AmoRequestPatchContacts
Allow fields methods see here
To create an object in AmoCRM, you need to use the post method.
>>> response = amocrm.post(AmoCRM.ACTION.LEADS, data=[AmoRequestPostLeads(name="test", price=100.0, ...), ...])
>>> response
AmoCRMFuncResponce(status=True, data=AmoResponcePost(links=..., embedded=DataLeadsPost(leads=[LeadsPost(id=1, name="test"))))]))
To use the complex addition of deals, you need to call the action "LEADS_COMPLEX". This method accepts an array with objects and, if necessary, splits it into packages of size {max_count}.
>>> amocrm.post(AmoCRM.ACTION.LEADS_COMPLEX, data=[...(> 100)], max_count=50)
1 step: Send data[0:49]
2 step: Send data[50:99]
To update the model data, you must use the patch method. Required parameter is the ID of the object that needs to be updated.
>>> response = amocrm.patch(AmoCRM.ACTION.LEADS, data=[AmoRequestPatchLeads(id=1, name="test", price=100.0, ...), ...])
>>> response
AmoCRMFuncResponce(status=True, data=AmoResponcePatch(links=..., embedded=DataLeadsPatch(leads=[LeadsPatch(id=1, name="test"))))]))