Dueros付费技能实践

摘要 dueros中创建一个自定义付费技能

属于 :API 标签: 发布于:2019-10-29 17:42:16

dueros中创建一个自定义付费技能

首先,创建一个自定义技能。勾选【技能内付费】

复制技能ID.

1.获取token - get方式

https://xiaodu.baidu.com/dbppay/skill-pay/product/token?skillId=技能ID

得到响应

{"status":0,"msg":"","data":{"token":"fuwTfgALRjpoZXFqIZcF%2F%2Fnqn9OEra0Q%2FoSiZR5eqdacCZebL0%2BAWK1w0fuudX74J1nS8j2ri9AXC9lPI1DSGG9xcXFn6OjDKfYn5CDDR6y84GiPCus9GBBoS1ywsJJwX%2BHlxYXXIyOQhuMQIwm%2BHQ%3D%3D","expireSeconds":14400}}

2.创建商品 - post方式

https://xiaodu.baidu.com/dbppay/skill-pay/product/inskillproducts

头部header

Content-type: application/json
	Authorization: Bearer {{fuwTfgALRjpoZXFqIZcF%2F%2Fnqn9OEra0Q%2FoSiZR5eqdacCZebL0%2BAWK1w0fuudX74J1nS8j2ri9AXC9lPI1DSGG9xcXFn6OjDKfYn5CDDR6y84GiPCus9GBBoS1ywsJJwX%2BHlxYXXIyOQhuMQIwm%2BHQ%3D%3D}}

Authorization: Bearer {{token}}

请求 request body  [替换里面的技能ID]

{
    "version": "1.0",
    "referenceName": "tulongdao",
    "type": "ENTITLEMENT",
    "publishingInformation": {
        "name": "屠龙刀",
        "smallIconUri": "https://x.y.z/small.png",
        "largeIconUri": "https://x.y.z/large.png",
        "summary": "屠龙刀为武林高手剑魔独孤求败所用的玄铁重剑所造,因称「武林至尊」而被武林人视为宝物,引得各大门派纷纷争夺。",
        "category": "装备",
        "description": "襄阳城破之前,郭靖和黄蓉在「屠龙刀」和「倚天剑」中分别藏入《九阴真经》、《降龙十八掌》和《武穆遗书》,期望有缘人得之,推翻蒙古政权,光复大宋。并在江湖上散布「武林至尊,宝刀屠龙,号令天下,莫敢不从,倚天不出,谁与争锋」的传言。",
        "examplePhrases": [
            "购买屠龙刀",
            "来一把屠龙刀"
        ],
        "keywords": [
            "屠龙",
            "倚天"
        ],
        "customProductPrompts": {
            "purchasePromptDescription": "武林至尊,宝刀屠龙,倚天不出,谁与争锋",
            "boughtCardDescription": "武林至尊,宝刀屠龙,号令天下,莫敢不从,倚天不出,谁与争锋"
        },
        "pricing": {
            "originalPrice": 999,
            "price": 1
        },
        "skillId": "技能ID",
        "purchasableState": "PURCHASABLE"
    }
}

得到响应

{"status":0,"msg":"","data":{"productId":"xxxxx"}}


技能应答 自定义服务 java示例

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/pay")
public class DuController {
	@RequestMapping("/diss")
	@ResponseBody
	public String diss(@RequestBody String data, HttpServletResponse response){
		System.out.println("-- du diss start --- ");
		System.out.println(data);
		JSONObject obj = JSONObject.fromObject(data);
		JSONObject session = obj.getJSONObject("session");
		JSONObject context = obj.getJSONObject("context");
		JSONObject req = obj.getJSONObject("request");
		
		String res="";
		String type = req.getString("type");
		System.out.println("--"+type+"--");
		if("LaunchRequest".equals( type )){
			
			res="{\"version\":\"2.0\",\"context\":{\"intent\":null},\"session\":{\"attributes\":{}},\"response\":{\"directives\":[{\"type\":\"Display.RenderTemplate\",\"template\":{\"type\":\"BodyTemplate2\",\"token\":\"83cc3cfa-10b3-48f7-9fac-76681414f8c9\",\"content\":{\"type\":\"PlainText\",\"text\":\"${welcome}\"},\"image\":{\"url\":\"${logo}\",\"widthPixels\":200,\"heightPixels\":200},\"backgroundImage\":{\"url\":\"${bg}\"},\"title\":\"${title}\"}}],\"shouldEndSession\":false,\"card\":null,\"outputSpeech\":{\"type\":\"PlainText\",\"text\":\"${welcome}\"},\"reprompt\":null}}";
			res=res.replace("${welcome}", "欢迎使用技能");
			res=res.replace("${title}", "微信提醒");
			res=res.replace("${bg}", "https://www.yf2017.top/Public/assets/img/duerBkg.jpg");
			res=res.replace("${logo}", "https://skillstore.cdn.bcebos.com/icon/36/2a471c51-9059-207c-f81c-d76a91888396.png");
			System.out.println(res);
			return res;
		}else{
			System.out.println("--todo--");
		}
		return "success";
	}
}

