Closed bigalmac closed 10 months ago
so, for each of your end users, your code will need to obtain a new token for the event gateway, and store it. so your lambda function will need to handle AcceptGrant requests, pass the authcode provided to the AWS endpoint to retrieve a token. check the token you have locally and manage the refresh call if needed. Then you use that token to call the event gateway!
code snippets (These will not work as they are):
`function(event, context) { const usrToken = event.directive.payload.grantee.token; const token = 'Bearer ' + usrToken; const grantCode = event.directive.payload.grant.code;
region = 'EU'; if (lambdaRegion.indexOf('us-east') > -1) { region = 'US'; }
const requestBody = { grant_type: 'authorization_code', code: grantCode, client_id: 'amzn1.application-oa2-client.5', client_secret: 'q23sad2324344243243', }; const formData = querystring.stringify(requestBody);
axios.request({
url: '/auth/o2/token/',
method: 'post',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
baseURL: 'https://api.amazon.com',
data: formData,
}).then((result) => {
const accessToken = result.data.access_token;
const refreshToken = result.data.refresh_token;
// these are your end user's tokens - which you will need to store })
Here's the event gateway bit:
token = tokenYouHaveStoredForEachUser
if (urlRegion === 'US') { url = 'https://api.amazonalexa.com/v3/events'; } else { url = 'https://api.eu.amazonalexa.com/v3/events'; } requestify.request(url, { method: 'POST', headers: { 'content-type': 'application/json;charset=UTF-8', 'Authorization': token, }, body: postData, datatype: 'json', }).then(function(reply) { console.log(reply); const statusCode = reply.getCode(); }) ` HTH
FYI:
if you receive statusCode == 401 then you have to refresh the token and try again with a new token
You can see all the error codes are here (https://developer.amazon.com/docs/smarthome/send-events-to-the-alexa-event-gateway.html)
` request({ method: "POST", url: "https://api.amazon.com/auth/o2/token", headers: { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" }, body: "grant_type=refresh_token&refresh_token=" + awsRefreshToken
"&client_secret=" + alexaSkillMessagingClientSecret }) .then((body) => {
let alexaAuth = JSON.parse(body);
let alexa_access_token = alexaAuth.access_token;
let alexa_refresh_token = alexaAuth.refresh_token;
})
`
Closing. It seems addressed.
I would like to setup a doorbell device and have the announcement of a doorbell press on all alexa devices. I cannot see a sample message for a doorbell although there is a doorbell device in the alexa_smart_home_message_schema.json
I want the message to be proactively reported rather than sent as a notification so that it is announced.
Do i need to get my end user to enroll for proactive reporting or is this already implemented in the code?
I understand that i need to send a json message to an endpoint gateway as per this article:
https://developer.amazon.com/docs/smarthome/send-events-to-the-alexa-event-gateway.html
and my device is wifi enabled and can send a packet via http/s but I'm not clear how to proceed.
Any guidance or support would be appreciated