What it does
Given a deal id on the command line, it fetches the deal (crm.item.get via actions.v2.call.make) and its activity timeline (crm.activity.list via actions.v2.callList.make). Sends a structured prompt to GPT-4o, asks for a JSON recommendation, and creates a task (tasks.task.add via actions.v3.call.make) bound to the deal via UF_CRM_TASK.
Stack
Node.js 18+, openai.
Install
pnpm add openai
Environment
export B24_HOOK='https://your.bitrix24.com/rest/1/secret'
export OPENAI_API_KEY='sk-...'
Run
npx tsx 08-ai-assistant.ts <DEAL_ID>
Source
skills/b24jssdk-recipes/examples/08-ai-assistant.ts.
Notes
- The deal lookup uses v3 (camelCase) fields; the activity list uses classic uppercase fields (
OWNER_TYPE_ID,OWNER_ID,CREATED). - Switching to OpenAI tool-calling (
toolsparameter, function-calling loop) is the modern shape — see the suggested-examples list. - The prompt asks for a strict JSON response and uses
response_format: { type: 'json_object' }to enforce it. - Priority mapping:
high → 2,medium → 1,low → 0matches Bitrix24's task priority enum.
Webhook handler
Express server that receives Bitrix24 outbound events, fetches details via REST, dispatches by event name. Always returns 200.
Web search + LLM
Two-step RAG: ask any web-search provider, run the result through your LLM with [N] citations, post the answer as a CRM timeline comment on a deal.