launch 请求

{
	"version": "v2.0",
	"session": {
		"new": true,
		"sessionId": "defe6436-6b1a-42b8-8e1f-9cfbef32209a"
	},
	"context": {
		"System": {
			"user": {
				"userId": "236660257",
				"userInfo": {
					"account": {},
					"appLocation": {
						"geo": {
							"bd09ll": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"wgs84": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"bd09mc": {
								"longitude": -1.0,
								"latitude": -1.0
							}
						},
						"city": "",
						"district": "",
						"countryCode": "0",
						"cityCode": "0",
						"province": "",
						"street": "",
						"sematicDescription": ""
					}
				}
			},
			"application": {
				"applicationId": "aa69187d-ed6e-4926-14d2-3f84be8d8f6d"
			},
			"device": {
				"deviceId": "8dd202a62af8c7b8922e9dae00295a88",
				"supportedInterfaces": {
					"VoiceInput": {},
					"VoiceOutput": {},
					"AudioPlayer": {},
					"Display": {}
				},
				"TVControl": {
					"controlType": "DCSControl",
					"controlStatus": 0
				},
				"originalDeviceSerialNumber": ""
			},
			"apiAccessToken": "8QhbDu0O3xdUVzFPh/+XdPvTGg7ZEqO3648IF+Xj+J0TDBvUhPheqxAdR1Jb0Fb+xavZQxYflT6B3OyfA9ky9v40euO56ZojCl0tRM1saHMJqb0A0Padfb+MlqW4HSW3xPIG32Q4JRYZUTbhBrKuYtNmIj8OA1XZyXJ16Stow4fNfeZJ9NJVPIQokR8u2OcdnittLt52WUm/8XcyxbNjq2Aau5RqujSwriqDg06Hb+RcU0p/QYEC77YONIw0jwkFHH/tzH7m65JM8oLh3SjI8vCHgLEdAUWz8CkJRtgiCZHbR5DbkCf6LMh7HEGmEm2G4CYikPlfPkKPjH3yQPRsTOEg531dXTGM05/3uPgiYFIJTS2TrHYXHlIvtsMkPxocgSz5BLMg47yMTL9IOuiXwA==",
			"apiEndPoint": "https://xiaodu.baidu.com",
			"avaliableApplicationIds": ["doudi_server", "phone", "aries_general", "ai.dueros.bot.alarm", "duer_weather", "sac", "restaurant_bot", "o2o_satisfy", "rent_car", "travel_server", "recharge", "online_shopping", "hotel", "baojie", "nba_search", "sysprofile_service", "audio_music", "ai.dueros.bot.timer", "audio_unicast", "audio_live", "ai.dueros.bot.vocal_joke", "ai.dueros.bot.smarthome", "speaker_hardware", "audio_news"]
		}
	},
	"request": {
		"type": "LaunchRequest",
		"requestId": "61711645f1be4b688fff9be7d18cec5d_DCS196omwdungl7yc_0#1_0",
		"timestamp": "1572416179",
		"dialogRequestId": "425fc55f-4ef7-43c8-a811-353cc6e9e824"
	}
}

launch 响应

