Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. Skripten / Logik
    4. JavaScript
    5. Notion Integration in ioBroker Javascript

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    Notion Integration in ioBroker Javascript

    This topic has been deleted. Only users with topic management privileges can see it.
    • Ronny Gerndt
      Ronny Gerndt last edited by Ronny Gerndt

      Guten Tag,

      da im Adapter Request Github Repo (https://github.com/ioBroker/AdapterRequests/issues/731) die Frage aufkam wie ich im JS eine einfache Notion Integration hinbekommen habe möchte ich das hier mal kurz beschreiben. Wer notion nicht kennt findet hier Infos dazu: https://www.notion.so/product , ich nutze es aktuell für ToDo Listen, es kann aber noch einiges mehr.

      Zuerst benötigt man in Notion eine eigene Integration(https://www.notion.so/my-integrations)
      64161be9-a025-4812-a766-e5a6e31ceb7d-image.png Dazu einfach mittels New Integration eine neue anlegen, ein Name muss vergeben werden und der richtige Workspace muss gewählt werden. Der Rest kann auf Standard bleiben.

      Nach dem Anlegen einmal die neue Integration öffnen und den Token kopieren, dieser wird später benötigt.
      b13f27e0-4bd2-46c4-9b2e-2675997f3fad-image.png

      Anschließend muss in Notion an den Datenbanken/Dokumenten die ansprechbar sein sollen die neue Integration hinzugefügt werden. Dazu auf Share klicken und die Integration "Einladen".
      d279db40-0308-4735-abd4-f4a6679465a7-image.png

      Nun kommen wir zum ioBroker part. Hier muss in der Javascript Instanz das "Zusätzliche NPM-Module" "@notionhq/client" hinzugefügt werden.

      Ich werfe nun einfach mal meinen Code hier rein, der Token wurde oben kopiert. Die DatabaseId findet man in der URL.
      d580afb7-c103-42c0-bd07-d7434318ba1f-image.png

      const { Client } = require("@notionhq/client")
      
      const notion = new Client({ auth: 'Token der Integration' });
      
      const _databaseId = 'Database_Id';
      
      async function addItem(databaseId, text, state) {
          const response = await notion.pages.create({
              parent: { database_id: databaseId },
              properties: {
                  title: {
                      title:[
                          {
                              "text": {
                                  "content": text
                              }
                          }
                      ]
                  },
                  'Status': {
                      select: {
                          name: state
                      },
                  }
              }
          });
          return response.id;
      }
      
      async function getPage(pageId) {
          return await notion.pages.retrieve({ page_id: pageId });
      }
      
      async function getDatabase(databaseId) {
          return await notion.databases.retrieve({ database_id: databaseId });
      }
      
      async function getItemsInState(databaseId, state) {
          const response = await notion.databases.query({
              database_id: databaseId,
              filter: {
                  property: 'Status',
                  select: {
                      equals: state
                  }
              }
          });
          return response;
      }
      
      async function updateItemState(pageId, newState) {
          try {
              await notion.pages.update({
                  page_id: pageId,
                  properties: {
                      'Status': {
                          select: {
                              name:newState
                          },
                      },
                  },
              });
          } catch (error) {
              console.error(error.body)
          }
      }
      
      let itemId = await addItem(_databaseId, 'Test from iobroker', 'To Do');
      await updateItemState(itemId, 'Doing');
      var page = await getPage(itemId);
      var results = await getItemsInState(_databaseId, 'Doing');
      console.log(results);
      

      Das ganze legt nun einfach in meiner Task Liste einen neuen Eintrag mit dem Namen Test from ioBroker an und setzt den Status auf To Do.
      Anschließend wird der Status auf Doing aktualisiert.
      Und ein Beispiel für alle Einträge im Status Doing abzurufen.

      Alle weiteren Möglichkeiten der Api sind hier beschrieben: https://developers.notion.com/reference/intro

      Vllt hilft das dem ein oder anderen weiter. Wenn nicht ist auch ok 😁

      Grüße

      Ronny

      fabiankrauss created this issue in ioBroker/AdapterRequests

      open Notion #731

      1 Reply Last reply Reply Quote 1
      • First post
        Last post

      Support us

      ioBroker
      Community Adapters
      Donate

      811
      Online

      31.8k
      Users

      80.0k
      Topics

      1.3m
      Posts

      1
      1
      269
      Loading More Posts
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes
      Reply
      • Reply as topic
      Log in to reply
      Community
      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
      The ioBroker Community 2014-2023
      logo