background

  • Although tmall Genie has a lot of functions at present, there are still some highly customized services that cannot be satisfied, such as school schedule query, postgraduate entrance examination information, bus status, specific website status and so on. However, Tmall Genie offers open platform capabilities that make it easy to add these custom capabilities to The Tmall Genie.
  • This article makes a simple example, and the Tmall genie said xiaoai, and the Tmall genie replied “Xiaoai, I am the Tmall genie” to show how to invoke our custom service through the Tmall genie.
  • Refer to the document, we know that there are two key steps to achieve this function, one is to let tmall genie identify, the other is to build a custom service for reply

implementation

Registered skills

  • Tmall genie access is relatively simple, after registering developers, you can directly add custom skills on the platform. open.aligenie.com/console

1. Add custom ability “Xiao Ai Classmate”

2. In the sidebar “Intention” TAB, add intention

  • Since we are only making a simple call, it is good to use a simple default intention. Note that the default intention is not a backstop intention, but the intention that is triggered when only the key words are spoken
  • If it has multiple functions, example sentences can be used to teach the Tmall genie how to distinguish between different intentions,Document links

3. Configure the callback in the “Reply Logic” sidebar

  • You can see that we need to fill in the Webhook URL, which is the address of our actual custom service. We haven’t built it yet. Now we will build it with FAAS
  • Note that there is a validation file that the open platform will verify in the service root directory to verify the service’s ownership. Let’s download the file and use it laterDocument links

Service building

  • There are many ways to build services, here we choose aliyun’s FAAS service, that is, function calculation to build. The nice thing about functional computation is that you don’t have to worry about the real server, you just need to write your own custom logic. There are also 1 million free calls per month, equivalent to nothing at 😄. Fc.console.aliyun.com
  • The function is calculated as a three-tier structure1 service -> N functions -> M triggers. Each service can have multiple functions; Each function is a project with an entry function; This function is then called by triggers, such as HTTP triggers and timed triggers
  • It can be seen that what we need is to create a service, and then two functions, one for answering tmall genie, one for checking the right of Tmall genie.
  1. Create a new service with an arbitrary name
  2. Create two new functions named xiaoai and Auth respectively. The corresponding trigger names are respectivelyxiaoai_http.auth_httpIs used for reply and verification

Answering service

  • The answer service is very simple. It just returns “Xiao Ai, I am a Tmall genie”.
  • So we just need to receive the request directly back, the code is as follows, copy to the code execution of the online editor, click save
var getJsonBody = require('body/json');

exports.handler = (req, resp, context) = > {
    getJsonBody(req, function (err, data) {
        if(! data || ! data.utterance) { resp.send("post body wrong! " + data);
        }
        resp.setStatusCode(200);
        resp.setHeader('content-type'.'application/json');
        resp.send(JSON.stringify(
            {
                "returnCode": "0"."returnErrorSolution": ""."returnMessage": ""."returnValue": {
                    "reply": 'Xiaoai classmate, I am the Tmall genie."resultType": "RESULT"."executeCode": "SUCCESS"."msgInfo": ""}})); }); }Copy the code
  • In the test below, you can enter the body above to test the result
  • At this point, the reply service is complete

Check the service

  • The validation service is even simpler, which is to read from a file and return the contents of the file.
  • So open the verification file downloaded above, replace the contents of the file with XXX below, paste it into the edit box of code execution, and click Save.
exports.handler = (req, resp, context) = > {
    resp.setHeader('content-type'.'text/plain');
    resp.send("xxx")}Copy the code
  • Get directly from the test validation below to see that the body is returned as XXX above

Path to the registered

  • After the completion of the above two services, although you can directly access the corresponding url of each function trigger, because the authentication file must be in the root directory /aligenie/, so you need to customize the domain name to unify the domain name
  • Open the custom domain name, you can see that there is a default domain name, we directly edit the domain name, add two paths, one is/aligenie/aaa.txt, replace aaa with the file name of the authentication file downloaded above, one is/xiaoai/*

You’re done

  • After obtaining the address, change the Tmall Sprite Webhook address to our custom domain name above
  • The test can be carried out in the test verification. If the real machine test is opened, the test can be carried out on the Tmall genie bound to Taobao account

Tips

  • This article only demonstrates simple FAAS services, so direct page editing is used. Aliyun is recommended for complex business logic, such as installing dependency packages and debug testsfuncraftProvides a way to test the service locally and quickly publish it. Note that the configuration generated by template.yml does not contain logs. It is difficult to manually configure logs. You are advised to export the parameters configured on the interface
  • You can view the online running status by querying logs calculated by functions. For the first configuration, enable the Aliyun log service.

The resources

  • Tmall Genie document
  • Function to calculate