Generate Nuki code triggered by Google Calendar Event

Hello,

I am a developer and looking to use the Nuki API to generate the code every time a google calendar event is created.

Any guidance will really help.

Has anyone done similar use case or can provide any info ?

TIA

  • Abhishek

Sure! To achieve this, you can create a Google Apps Script that interacts with the Nuki API and generates a new key every time a new event is created in your Google Calendar. Here’s a step-by-step guide to implementing this:

  1. Set up the Nuki API: Before writing the Google Apps Script, you need to set up an account with Nuki and obtain an API token or key that allows you to interact with their API. You should refer to Nuki’s API documentation for details on how to obtain the necessary credentials.

  2. Create a Google Apps Script: Go to https://script.google.com/ and create a new project. Give your project a name.

  3. Write the Apps Script: Replace "YOUR_NUKI_API_KEY" with your actual Nuki API key in the script below:

function createNukiKey() {
  var nukiAPIKey = "YOUR_NUKI_API_KEY"; // Replace with your Nuki API key
  var calendarId = "primary"; // You can change this to the specific calendar ID if needed

  var calendar = CalendarApp.getCalendarById(calendarId);
  var events = calendar.getEvents(new Date(), new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000)); // Look for events in the next 7 days

  if (events.length > 0) {
    var newEvent = events[0]; // Assuming the most recent event is at index 0
    var title = newEvent.getTitle();
    var description = newEvent.getDescription();
    var location = newEvent.getLocation();
    var startTime = newEvent.getStartTime();
    var endTime = newEvent.getEndTime();

    // Use the event details to create a new key via Nuki API
    var nukiKey = createKeyWithNukiAPI(nukiAPIKey, title, description, location, startTime, endTime);

    // Do something with the generated Nuki key (e.g., log it or send it somewhere)
    Logger.log("New Nuki Key: " + nukiKey);
  }
}

function createKeyWithNukiAPI(apiKey, title, description, location, startTime, endTime) {
  // Implement the Nuki API request here to create a new key
  // Use the apiKey, event details, or any other data required by the Nuki API

  // Sample code (replace this with actual API request)
  // var apiUrl = "https://api.nuki.io/create_key";
  // var payload = {
  //   "title": title,
  //   "description": description,
  //   "location": location,
  //   "startTime": startTime,
  //   "endTime": endTime,
  //   "apiKey": apiKey
  // };
  // var options = {
  //   "method": "post",
  //   "contentType": "application/json",
  //   "payload": JSON.stringify(payload)
  // };
  // var response = UrlFetchApp.fetch(apiUrl, options);
  // var data = JSON.parse(response.getContentText());
  // var nukiKey = data.key;

  // return nukiKey;

  // In this sample script, we're returning a random 10-character key (for demonstration purposes)
  return Math.random().toString(36).substr(2, 10);
}

  1. Save and run the script: Save the script, and you can manually run the createNukiKey function by clicking the play button on the toolbar. This will fetch the latest event from your calendar and use its details to generate a new key via the Nuki API. The key will be logged in the Google Apps Script console.
  2. Set up a trigger (optional): If you want this script to run automatically every time a new event is created, you can set up a trigger in Google Apps Script. Go to the “Triggers” menu, click on “Add Trigger,” and configure the trigger to run the createNukiKey function when “OnEventCreated” is the event type.

Please note that this is a basic example, and the actual implementation may vary