@ask-utils/isp
v3.11.0
Published
Alexa ISP utilities
Downloads
103
Readme
ASK-Utils - ISP helpers
https://ask-utils.dev
NPM: https://www.npmjs.com/package/@ask-utils/isp
Alexa ISP (In Skill Purchasing) helpers. Easy, Shot, Simply.
Getting started
$ npm i -S @ask-utils/isp
Before ISP Utils
We have to take care of a lot of case to make a buy request.
import { getLocale } from 'ask-sdk-core'
const beforeISPUtils = {
handle() {
return true
},
async handler(handlerInput) {
if (!handlerInput.serviceClientFactory) throw new Error('No service client')
const client = handlerInput.serviceClientFactory.getMonetizationServiceClient()
const { inSkillProducts } = await client.getInSkillProducts(getLocale(handlerInput.requestEnvelope))
if (!inSkillProducts) throw new Error('No product')
const product = inSkillProducts.find(product => product.name === 'YOUR_PPRODUCT_NAME')
if (!product) throw new Error('no product')
return handlerInput.responseBuilder
.addDirective({
'type': 'Connections.SendRequest',
'name': 'Buy',
'payload': {
'InSkillProduct': {
'productId': product.productId
}
},
'token': 'correlationToken'
})
.getResponse()
}
}
We just call our client. Don't have to find or filter API response lists.
import { ISPProductClient, getBuyProductDirective } from '@ask-utils/isp'
const afterISPUtils = {
handle() {
return true
},
async handler(handlerInput) {
const client = new ISPProductClient(handlerInput)
const product = await client.searchProduct({
productName: 'YOUR_PPRODUCT_NAME'
})
if (!product) throw new Error('no product')
return handlerInput.responseBuilder
.addDirective(getBuyProductDirective(product.productId))
.getResponse()
}
}
Interceptors
Load ISP data
If start a new Session, it call ISP API to get your products and set to the session attributes. The idea is from this blog post. https://developer.amazon.com/ja/blogs/alexa/post/75ee61df-8365-44bb-b28f-e708000891ad/how-to-use-interceptors-to-simplify-handler-code-and-cache-product-and-purchase-information-in-monetized-alexa-skills
import { loadISPDataInterceptor } from '@ask-utils/isp'
// add interceptor
.addRequestInteceptor(loadISPDataInterceptor)
You can get the products by the following code.
// get product from session attributes
import { getAllEntitledProducts } from '@ask-utils/isp'
const { inSkillProducts } = handlerInput.attributesManager.getSessionAttributes()
const entitledProducts = getAllEntitledProducts(inSkillProducts)
General Handlers
You can add several handlers for ISP request.
import { SkillBuilders } from 'ask-sdk'
import {
ISPHandlers,
loadISPDataInterceptor
} from '@ask-utils/isp'
SkillBuilders.standard()
.addRequestHandlers(
...ISPHandlers,
)
.addRequestInterceptors(
loadISPDataInterceptor
)
.lambda()
development
$ git clone [email protected]:ask-utils/isp.git
$ cd isp
$ npm i
test
$ npm test
Lint
$ npm run lint
or
$ npm run lint -- --fix
History
-> Release Note