{
	"version": "2.0",
	"context": {
		"intent": null
	},
	"session": {
		"attributes": {}
	},
	"response": {
		"directives": [{
			"type": "Display.RenderTemplate",
			"template": {
				"type": "BodyTemplate2",
				"token": "83cc3cfa-10b3-48f7-9fac-76681414f8c9",
				"content": {
					"type": "PlainText",
					"text": "欢迎使用技能"
				},
				"image": {
					"url": "https://skillstore.cdn.bcebos.com/icon/36/2a471c51-9059-207c-f81c-d76a91888396.png",
					"widthPixels": 200,
					"heightPixels": 200
				},
				"backgroundImage": {
					"url": "https://www.yf2017.top/Public/assets/img/duerBkg.jpg"
				},
				"title": "微信提醒"
			}
		}],
		"shouldEndSession": false,
		"card": null,
		"outputSpeech": {
			"type": "PlainText",
			"text": "欢迎使用技能"
		},
		"reprompt": null
	}
}

购买逻辑实现

else if("IntentRequest".equals(type)){
			JSONArray intents = req.getJSONArray("intents");
			JSONObject intent = intents.getJSONObject(0);
			// 意图名称
			String intentName = intent.getString("name");
			System.out.println("--intent--"+intentName);
			if("puchurse".equals(intentName)){
				// 槽位
				JSONObject slots = intent.getJSONObject("slots");
				String no = slots.getJSONObject("sys.number").getString("value");
				if("1".equals(no)){
					res="{\"version\":\"2.0\",\"session\":{\"attributes\":{}},\"context\":{\"intent\":null},\"response\":{\"outputSpeech\":{\"type\":\"PlainText\",\"text\":\"请支付\"},\"reprompt\":{\"outputSpeech\":{\"type\":\"PlainText\",\"text\":\"请支付\"}},\"directives\":[{\"type\":\"Connections.SendRequest.Buy\",\"payload\":{\"productId\":\"191029172944717236\"},\"token\":\"acfd2993-d5a7-465d-a84d-d701bc6092ad\"}],\"shouldEndSession\":false}}";
					System.out.println(res);
					return res;
				}
			}
			// ai.dueros.common.default_intent  default
		}else{
			System.out.println("--todo--");
		}


报文详解

购买意图请求

{
	"version": "v2.0",
	"session": {
		"new": false,
		"sessionId": "b844d71b-3244-4e13-a36b-07d5e89c99c6",
		"attributes": {}
	},
	"context": {
		"System": {
			"user": {
				"userId": "35524460257",
				"userInfo": {
					"account": {},
					"appLocation": {
						"geo": {
							"bd09ll": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"wgs84": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"bd09mc": {
								"longitude": -1.0,
								"latitude": -1.0
							}
						},
						"city": "",
						"district": "",
						"countryCode": "0",
						"cityCode": "0",
						"province": "",
						"street": "",
						"sematicDescription": ""
					}
				}
			},
			"application": {
				"applicationId": "aa69187d-ed6e-4926-14d2-3f84be8d8f6d"
			},
			"device": {
				"deviceId": "708448dd202a62af8c7b8922e9dae00295a88",
				"supportedInterfaces": {
					"VoiceInput": {},
					"VoiceOutput": {},
					"AudioPlayer": {},
					"Display": {}
				},
				"TVControl": {
					"controlType": "DCSControl",
					"controlStatus": 0
				},
				"originalDeviceSerialNumber": ""
			},
			"apiAccessToken": "129AbDu0O3xdUVzFPAh/+XdPvTGg7ZEqO3648IF+Xj+J0TDBvUhPheqxAdR1Jb0Fb+xavZQxYflT6B3OyfA9ky9v40euO56ZojCl0tRM1saHMJqb0A0Padfb+MlqW4HSW3xPIG32Q4JRYZUTbhBrKuYtNmIj8OA1XZyXJ16Stow4e27xolVilf2CRoEKlIEeCinittLt52WUm/8XcyxbNjq2Aau5RqujSwriqDg06Hb+RcU0p/QYEC77YONIw0jwkFHH/tzH7m65JM8oLh3SjI8vCHgLEdAUWz8CkJRtgiCZGbBSMgUBIVQDf7+xFvp1GO4CYikPlfPkKPjH3yQPRsTOEg531dXTGM05/3uPgiYFIJTS2TrHYXHlIvtsMkPxocgSz5BLMg47yMTL9IOuiXwA==",
			"apiEndPoint": "https://xiaodu.baidu.com",
			"avaliableApplicationIds": ["doudi_server", "phone", "aries_general", "ai.dueros.bot.alarm", "duer_weather", "sac", "restaurant_bot", "o2o_satisfy", "rent_car", "travel_server", "recharge", "online_shopping", "hotel", "baojie", "nba_search", "sysprofile_service", "audio_music", "ai.dueros.bot.timer", "audio_unicast", "audio_live", "ai.dueros.bot.vocal_joke", "ai.dueros.bot.smarthome", "speaker_hardware", "audio_news"]
		},
		"Screen": {
			"token": "83cc3cfa-10b3-48f7-9fac-76681414f8c9",
			"template": {
				"image": {
					"url": "https://skillstore.cdn.bcebos.com/icon/36/2a471c51-9059-207c-f81c-d76a91888396.png",
					"widthPixels": 200,
					"heightPixels": 200
				},
				"backgroundImage": {
					"url": "https://www.yf2017.top/Public/assets/img/duerBkg.jpg"
				},
				"title": "微信提醒",
				"content": {
					"type": "PlainText",
					"text": "欢迎使用技能"
				},
				"type": "BodyTemplate2",
				"token": "83cc3cfa-10b3-48f7-9fac-76681414f8c9"
			}
		}
	},
	"request": {
		"query": {
			"type": "TEXT",
			"original": "购买第一个商品"
		},
		"dialogState": "COMPLETED",
		"intents": [{
			"name": "puchurse",
			"confirmationStatus": "NONE",
			"slots": {
				"sys.number": {
					"name": "sys.number",
					"value": "1",
					"values": ["1"],
					"confirmationStatus": "NONE",
					"analyses": [{
						"value": "1",
						"originalText": "1"
					}]
				}
			}
		}],
		"queryInfo": {},
		"type": "IntentRequest",
		"requestId": "2cae65b0de5d4e95b41ffe4ab8c07fb3_DCSos7p21ks9wu8cx_0#1_0",
		"timestamp": "1572417067",
		"dialogRequestId": "ad5462e9-1529-431f-9f1a-f1b44518b443"
	}
}

购买意图响应

{
	"version": "2.0",
	"session": {
		"attributes": {}
	},
	"context": {
		"intent": null
	},
	"response": {
		"outputSpeech": {
			"type": "PlainText",
			"text": "请支付"
		},
		"reprompt": {
			"outputSpeech": {
				"type": "PlainText",
				"text": "请支付"
			}
		},
		"directives": [{
			"type": "Connections.SendRequest.Buy",
			"payload": {
				"productId": "191029172944717236"
			},
			"token": "acfd2993-d5a7-465d-a84d-d701bc6092ad"
		}],
		"shouldEndSession": false
	}
}

buy扣款成功

{
    "type": "Connections.Response.Buy",
    "requestId": "{{STRING}}",
    "timestamp": {{INT32}},
    "token": "{{STRING}}",
    "payload": {
        "baiduOrderReferenceId": "{{STRING}}",
        "purchaseResult":"{{ENUM}}",
        "message":"{{STRING}}"
    }
}
  • type

    • 请求类型

  • requestId

    • 标识本次请求的唯一ID

  • timestamp

    • 请求时间戳,单位是秒,是一个全部是数字的字符串

  • token

    • Buy指令中提供的token

  • payload

    • 支付状态对应的信息

    • 此次支付结果

    • SUCCESS 支付成功

    • ERROR_SERVER DuerOS内部错误

    • ERROR_PRODUCT_NOT_EXIST 商品不存在

    • ERROR_PRODUCT_NOT_PUBLISHED 商品未发布上线,不允许购买

    • ERROR_REPEAT_PURCHASE 用户之前已购买过,无需再次购买

    • 此次交易百度生成的订单ID

    • baiduOrderReferenceId

    • purchaseResult

    • message

调用buy指令,有屏音箱弹出支付二维码,用户扫码支付完成后,消息推送给开发者            

