Edits history of script submission #16775 for ' Update draft (kustomer)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Kustomer = {
      apiKey: string;
    };
    /**
     * Update draft
     * Updates a draft.
    
    You can use this endpoint to update any available properties for a channel.
    
    ## `sendAt` property options
    
    * If the `sendAt` property is set to a valid timestamp, the draft will be scheduled to be sent.
    * If the `sendAt` property is empty, the draft will be unscheduled.
    * If the `sendAt` property is omitted, the draft status won't change.
     */
    export async function main(
      auth: Kustomer,
      id: string,
      body:
        | {
            channel: "email";
            conversation?: string;
            app?: "gmail" | "postmark";
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?:
              | { email: string; name?: string }[]
              | { email: string; name?: string };
            from?: { email: string; name?: string };
            body?: string;
            htmlBody?: string;
            cc?: { email: string; name?: string }[];
            bcc?: { email: string; name?: string }[];
            subject?: string;
            replyTo?: string;
            headers?: { name: string; value?: string }[];
            templateType?:
              | "email-reply"
              | "email-autoresponse"
              | "email-satisfaction";
            template?: string;
            payload?: {};
          }
        | {
            channel: "sms";
            app?: "twilio" | "zipwhip" | "messagebird";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "whatsapp";
            app: "messagebird" | "whatsapp" | "twilio_whatsapp";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "chat";
            app?: "chat" | "smooch";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
            responseButtons?: { label: string; value: string }[];
            messageTemplate?:
              | { id?: string; templateType: "text"; body: string }
              | {
                  id?: string;
                  templateType: "quick_replies";
                  body?: string;
                  actions: {
                    displayText: string;
                    valueType: "text" | "url";
                    value: string;
                  }[];
                }
              | {
                  id?: string;
                  templateType: "deflection";
                  body?: string;
                  actions: {
                    displayText: string;
                    valueType: "text" | "url";
                    value: string;
                  }[];
                  articles: { id: string; type?: string; attributes?: {} }[];
                  followupText?: string;
                }
              | {
                  id?: string;
                  templateType: "mll";
                  body?: string;
                  actions: { tree: {} };
                };
            attachments?: string[];
          }
        | {
            channel: "facebook";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "twitter-tweet";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "twitter-dm";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          }
        | {
            channel: "note";
            conversation?: string;
            customer?: string;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            body?: string;
            payload?: {};
            userMentions?: { user?: string; team?: string }[];
          }
        | {
            channel: "instagram";
            conversation?: string;
            customer?: string;
            auto?: false | true;
            scheduled?: false | true;
            sendAt?: string;
            source?: "bulk" | "satisfaction" | "biz-rules";
            lang?: string;
            shortcuts?: string[];
            kbArticles?: string[];
            to?: string;
            from?: string;
            body?: string;
            payload?: {};
          },
    ) {
      const url = new URL(`https://api.kustomerapp.com/v1/drafts/${id}`);
    
      const response = await fetch(url, {
        method: "PUT",
        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