AutoVoice Natural language: webhook to IFTTT (no AV subscription required)

Discussion in 'Guides / Examples / Ideas Forum' started by c727, Jan 30, 2017.

  1. c727

    c727 New Member

    Joined:
    Jan 30, 2017
    Messages:
    2
    Likes Received:
    0
    info:
    • using natural language is great. This webhook is only a base you can use to handle your own actions using NL
    advantages:
    • send commands to IFTTT while your phone is offline, eg from Google Home
    • you don't need joao's server (pay for natural language support every month)
    requirements:
    • AutoVoice (if you want to use it on your phone, too)
    • IFTTT account
    • (free) webspace with php and curl
    • how to use api.ai (link your Google Home, set webhook, create intents/entities, ...) else check this video: www.youtube.com/watch?v=9SUAuy9OJg4 and read docs
    • how to use IFTTT + maker service, https://ifttt.com/maker
    • how to configure api.ai keys in AutoVoice
    • basic skills in web development
    webhook:
    This is an example webhook (I'm not a programmer) I use to send a notification (including commands) to my phone (I use AutoNotification to intercept notifcations from IFTTT app).
    You can link your maker to any other IFTTT service instead (configure it at IFTTT website).
    You could also add other web services than IFTTT.

    PHP:
    <?php
    // api.api to ifttt v0.1
    // config
    $iftKey = '<<your  key>>';

    $error = '';
    // from api.ai
    $in = file_get_contents('php://input');
    $req = json_decode($in, true);
    $action = $req['result']['action'];
    $param = $req['result']['parameters'];

    // to IFTTT
    switch ($action) {
        case 'to_tasker_on_phone':
            if (isset($param['cmdPhone'])) {
                switch ($param['cmdPhone']) {
                    case 'ring':
                        $data['value1'] = 'ring';
                        break;
                    case 'set_clipboard':
                        $data['value1'] = 'set_clipboard';
                        if (isset($param['textCb'])) {
                            $data['value2'] = $param['textCb'];
                        } else {
                            $data['value2'] = '(no text)';
                        }
                        break;
                    default:
                        $error = 'error webhook: cmdPhone not found';
                }
            } else {
                $error = 'error webhook: cmdPhone not set';
            }
            break;
        case 'harmony':
            // ...
            break;
        default:
            $error = 'error webhook: action not found';
    }

    if ($error == '') {
        $maker = 'https://maker.ifttt.com/trigger/' . $action . '/with/key/' . $iftKey;
        $ch = curl_init($maker);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        if (isset($data)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $iftRes = curl_exec($ch);
        curl_close($ch);

        if (substr($iftRes, 0, 16) == 'Congratulations!') {
            $text = 'Command sent';
        } else {
            $error = 'error webhook: IFTTT response';
        }
    }

    if ($error != '') {
        $text = $error;
    }

    // to api.ai
    $res['speech'] = $text;
    $res['displayText'] = $text;
    $res['contextOut'] = array();
    $res['source'] = 'webhook for ifttt';

    header('Content-Type: application/json');
    echo json_encode($res);
    ?>
     
    config for api.ai:
    (I use German language for this)
    [​IMG]
    IFTTT & Tasker:
    in my other post you can see the required setup for IFTTT and Tasker for this example: http://forum.joaoapps.com/index.php...owser-set-clipboard-or-ring-your-phone.13203/
     
    Last edited: Jan 31, 2017

Share This Page