Edits history of script submission #23126 for ' Create Comment (notion)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Create Comment
     * Add a comment to a page, or reply in an existing discussion thread by passing `discussion_id` instead of `page_id`. Needs the insert comments capability.
     */
    export async function main(
      auth: RT.Notion,
      text: string,
      page_id: string | undefined,
      discussion_id: string | undefined
    ) {
      const body: { [key: string]: any } = {
        rich_text: [{ text: { content: text } }],
      }
      if (discussion_id !== undefined && discussion_id !== "") {
        body.discussion_id = discussion_id
      } else {
        body.parent = { page_id }
      }
    
      const response = await fetch("https://api.notion.com/v1/comments", {
        method: "POST",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          "Content-Type": "application/json",
          Accept: "application/json",
          "Notion-Version": "2026-03-11",
        },
        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