|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import requests |
| 4 | + |
| 5 | +from flask import Flask, json, render_template |
| 6 | +from flask_ask import Ask, request, session, question, statement, context, buy, upsell, refund, logger |
| 7 | +from model import Product |
| 8 | + |
| 9 | +app = Flask(__name__) |
| 10 | +ask = Ask(app, "/") |
| 11 | +logging.getLogger('flask_ask').setLevel(logging.DEBUG) |
| 12 | + |
| 13 | + |
| 14 | +PRODUCT_KEY = "PRODUCT" |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +@ask.on_purchase_completed( mapping={'payload': 'payload','name':'name','status':'status','token':'token'}) |
| 19 | +def completed(payload, name, status, token): |
| 20 | + products = Product(context.System.apiAccessToken) |
| 21 | + logger.info('on-purchase-completed {}'.format( request)) |
| 22 | + logger.info('payload: {} {}'.format(payload.purchaseResult, payload.productId)) |
| 23 | + logger.info('name: {}'.format(name)) |
| 24 | + logger.info('token: {}'.format(token)) |
| 25 | + logger.info('status: {}'.format( status.code == 200)) |
| 26 | + product_name = products.productName(payload.productId) |
| 27 | + logger.info('Product name'.format(product_name)) |
| 28 | + if status.code == '200' and ('ACCEPTED' in payload.purchaseResult): |
| 29 | + return question('To listen it just say - play {} '.format(product_name)) |
| 30 | + else: |
| 31 | + return question('Do you want to buy another product?') |
| 32 | + |
| 33 | +@ask.launch |
| 34 | +def launch(): |
| 35 | + products = Product(context.System.apiAccessToken) |
| 36 | + question_text = render_template('welcome', products=products.list()) |
| 37 | + reprompt_text = render_template('welcome_reprompt') |
| 38 | + return question(question_text).reprompt(reprompt_text).simple_card('Welcome', question_text) |
| 39 | + |
| 40 | + |
| 41 | +@ask.intent('BuySkillItemIntent', mapping={'product_name': 'ProductName'}) |
| 42 | +def buy_intent(product_name): |
| 43 | + products = Product(context.System.apiAccessToken) |
| 44 | + logger.info("PRODUCT: {}".format(product_name)) |
| 45 | + buy_card = render_template('buy_card', product=product_name) |
| 46 | + productId = products.productId(product_name) |
| 47 | + if productId is not None: |
| 48 | + session.attributes[PRODUCT_KEY] = productId |
| 49 | + else: |
| 50 | + return statement("I didn't find a product {}".format(product_name)) |
| 51 | + raise NotImplementedError() |
| 52 | + return buy(productId).simple_card('Welcome', question_text) |
| 53 | + |
| 54 | + #return upsell(product,'get this great product') |
| 55 | + |
| 56 | + |
| 57 | +@ask.intent('RefundSkillItemIntent', mapping={'product_name': 'ProductName'}) |
| 58 | +def refund_intent(product_name): |
| 59 | + refund_card = render_template('refund_card') |
| 60 | + logger.info("PRODUCT: {}".format(product_name)) |
| 61 | + |
| 62 | + products = Product(context.System.apiAccessToken) |
| 63 | + productId = products.productId(product_name) |
| 64 | + |
| 65 | + if productId is not None: |
| 66 | + session.attributes[PRODUCT_KEY] = productId |
| 67 | + else: |
| 68 | + raise NotImplementedError() |
| 69 | + return refund(productId) |
| 70 | + |
| 71 | + |
| 72 | +@ask.intent('AMAZON.FallbackIntent') |
| 73 | +def fallback_intent(): |
| 74 | + return statement("FallbackIntent") |
| 75 | + |
| 76 | + |
| 77 | +@ask.session_ended |
| 78 | +def session_ended(): |
| 79 | + return "{}", 200 |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + if 'ASK_VERIFY_REQUESTS' in os.environ: |
| 84 | + verify = str(os.environ.get('ASK_VERIFY_REQUESTS', '')).lower() |
| 85 | + if verify == 'false': |
| 86 | + app.config['ASK_VERIFY_REQUESTS'] = False |
| 87 | + app.run(debug=True) |
| 88 | + |
0 commit comments