From 9d55c6f027e6703197dd03e3b535b3ccf4801a69 Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Wed, 7 Feb 2024 19:20:15 +0300 Subject: [PATCH 1/2] add CRMPlugin and WhatsApp chat templates methods --- scripts/type-scraper.ts | 3 ++ src/api/chat-template/client.ts | 25 +++++++++++++++- src/api/chat-template/types.ts | 23 +++++++++++++++ src/api/source/client.ts | 51 ++++++++++++++++++++++++++++++++- src/api/source/types.ts | 29 +++++++++++++++++-- src/typings/entities.ts | 28 +++++++++++++++++- 6 files changed, 154 insertions(+), 5 deletions(-) diff --git a/scripts/type-scraper.ts b/scripts/type-scraper.ts index e92249e..c3e87cc 100644 --- a/scripts/type-scraper.ts +++ b/scripts/type-scraper.ts @@ -224,3 +224,6 @@ export function writeTypes(data: string): void { // parseTableFromUrl("File", "https://www.amocrm.ru/developers/content/files/files-api", 5, { // comment: true, // }).then(writeTypes); +// parseTableFromUrl("CRMPlugin", "https://www.amocrm.ru/developers/content/crm_platform/sources-api", 26, { +// comment: true, +// }).then(writeTypes); diff --git a/src/api/chat-template/client.ts b/src/api/chat-template/client.ts index 2705901..cb40abb 100644 --- a/src/api/chat-template/client.ts +++ b/src/api/chat-template/client.ts @@ -1,18 +1,22 @@ -import type { JSONValue } from "../../typings/utility.ts"; import type { RequestAddChatTemplate, RequestDeleteChatTemplate, RequestUpdateChatTemplate, + RequestUpdateStatusWhatsAppTemplate, ResponseAddChatTemplates, ResponseGetChatTemplateById, ResponseGetChatTemplates, + ResponseModerationWhatsAppTemplate, ResponseUpdateChatTemplate, ResponseUpdateChatTemplates, + ResponseUpdateStatusWhatsAppTemplate, } from "./types.ts"; import { Endpoint } from "../../core/endpoint.ts"; import { FilterLike } from "../../helpers/filter.ts"; import { query } from "../../helpers/query.ts"; +import type { JSONValue } from "../../typings/utility.ts"; + export class ChatTemplateApi extends Endpoint { /** Метод позволяет получить список шаблонов в аккаунте. */ getChatTemplates(params?: { @@ -71,4 +75,23 @@ export class ChatTemplateApi extends Endpoint { url: `/api/v4/chats/templates/${id}`, }); } + + /** Метод позволяет отправлять шаблон WhatsApp на модерацию. */ + sendWhatsAppTemplateToModeration(id: number): Promise { + return this.rest.post({ + url: `/api/v4/chats/templates/${id}/review`, + }); + } + + /** Метод позволяет редактировать шаблоны по ID. */ + updateWhatsAppTemplateById( + id: number, + review_id: number, + review: RequestUpdateStatusWhatsAppTemplate, + ): Promise { + return this.rest.patch({ + url: `/api/v4/chats/templates/${id}/review/${review_id}`, + payload: review as JSONValue, + }); + } } diff --git a/src/api/chat-template/types.ts b/src/api/chat-template/types.ts index f43e183..00c5b80 100644 --- a/src/api/chat-template/types.ts +++ b/src/api/chat-template/types.ts @@ -22,3 +22,26 @@ export type RequestUpdateChatTemplate = DeepPartial & RequestId; export type ResponseUpdateChatTemplates = ResponseAddChatTemplates; export type ResponseUpdateChatTemplate = Links & ChatTemplate & RequestId; export type RequestDeleteChatTemplate = Pick; + +export type ResponseModerationWhatsAppTemplate = { + _embedded: { + reviews: { + id: number; + source_id: number; + status: string; + reject_reason: string; + }; + }; +}; + +export type RequestUpdateStatusWhatsAppTemplate = { + status: "approved" | "rejected" | "paused"; + reject_reason: string; +}; + +export type ResponseUpdateStatusWhatsAppTemplate = { + id: number; + source_id: number; + status: string; + reject_reason: string; +}; diff --git a/src/api/source/client.ts b/src/api/source/client.ts index 4e36c26..fdbabd7 100644 --- a/src/api/source/client.ts +++ b/src/api/source/client.ts @@ -1,12 +1,17 @@ -import type { JSONValue } from "../../typings/utility.ts"; import type { RequestAddSource, + RequestCreateCRMPlugin, RequestDeleteSource, + RequestUpdateCRMPlugin, RequestUpdateSource, RequestUpdateSourceById, ResponseAddSources, + ResponseCreateCRMPlugin, + ResponseGetCRMPlugin, + ResponseGetCRMPlugins, ResponseGetSourceById, ResponseGetSources, + ResponseUpdateCRMPlugin, ResponseUpdateSourceById, ResponseUpdateSources, } from "./types.ts"; @@ -14,6 +19,8 @@ import { Endpoint } from "../../core/endpoint.ts"; import { FilterLike } from "../../helpers/filter.ts"; import { query } from "../../helpers/query.ts"; +import type { JSONValue, With } from "../../typings/utility.ts"; + export class SourceApi extends Endpoint { /** Метод позволяет получить список источников интеграции. */ getSources(params?: { @@ -70,4 +77,46 @@ export class SourceApi extends Endpoint { url: `/api/v4/sources/${id}`, }); } + + /** Метод позволяет создать CRM Plugin. */ + createCRMPlugin(plugin: RequestCreateCRMPlugin): Promise { + return this.rest.post({ + url: `/api/v4/website_buttons`, + payload: plugin as JSONValue, + }); + } + + /** Метод позволяет подключить к CRM Plugin онлайн-чат. */ + connectChatToCRMPluginById(source_id: number): Promise { + return this.rest.post({ + url: `/api/v4/website_buttons/${source_id}/online_chat`, + }); + } + + /** Метод позволяет внести изменения в существующий CRM Plugin. */ + updateCRMPluginById(source_id: number, data: RequestUpdateCRMPlugin): Promise { + return this.rest.patch({ + url: `/api/v4/website_buttons/${source_id}`, + payload: data as JSONValue, + }); + } + + /** Метод позволяет получить параметры модели одного CRM Plugin. */ + getCRMPluginById(source_id: number): Promise { + return this.rest.get({ + url: `/api/v4/website_buttons/${source_id}`, + }); + } + + /** Метод позволяет получить пагинированный список параметров моделей всех CRM Plugin в аккаунте. */ + getCRMPlugins(params?: { + with?: With<["scripts"]>; + page?: number; + limit?: number; + }): Promise { + return this.rest.get({ + url: `/api/v4/website_buttons`, + query: query(params), + }); + } } diff --git a/src/api/source/types.ts b/src/api/source/types.ts index 398eb3b..311e2b6 100644 --- a/src/api/source/types.ts +++ b/src/api/source/types.ts @@ -1,5 +1,5 @@ -import type { Source } from "../../typings/entities.ts"; -import type { DeepPartial, Links, Total } from "../../typings/utility.ts"; +import type { CRMPlugin, Source } from "../../typings/entities.ts"; +import type { DeepPartial, Links, Page, Total } from "../../typings/utility.ts"; export type ResponseGetSources = Total & Links & { _embedded: { @@ -24,3 +24,28 @@ export type RequestUpdateSourceById = DeepPartial< export type ResponseUpdateSourceById = ResponseGetSourceById; export type RequestDeleteSource = Pick[]; + +export type RequestCreateCRMPlugin = { + pipeline_id: number; + trusted_websites?: string[]; + is_used_in_app?: boolean; +}; + +export type ResponseCreateCRMPlugin = { + source_id: number; + trusted_websites: string[]; +}; + +export type RequestUpdateCRMPlugin = { + trusted_websites: { + add: string[]; + }; +}; + +export type ResponseUpdateCRMPlugin = CRMPlugin; +export type ResponseGetCRMPlugin = CRMPlugin; +export type ResponseGetCRMPlugins = Page & Links & { + _embedded: { + website_buttons: CRMPlugin[]; + }; +}; diff --git a/src/typings/entities.ts b/src/typings/entities.ts index bfcff76..71f23db 100644 --- a/src/typings/entities.ts +++ b/src/typings/entities.ts @@ -1187,7 +1187,9 @@ export type WebhookType = /** Примечание добавлено в компанию */ | "note_company" /** Примечание добавлено в покупателя */ - | "note_customer"; + | "note_customer" + /** Шаблон WhatsApp отправлен на одобрение */ + | "add_chat_template_review"; export type Widget = { /** ID виджета */ @@ -1300,6 +1302,11 @@ export type Source = { services: { /** тип сервиса, на данный момент поддерживается только один тип: "whatsapp" */ type: string; + /** Объект с настройками источника. Поле не является обязательным. */ + params: { + /** Является ли источник белым whatsapp. Следует добавить для возможности работы с одобренными шаблонами whatsapp в salesbot в новом шаге отправки сообщения. Поле не является обязательным. */ + waba: boolean; + }; /** Для whatsapp сервиса содержит список элементов, которые можно выбрать при настройке CRM Plugin (кнопки на сайт) */ pages: { /** Отображаемое пользователю название пункта в выпадающем списке при настройке кнопки на сайте */ @@ -1312,6 +1319,25 @@ export type Source = { }[]; }; +export type CRMPlugin = { + /** ID аккаунта */ + account_id: number; + /** ID источника */ + source_id: number; + /** ID кнопки, связанной с источником */ + button_id: number; + /** статус наличия контроля дублей для источников */ + is_duplication_control_enabled: boolean; + /** название истчника */ + name: string; + /** статус создания источника ("created" или "creation_pending") */ + creation_status: "created" | "creation_pending"; + /** ID воронки */ + pipeline_id: number | null; + /** скрипт CRM Plugin */ + script: string | null; +}; + export type ShortLink = { /** Адрес страницы. Обязательный параметр */ url: string; From 41061ef7912aaa572104425586196904761bb7bc Mon Sep 17 00:00:00 2001 From: shevernitskiy Date: Wed, 7 Feb 2024 19:29:15 +0300 Subject: [PATCH 2/2] fix --- src/api/chat-template/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/chat-template/types.ts b/src/api/chat-template/types.ts index 00c5b80..9fd567b 100644 --- a/src/api/chat-template/types.ts +++ b/src/api/chat-template/types.ts @@ -30,7 +30,7 @@ export type ResponseModerationWhatsAppTemplate = { source_id: number; status: string; reject_reason: string; - }; + }[]; }; };