Edits history of script submission #23474 for ' Send Message (hubspot)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Send Message
     * Send a message on a conversation thread to a recipient. `sender_actor_id` is an agent actor ID (A-<user ID>), `channel_id` and `channel_account_id` come from the thread or List Channel Accounts, and `recipient_type` is the kind of address in `recipient_value` (e.g. HS_EMAIL_ADDRESS). Requires conversations.write.
     */
    export async function main(
      auth: RT.Hubspot,
      thread_id: string,
      text: string,
      sender_actor_id: string,
      channel_id: string,
      channel_account_id: string,
      recipient_type:
        | "HS_EMAIL_ADDRESS"
        | "HS_PHONE_NUMBER"
        | "CHANNEL_SPECIFIC_OPAQUE_ID",
      recipient_value: string,
      subject: string | undefined,
      rich_text: string | undefined,
      file_ids: string[] | undefined
    ) {
      const body: { [key: string]: any } = {
        type: "MESSAGE",
        text,
        senderActorId: sender_actor_id,
        channelId: channel_id,
        channelAccountId: channel_account_id,
        recipients: [
          {
            recipientField: "TO",
            deliveryIdentifiers: [{ type: recipient_type, value: recipient_value }],
          },
        ],
      }
      if (subject != null && subject !== "") body.subject = subject
      if (rich_text != null && rich_text !== "") body.richText = rich_text
      if (file_ids?.length) {
        body.attachments = file_ids.map((fileId) => ({ fileId, type: "FILE" }))
      }
    
      const response = await fetch(
        `https://api.hubapi.com/conversations/v3/conversations/threads/${thread_id}/messages`,
        {
          method: "POST",
          headers: {
            Authorization: `Bearer ${auth.token}`,
            "Content-Type": "application/json",
            Accept: "application/json",
          },
          body: JSON.stringify(body),
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 4 hours ago