Edits history of script submission #16500 for ' Create draft for customer (kustomer)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Kustomer = {
      apiKey: string;
    };
    /**
     * Create draft for customer
     * Creates an outbound draft to a customer through a specific channel (email, SMS, Facebook DM, etc).
     */
    export async function main(
      auth: Kustomer,
      id: string,
      body:
        | {
            channel: "email";
            conversation?: string;
            app?: "gmail" | "postmark";
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?:
              | { email: string; name?: string }[]
              | { email: string; name?: string };
            from?: { email: string; name?: string };
            body?: string;
            cc?: { email: string; name?: string }[];
            bcc?: { email: string; name?: string }[];
            subject?: string;
            replyTo?: string;
            headers?: { name: string; value?: string }[];
            template?: string;
            payload?: {};
          }
        | {
            channel: "sms";
            app?: "twilio" | "zipwhip";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | ({
            channel: "whatsapp";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            payload?: {};
          } & { app: "whatsapp" | "twilio_whatsapp" | "messagebird"; body: string })
        | ({
            channel: "whatsapp";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            payload?: {};
          } & {
            app: "whatsapp";
            meta: {
              interactive:
                | {
                    type: "list";
                    header?: { type: "text"; text: string };
                    body: { text: string };
                    footer?: { text: string };
                    action: {
                      button: string;
                      sections: {
                        title?: string;
                        rows: { id: string; title: string; description?: string }[];
                      }[];
                    };
                  }
                | {
                    type: "button";
                    header?: { type: "text"; text: string };
                    body: { text: string };
                    footer?: { text: string };
                    action: {
                      buttons: {
                        type: "reply";
                        reply: { id: string; title: string };
                      }[];
                    };
                  };
            };
          })
        | ({
            channel: "whatsapp";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            payload?: {};
          } & {
            app: "whatsapp";
            meta: {
              template: string;
              components?: {
                header?:
                  | { type: "text"; text: {} }
                  | { type: "image" | "video" | "document"; attachment: string };
                body?: {};
                buttons?: { index: number; sub_type: "url" }[];
              };
            };
          })
        | {
            channel: "chat";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
            attachments?: string[];
          }
        | {
            channel: "facebook";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "twitter-tweet";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "twitter-dm";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "note";
            conversation?: string;
            customer?: string;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            body?: string;
            payload?: {};
            userMentions?: { user?: string; team?: string }[];
          }
        | {
            channel: "instagram";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            sendAt?: string;
            scheduled?: false | true;
            source?: "bulk" | "satisfaction";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          },
    ) {
      const url = new URL(`https://api.kustomerapp.com/v1/customers/${id}/drafts`);
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.apiKey,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago