@botozap/sdk 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -39,16 +39,28 @@ function assertAudioPricing(value) {
39
39
  var BotoZapError = class extends Error {
40
40
  code;
41
41
  status;
42
+ outcome;
43
+ retry;
42
44
  /** Headers da resposta HTTP. Ausente em erros sem resposta (status 0). */
43
45
  headers;
44
- constructor(code, message, status, headers) {
46
+ constructor(code, message, status, headers, details) {
45
47
  super(message);
46
48
  this.name = "BotoZapError";
47
49
  this.code = code;
48
50
  this.status = status;
49
51
  this.headers = headers;
52
+ this.outcome = details?.outcome;
53
+ this.retry = details?.retry;
50
54
  }
51
55
  };
56
+ var PLAN_ERROR_CODES = [
57
+ "plan_restricted",
58
+ "free_form_limit_reached",
59
+ "free_number_cap"
60
+ ];
61
+ function isPlanError(error) {
62
+ return error instanceof BotoZapError && PLAN_ERROR_CODES.includes(error.code);
63
+ }
52
64
 
53
65
  // src/resources/ai/upload.ts
54
66
  var receipts = /* @__PURE__ */ new WeakMap();
@@ -3813,6 +3825,7 @@ var AI_OPERATIONS = [
3813
3825
  ];
3814
3826
 
3815
3827
  // src/resources/ai/types.ts
3828
+ var AI_CREDENTIAL_PROVIDERS = ["openai", "anthropic"];
3816
3829
  var AI_PURPOSES = [
3817
3830
  "default",
3818
3831
  "agent_turn",
@@ -3936,7 +3949,7 @@ var AiCredentialsResource = class {
3936
3949
  get(input) {
3937
3950
  return request(this.client, AI_OPERATIONS[11], input);
3938
3951
  }
3939
- /** Cria credencial própria no Vault; a resposta nunca contém a chave. */
3952
+ /** Cria credencial própria no Vault; a resposta nunca contém a chave. Só `AI_CREDENTIAL_PROVIDERS` (openai/anthropic); os demais: 422 `provider_not_enabled`. */
3940
3953
  create(input) {
3941
3954
  return request(this.client, AI_OPERATIONS[12], input);
3942
3955
  }
@@ -5080,8 +5093,9 @@ var Messages = class {
5080
5093
  }
5081
5094
  client;
5082
5095
  /** Envia uma mensagem de texto. */
5083
- async send(params) {
5096
+ async send(params, options = {}) {
5084
5097
  const result = await this.client.requestObject("POST", "/messages", {
5098
+ idempotencyKey: options.idempotencyKey,
5085
5099
  body: {
5086
5100
  to: params.to,
5087
5101
  type: "text",
@@ -5092,8 +5106,9 @@ var Messages = class {
5092
5106
  return assertSendResult(result);
5093
5107
  }
5094
5108
  /** Envia uma mensagem usando um template aprovado. */
5095
- async sendTemplate(params) {
5109
+ async sendTemplate(params, options = {}) {
5096
5110
  const result = await this.client.requestObject("POST", "/messages", {
5111
+ idempotencyKey: options.idempotencyKey,
5097
5112
  body: {
5098
5113
  to: params.to,
5099
5114
  type: "template",
@@ -5104,7 +5119,7 @@ var Messages = class {
5104
5119
  return assertSendResult(result);
5105
5120
  }
5106
5121
  /** Envia mídia por URL pública. */
5107
- async sendMedia(params) {
5122
+ async sendMedia(params, options = {}) {
5108
5123
  const media = { link: params.link };
5109
5124
  if ("caption" in params && params.caption !== void 0) {
5110
5125
  media.caption = params.caption;
@@ -5113,6 +5128,7 @@ var Messages = class {
5113
5128
  media.filename = params.filename;
5114
5129
  }
5115
5130
  const result = await this.client.requestObject("POST", "/messages", {
5131
+ idempotencyKey: options.idempotencyKey,
5116
5132
  body: {
5117
5133
  to: params.to,
5118
5134
  from: params.from,
@@ -5122,6 +5138,47 @@ var Messages = class {
5122
5138
  });
5123
5139
  return assertSendResult(result);
5124
5140
  }
5141
+ /**
5142
+ * Envia botões de resposta, lista ou botão de link (WhatsApp; mensagem livre,
5143
+ * exige janela de 24h aberta).
5144
+ */
5145
+ async sendInteractive(params, options = {}) {
5146
+ return postMessage(
5147
+ this.client,
5148
+ { to: params.to, from: params.from, type: "interactive", interactive: params.interactive },
5149
+ options
5150
+ );
5151
+ }
5152
+ /** Envia uma localização (WhatsApp; mensagem livre). */
5153
+ async sendLocation(params, options = {}) {
5154
+ const location = {
5155
+ latitude: params.latitude,
5156
+ longitude: params.longitude
5157
+ };
5158
+ if (params.name !== void 0) location.name = params.name;
5159
+ if (params.address !== void 0) location.address = params.address;
5160
+ return postMessage(
5161
+ this.client,
5162
+ { to: params.to, from: params.from, type: "location", location },
5163
+ options
5164
+ );
5165
+ }
5166
+ /**
5167
+ * Reage a uma mensagem recebida (WhatsApp). `emoji: ""` retira a reação.
5168
+ * O retorno traz `reaction` com o alvo e a ação.
5169
+ */
5170
+ async sendReaction(params, options = {}) {
5171
+ return postMessage(
5172
+ this.client,
5173
+ {
5174
+ to: params.to,
5175
+ from: params.from,
5176
+ type: "reaction",
5177
+ reaction: { message_id: params.message_id, emoji: params.emoji }
5178
+ },
5179
+ options
5180
+ );
5181
+ }
5125
5182
  /** Lista as mensagens da conta (paginação por cursor). */
5126
5183
  list(params = {}) {
5127
5184
  return this.client.requestCursorList("GET", "/messages", {
@@ -5129,6 +5186,7 @@ var Messages = class {
5129
5186
  limit: params.limit,
5130
5187
  after: params.after,
5131
5188
  before: params.before,
5189
+ sort: params.sort,
5132
5190
  phone_number_id: params.phone_number_id,
5133
5191
  conversation_id: params.conversation_id,
5134
5192
  direction: params.direction,
@@ -5146,6 +5204,13 @@ var Messages = class {
5146
5204
  );
5147
5205
  }
5148
5206
  };
5207
+ async function postMessage(client, body, options) {
5208
+ const result = await client.requestObject("POST", "/messages", {
5209
+ idempotencyKey: options.idempotencyKey,
5210
+ body
5211
+ });
5212
+ return assertSendResult(result);
5213
+ }
5149
5214
  function assertSendResult(value) {
5150
5215
  if (typeof value.wamid !== "string" || typeof value.to !== "string" || typeof value.status !== "string" || value.id !== null && typeof value.id !== "string") {
5151
5216
  throw new BotoZapError(
@@ -5415,6 +5480,8 @@ var Conversations = class {
5415
5480
  {
5416
5481
  query: {
5417
5482
  limit: params.limit,
5483
+ customer_id: params.customer_id,
5484
+ agent_paused: params.agent_paused === void 0 ? void 0 : String(params.agent_paused),
5418
5485
  after: params.after,
5419
5486
  before: params.before,
5420
5487
  phone_number_id: params.phone_number_id,
@@ -5436,7 +5503,7 @@ var Conversations = class {
5436
5503
  * A leitura e o envio usam a mesma chave; o POST /messages revalida Conta,
5437
5504
  * ambiente, identidade, janela, quota e billing antes de chamar a Meta.
5438
5505
  */
5439
- async reply(id, params) {
5506
+ async reply(id, params, options = {}) {
5440
5507
  const conversation = await this.get(id);
5441
5508
  const from = conversation.phone_number_id;
5442
5509
  if (typeof from !== "string" || !from.trim()) {
@@ -5455,7 +5522,7 @@ var Conversations = class {
5455
5522
  422
5456
5523
  );
5457
5524
  }
5458
- return this.client.messages.send({ to, text: params.text, from });
5525
+ return this.client.messages.send({ to, text: params.text, from }, options);
5459
5526
  }
5460
5527
  update(id, params) {
5461
5528
  return this.client.requestItem(
@@ -5732,6 +5799,18 @@ var WebhookDeliveries = class {
5732
5799
  }
5733
5800
  };
5734
5801
 
5802
+ // src/resources/me.ts
5803
+ var MeResource = class {
5804
+ constructor(client) {
5805
+ this.client = client;
5806
+ }
5807
+ client;
5808
+ /** Retorna a Conta, o ambiente e os scopes efetivos da chave. */
5809
+ get() {
5810
+ return this.client.requestItem("GET", "/me");
5811
+ }
5812
+ };
5813
+
5735
5814
  // src/client.ts
5736
5815
  var DEFAULT_BASE_URL = "https://botozap.com.br/api/v1";
5737
5816
  var BotoZap = class {
@@ -5760,14 +5839,15 @@ var BotoZap = class {
5760
5839
  users;
5761
5840
  apiLogs;
5762
5841
  webhookDeliveries;
5763
- apiKey;
5842
+ me;
5843
+ credential;
5764
5844
  baseUrl;
5765
5845
  fetchImpl;
5766
5846
  constructor(options) {
5767
- if (!options || !options.apiKey) {
5768
- throw new Error("BotoZap: apiKey \xE9 obrigat\xF3rio.");
5847
+ if (!options || Boolean(options.apiKey) === Boolean(options.accessToken)) {
5848
+ throw new Error("BotoZap: informe apenas apiKey ou accessToken.");
5769
5849
  }
5770
- this.apiKey = options.apiKey;
5850
+ this.credential = options.accessToken ?? options.apiKey;
5771
5851
  this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
5772
5852
  const resolvedFetch = options.fetch ?? globalThis.fetch;
5773
5853
  if (typeof resolvedFetch !== "function") {
@@ -5801,6 +5881,7 @@ var BotoZap = class {
5801
5881
  this.users = new Users(this);
5802
5882
  this.apiLogs = new ApiLogs(this);
5803
5883
  this.webhookDeliveries = new WebhookDeliveries(this);
5884
+ this.me = new MeResource(this);
5804
5885
  }
5805
5886
  /** PUT de artefato em URL assinada, sem token BotoZap nem cookies. Não segue redirecionamentos. */
5806
5887
  async putArtifact(url, contentType, file) {
@@ -5842,12 +5923,16 @@ var BotoZap = class {
5842
5923
  if (value !== void 0) url.searchParams.set(key, String(value));
5843
5924
  }
5844
5925
  }
5926
+ const credential = typeof this.credential === "function" ? await this.credential() : this.credential;
5927
+ if (!credential || /[\s,]/.test(credential)) {
5928
+ throw new BotoZapError("invalid_credential", "Credencial Bearer inv\xE1lida.", 401);
5929
+ }
5845
5930
  let res;
5846
5931
  try {
5847
5932
  res = await this.fetchImpl(url.toString(), {
5848
5933
  method,
5849
5934
  headers: {
5850
- Authorization: `Bearer ${this.apiKey}`,
5935
+ Authorization: `Bearer ${credential}`,
5851
5936
  ...opts.body instanceof FormData ? {} : { "Content-Type": "application/json" },
5852
5937
  Accept: "application/json",
5853
5938
  ...opts.idempotencyKey ? { "Idempotency-Key": opts.idempotencyKey } : {}
@@ -5856,6 +5941,7 @@ var BotoZap = class {
5856
5941
  signal: opts.signal
5857
5942
  });
5858
5943
  } catch (cause) {
5944
+ if (cause instanceof BotoZapError) throw cause;
5859
5945
  const detail = cause instanceof Error ? cause.message : String(cause);
5860
5946
  throw new BotoZapError(
5861
5947
  "network_error",
@@ -5876,7 +5962,11 @@ var BotoZap = class {
5876
5962
  envelope?.error?.code ?? "http_error",
5877
5963
  envelope?.error?.message ?? `HTTP ${res.status}`,
5878
5964
  res.status,
5879
- headers
5965
+ headers,
5966
+ {
5967
+ outcome: envelope?.error?.outcome === "rejected" || envelope?.error?.outcome === "accepted" || envelope?.error?.outcome === "unknown" ? envelope.error.outcome : void 0,
5968
+ retry: envelope?.error?.retry === "backoff" || envelope?.error?.retry === "after_correction" || envelope?.error?.retry === "reconcile_first" || envelope?.error?.retry === "unknown" ? envelope.error.retry : void 0
5969
+ }
5880
5970
  );
5881
5971
  }
5882
5972
  return data;
@@ -5940,8 +6030,18 @@ function safeJson(text) {
5940
6030
  return text;
5941
6031
  }
5942
6032
  }
6033
+
6034
+ // src/types.ts
6035
+ var WEBHOOK_CATEGORIES = [
6036
+ "messages",
6037
+ "statuses",
6038
+ "crm",
6039
+ "account",
6040
+ "app_messages"
6041
+ ];
5943
6042
  export {
5944
6043
  AI_CONFIGURABLE_PURPOSES,
6044
+ AI_CREDENTIAL_PROVIDERS,
5945
6045
  AI_OPERATIONS,
5946
6046
  AI_PURPOSES,
5947
6047
  AgendaConfiguration,
@@ -5978,7 +6078,10 @@ export {
5978
6078
  CrmResource,
5979
6079
  Inbox,
5980
6080
  Journeys,
6081
+ PLAN_ERROR_CODES,
5981
6082
  Radar,
5982
- SavedReplies
6083
+ SavedReplies,
6084
+ WEBHOOK_CATEGORIES,
6085
+ isPlanError
5983
6086
  };
5984
6087
  //# sourceMappingURL=index.js.map