这是一个示例代码,可以帮助您和其他希望向alexa网关发送事件的人。
const AWS = require('aws-sdk'); AWS.config.update({region: 'eu-west-1'}); // Create the DynamoDB service object const ddb = new AWS.DynamoDB({ apiVersion: 'latest' }); const doc = new AWS.DynamoDB.DocumentClient({ convertEmptyValues: true, service: ddb }); // Using 'request' for http POST and GET request. // https://www.npmjs.com/package/requests // npm install --save requests const r = require('request'); //Handle Authorization. Call this method from your lambda handler whenever you get Alexa.Authorization message. You will get this message only when you select permission to //send events in your Smart Home Skill. //Access to Event gateway allows you to enable Proactive Device Discovery and //Proactive State Reporting in your skill //More information on Alexa.Authorization can be found on https://developer.amazon.com/docs/device-apis/alexa-authorization.html function handleAuthorization(request, context, user) { //Even when you are using your own authentication, the url below will still //point to amazon OAuth token url. The token you obtain here has to be stored //separately for this user. Whenever sending an event to alexa event gateway you will //require this token. //URL below is for EU server. Look at following documentation link to identify correct url //for your system. //https://developer.amazon.com/docs/smarthome/send-events-to-the-alexa-event-gateway.html var url = "https://api.amazon.com/auth/o2/token"; var body = { grant_type : 'authorization_code', code : request.directive.payload.grant.code, client_id : 'your client id from permissions page on developer portal where you enable alexa events. This is id different than one you specify in account linking settings', client_secret : 'client secret from permissions page' } //https://developer.amazon.com/docs/smarthome/authenticate-a-customer-permissions.html r.post({ url: url, form : body }, function(error, response, b){ if (error) { return console.log(error); } var body = JSON.parse(b); var params = { TableName: 'Devices', Item: { 'id' : user, 'auth_token' : body.access_token, 'refresh_token' : body.refresh_token } } log("DEBUG:", "Authorization Body", JSON.stringify(body)); log("DEBUG:", "Authorization Response", JSON.stringify(response)); log("DEBUG:", "Database Params", JSON.stringify(params)); // Call DynamoDB to add the item to the table var putObjectPromise = doc.put(params).promise(); //Store auth_token and refresh_token in database. We will need these //while sending events to event gateway. //Send a success response. putObjectPromise.then(function(data) { var response = { event: { header: { messageId: request.directive.header.messageId, namespace: "Alexa.Authorization", name: "AcceptGrant.Response", payloadVersion: "3" }, "payload": { } } }; context.succeed(response); }).catch(function(err) { //TODO - Add a Authorization error response JSON here. console.log(err); }); }); }