noelportugal / alexa-reminders

A nodejs library to create Amazon Echo Alexa Reminders
MIT License
18 stars 8 forks source link

Alexa Alarms? #1

Open keatontaylor opened 6 years ago

keatontaylor commented 6 years ago

So I wanted to know if you ever looked into extending this to support the creation or editing of alexa alarms. I have been trying to get the code to work myself by editing an existing alarm (I don't see an endpoint that allows create an alarm from scratch)

My issue is that the POST request that amazon expects includes a version number that increments anytime the alarm is edited, activated or canceled. I can fetch the version number from an endpoint including the id of the associated alarm, but when passing that number (amazon wants it as a string) in the POST request it fails, however hardcoding that value works every time. I've checked my JSON to make sure it was correct and am essentially do exactly what you are doing for fetching the device serial, etc. However, I am stuck.

Here is the code, maybe you can glean some light onto my issue.

var alarmStatus = {}

request({
  method: 'GET',
  url: 'https://alexa.amazon.com/api/notifications/<id_of_alarm_notification>?_=<request_timestamp>',
  headers: {
    'Cookie': config.cookies,
    'csrf': config.csrf
  }}, function(error, response, body) {
  if(!error && response.statusCode === 200) {
    alarmStatus = JSON.parse(body)
    config.version = alarmStatus.version
    callback(null, 'Fetched version number correctly ' + config.version)
  } else {
    callback(error, response)
  }
})

request({
  method: 'PUT',
  url: 'https://alexa.amazon.com/api/notifications/createAlarm',
  headers: {
    'Cookie': config.cookies,
    'csrf': config.csrf
  },
  json: {
    version: config.version,
    alarmIndex: null,
    alarmTime: alarmTime,
    createdDate: createdDate,
    deviceSerialNumber: config.deviceSerialNumber,
    deviceType: config.deviceType,
    id: '<id_of_alarm_notification>',
    isSaveInFlight: true,
    musicAlarmId: null,
    musicEntity: null,
    notificationIndex: '<index that is part of the alarm notification>',
    originalDate: null,
    originalTime: originalTime,
    provider: null,
    recurringPattern: null,
    remainingTime: 0,
    reminderLabel: null,
    sound: {'cid':'c11670','attributes':{'displayName':'Countertop','folder':null,'id':'system_alerts_repetitive_04','providerId':'ECHO','sampleUrl':'https://s3.amazonaws.com/deeappservice.prod.notificationtones/system_alerts_repetitive_04.mp3'},'_changing':false,'_previousAttributes':{},'changed':{},'id':'system_alerts_repetitive_04','_pending':false,'hasSynced':false,'_isExpired':false,'_listeners':{'l11671':{'displayName':'Countertop','folder':null,'id':'system_alerts_repetitive_04','providerId':'ECHO','sampleUrl':'https://s3.amazonaws.com/deeappservice.prod.notificationtones/system_alerts_repetitive_04.mp3'}},'_listenerId':'l11671','_events':{'sync':[{'context':{'displayName':'Countertop','folder':null,'id':'system_alerts_repetitive_04','providerId':'ECHO','sampleUrl':'https://s3.amazonaws.com/deeappservice.prod.notificationtones/system_alerts_repetitive_04.mp3'},'ctx':{'displayName':'Countertop','folder':null,'id':'system_alerts_repetitive_04','providerId':'ECHO','sampleUrl':'https://s3.amazonaws.com/deeappservice.prod.notificationtones/system_alerts_repetitive_04.mp3'}}]}},
    status: 'ON',
    timeZoneId: null,
    timerLabel: null,
    triggerTime: 0,
    type: 'Alarm',
  }
}, function(error, response, body) {
  if(!error && response.statusCode === 200) {
    callback(null, 'Your reminder "' + message + '" was created')
  } else {
    callback(error, 'error')
  }
})

I grabbed the ID's for the alarm using google developer tools and looking at the exchange between the server and the browser.

noelportugal commented 6 years ago

Interesting...I will take a look