{
	"version": "v2.0",
	"session": {
		"new": false,
		"sessionId": "261ba28d-47e5-483b-9689-1f0f7c299555",
		"attributes": {}
	},
	"context": {
		"System": {
			"user": {
				"userId": "12236660257",
				"userInfo": {
					"account": {},
					"appLocation": {
						"geo": {
							"bd09ll": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"wgs84": {
								"longitude": -1.0,
								"latitude": -1.0
							},
							"bd09mc": {
								"longitude": -1.0,
								"latitude": -1.0
							}
						},
						"city": "",
						"district": "",
						"countryCode": "0",
						"cityCode": "0",
						"province": "",
						"street": "",
						"sematicDescription": ""
					}
				}
			},
			"application": {
				"applicationId": "aa69187d-ed6e-4926-14d2-3f84be8d8f6d"
			},
			"device": {
				"deviceId": "4012364f42022f957b60e0e18df4d6f6f3",
				"supportedInterfaces": {
					"VoiceInput": {},
					"VoiceOutput": {},
					"AudioPlayer": {},
					"Display": {},
					"VideoPlayer": {
						"preferedBitrate": "",
						"preferedWidth": 0,
						"preferedHeight": 0
					},
					"DPL": {
						"version": "0.9.0"
					}
				},
				"TVControl": {
					"controlType": "DCSControl",
					"controlStatus": 0
				},
				"originalDeviceSerialNumber": ""
			},
			"apiAccessToken": "07QhbDu0O3xdUVzFPh/+XdPvTGg7ZEqO3648IF+Xj+J0TDBvUhPheqxAdR1Jb0Fb+MvywDFKgkIgWDk96vYH5wGB8JZEyBln4YMWcQ5ctO3nRFWIY/SiLHdrnr5JGixpW4NxSOj3UJBj6C1/wTGazah42Q/Wtxw1I8Epun4dSptIDkDrD+2imh7MQqfAp+YB5XBTfDGJM98Ed2U53pDVoG91cxj4U7oQv/fXf4t6e/igcf+3MfubrkkzyguHdKMjy8IeAsR0BRbPwKQlG2CIJkdtHkNuQJ/osyHscQaYSbYZd2k621d63Dxyeq26ADlCPb/LfHGoG2jNGKoY0QewPaglNLZOsdhceUi+2wyQ/GhyBLPkEsyDjvIxMv0g66JfA",
			"apiEndPoint": "https://xiaodu.baidu.com",
			"avaliableApplicationIds": ["doudi_server", "aries_general", "ai.dueros.bot.alarm", "ai.dueros.bot.timer", "duer_weather", "ai.dueros.bot.video_on_demand", "sysprofile_service", "audio_music", "ai.dueros.bot.ui_voice_interaction", "ai.dueros.bot.homecard", "audio_news", "audio_unicast", "ai.dueros.bot.vocal_joke", "speaker_hardware", "audio_live", "ai.dueros.bot.smarthome", "ai.dueros.bot.tv_control", "ai.dueros.bot.short_video", "ai.dueros.bot.faq", "ai.dueros.bot.audio_museum", "ai.dueros.bot.gimi_custom_command", "ai.dueros.bot.oss", "0b082db2-fc64-6396-5744-1ffd84e83a32", "ai.dueros.bot.content_square", "6402fc3a-7825-3147-b52e-c25911b6d03c", "0a7d4511-26d3-78ab-9652-2781dd6a073f", "user_profile", "chitchat", "baidu_search"]
		}
	},
	"request": {
		"token": "acfd2993-d5a7-465d-a84d-d701bc6092ad",
		"payload": {
			"goods": {
				"playUrl": ""
			},
			"baiduOrderReferenceId": "408758559157891996544",
			"sellerOrderId": "",
			"purchaseResult": "SUCCESS",
			"message": "",
			"sellerApplicationId": "",
			"productId": "",
			"orderName": "",
			"sellerAmount": "",
			"capturedAmount": "",
			"capturedTime": ""
		},
		"type": "Connections.Response.Buy",
		"requestId": "0a4897f3548b4b6090e38d476a741cea_DCSt11tl1ykcv2ow8_0",
		"timestamp": "1572447666",
		"dialogRequestId": "66fae2a3cae4e37ed1fc186b5746ac12"
	}
}

type类型为 

Connections.Response.Buy