Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CRMPlugin and WhatsApp chat templates methods #22

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scripts/type-scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
25 changes: 24 additions & 1 deletion src/api/chat-template/client.ts
Original file line number Diff line number Diff line change
@@ -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?: {
Expand Down Expand Up @@ -71,4 +75,23 @@ export class ChatTemplateApi extends Endpoint {
url: `/api/v4/chats/templates/${id}`,
});
}

/** Метод позволяет отправлять шаблон WhatsApp на модерацию. */
sendWhatsAppTemplateToModeration(id: number): Promise<ResponseModerationWhatsAppTemplate> {
return this.rest.post<ResponseModerationWhatsAppTemplate>({
url: `/api/v4/chats/templates/${id}/review`,
});
}

/** Метод позволяет редактировать шаблоны по ID. */
updateWhatsAppTemplateById(
id: number,
review_id: number,
review: RequestUpdateStatusWhatsAppTemplate,
): Promise<ResponseUpdateStatusWhatsAppTemplate> {
return this.rest.patch<ResponseUpdateStatusWhatsAppTemplate>({
url: `/api/v4/chats/templates/${id}/review/${review_id}`,
payload: review as JSONValue,
});
}
}
23 changes: 23 additions & 0 deletions src/api/chat-template/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ export type RequestUpdateChatTemplate = DeepPartial<ChatTemplate> & RequestId;
export type ResponseUpdateChatTemplates = ResponseAddChatTemplates;
export type ResponseUpdateChatTemplate = Links & ChatTemplate & RequestId;
export type RequestDeleteChatTemplate = Pick<ChatTemplate, "id">;

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;
};
51 changes: 50 additions & 1 deletion src/api/source/client.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
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";
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?: {
Expand Down Expand Up @@ -70,4 +77,46 @@ export class SourceApi extends Endpoint {
url: `/api/v4/sources/${id}`,
});
}

/** Метод позволяет создать CRM Plugin. */
createCRMPlugin(plugin: RequestCreateCRMPlugin): Promise<ResponseCreateCRMPlugin> {
return this.rest.post<ResponseCreateCRMPlugin>({
url: `/api/v4/website_buttons`,
payload: plugin as JSONValue,
});
}

/** Метод позволяет подключить к CRM Plugin онлайн-чат. */
connectChatToCRMPluginById(source_id: number): Promise<void> {
return this.rest.post<void>({
url: `/api/v4/website_buttons/${source_id}/online_chat`,
});
}

/** Метод позволяет внести изменения в существующий CRM Plugin. */
updateCRMPluginById(source_id: number, data: RequestUpdateCRMPlugin): Promise<ResponseUpdateCRMPlugin> {
return this.rest.patch<ResponseUpdateCRMPlugin>({
url: `/api/v4/website_buttons/${source_id}`,
payload: data as JSONValue,
});
}

/** Метод позволяет получить параметры модели одного CRM Plugin. */
getCRMPluginById(source_id: number): Promise<ResponseGetCRMPlugin> {
return this.rest.get<ResponseGetCRMPlugin>({
url: `/api/v4/website_buttons/${source_id}`,
});
}

/** Метод позволяет получить пагинированный список параметров моделей всех CRM Plugin в аккаунте. */
getCRMPlugins(params?: {
with?: With<["scripts"]>;
page?: number;
limit?: number;
}): Promise<ResponseGetCRMPlugins> {
return this.rest.get<ResponseGetCRMPlugins>({
url: `/api/v4/website_buttons`,
query: query(params),
});
}
}
29 changes: 27 additions & 2 deletions src/api/source/types.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -24,3 +24,28 @@ export type RequestUpdateSourceById = DeepPartial<

export type ResponseUpdateSourceById = ResponseGetSourceById;
export type RequestDeleteSource = Pick<Source, "id">[];

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[];
};
};
28 changes: 27 additions & 1 deletion src/typings/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,9 @@ export type WebhookType =
/** Примечание добавлено в компанию */
| "note_company"
/** Примечание добавлено в покупателя */
| "note_customer";
| "note_customer"
/** Шаблон WhatsApp отправлен на одобрение */
| "add_chat_template_review";

export type Widget = {
/** ID виджета */
Expand Down Expand Up @@ -1300,6 +1302,11 @@ export type Source = {
services: {
/** тип сервиса, на данный момент поддерживается только один тип: "whatsapp" */
type: string;
/** Объект с настройками источника. Поле не является обязательным. */
params: {
/** Является ли источник белым whatsapp. Следует добавить для возможности работы с одобренными шаблонами whatsapp в salesbot в новом шаге отправки сообщения. Поле не является обязательным. */
waba: boolean;
};
/** Для whatsapp сервиса содержит список элементов, которые можно выбрать при настройке CRM Plugin (кнопки на сайт) */
pages: {
/** Отображаемое пользователю название пункта в выпадающем списке при настройке кнопки на сайте */
Expand All @@ -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;
Expand Down
Loading