前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Cozmo人工智能机器人SDK使用笔记(9)-判断部分if_this_then_that

Cozmo人工智能机器人SDK使用笔记(9)-判断部分if_this_then_that

作者头像
zhangrelay
发布2019-01-31 16:06:05
5710
发布2019-01-31 16:06:05
举报

Cozmo判断部分有3个主题:

  1. gmail
  2. sport
  3. stock

1. gmail

此示例演示了如何使用“If This Then That”(http://ifttt.com)使Cozmo在Gmail帐户收到电子邮件时作出回应。以下说明将引导您在IFTTT网站上设置小程序。当调用applet触发器(发送在此示例中启动的Web服务器收到的Web请求)时,Cozmo将播放动画,说出电子邮件发件人的姓名并在他的脸上显示邮箱图像。

If This Then That" Gmail example This example demonstrates how "If This Then That" (http://ifttt.com) can be used make Cozmo respond when a Gmail account receives an email. Instructions below will lead you through setting up an applet on the IFTTT website. When the applet trigger is called (which sends a web request received by the web server started in this example), Cozmo will play an animation, speak the email sender's name and show a mailbox image on his face. Please place Cozmo on the charger for this example. When necessary, he will be rolled off and back on. Follow these steps to set up and run the example:     1) Provide a a static ip, URL or similar that can be reached from the If This         Then That server. One easy way to do this is with ngrok, which sets up         a secure tunnel to localhost running on your machine.         To set up ngrok:         a) Follow instructions here to download and install:             https://ngrok.com/download         b) Run this command to create a secure public URL for port 8080:             ./ngrok http 8080         c) Note the HTTP forwarding address shown in the terminal (e.g., http://55e57164.ngrok.io).             You will use this address in your applet, below.         WARNING: Using ngrok exposes your local web server to the internet. See the ngrok         documentation for more information: https://ngrok.com/docs     2) Set up your applet on the "If This Then That" website.         a) Sign up and sign into https://ifttt.com         b) Create an applet: https://ifttt.com/create         c) Set up your trigger.             1. Click "this".             2. Select "Gmail" as your service. If prompted, click "Connect",                 select your Gmail account, and click “Allow” to provide permissions                 to IFTTT for your email account. Click "Done".             3. Under "Choose a Trigger", select “Any new email in inbox".         d) Set up your action.             1. Click “that".             2. Select “Maker Webhooks" to set it as your action channel. Connect to the Maker channel if prompted.             3. Click “Make a web request" and fill out the fields as follows. Remember your publicly                 accessible URL from above (e.g., http://55e57164.ngrok.io) and use it in the URL field,                 followed by "/iftttGmail" as shown below:                  URL: http://55e57164.ngrok.io/iftttGmail                  Method: POST                  Content Type: application/json                  Body: {"FromAddress":"{{FromAddress}}"}             5. Click “Create Action" then “Finish".     3) Test your applet.         a) Run this script at the command line: ./ifttt_gmail.py         b) On ifttt.com, on your applet page, click “Check now”. See that IFTTT confirms that the applet             was checked.         c) Send an email to the Gmail account in your recipe         d) On your IFTTT applet webpage, again click “Check now”. This should cause IFTTT to detect that             the email was received and send a web request to the ifttt_gmail.py script.         e) In response to the ifttt web request, Cozmo should roll off the charger, raise and lower             his lift, announce the email, and then show a mailbox image on his face.

代码语言:javascript
复制
import asyncio
import re
import sys


try:
    from aiohttp import web
except ImportError:
    sys.exit("Cannot import from aiohttp. Do `pip3 install --user aiohttp` to install")

import cozmo

from common import IFTTTRobot


app = web.Application()


async def serve_gmail(request):
    '''Define an HTTP POST handler for receiving requests from If This Then That.

    You may modify this method to change how Cozmo reacts to the email
    being received.
    '''

    json_object = await request.json()

    # Extract the name of the email sender.
    from_email_address = json_object["FromAddress"]

    # Use a regular expression to break apart pieces of the email address
    match_object = re.search(r'([\w.]+)@([\w.]+)', from_email_address)
    email_local_part = match_object.group(1)

    robot = request.app['robot']
    async def read_name():
        try:
            async with robot.perform_off_charger():
                '''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face.'''
                await robot.get_in_position()

                # First, have Cozmo play an animation
                await robot.play_anim_trigger(cozmo.anim.Triggers.ReactToPokeStartled).wait_for_completed()

                # Next, have Cozmo speak the name of the email sender.
                await robot.say_text("Email from " + email_local_part).wait_for_completed()

                # Last, have Cozmo display an email image on his face.
                robot.display_image_file_on_face("../face_images/ifttt_gmail.png")

        except cozmo.RobotBusy:
            cozmo.logger.warning("Robot was busy so didn't read email address: "+ from_email_address)

    # Perform Cozmo's task in the background so the HTTP server responds immediately.
    asyncio.ensure_future(read_name())

    return web.Response(text="OK")

# Attach the function as an HTTP handler.
app.router.add_post('/iftttGmail', serve_gmail)


if __name__ == '__main__':
    cozmo.setup_basic_logging()
    cozmo.robot.Robot.drive_off_charger_on_connect = False

    # Use our custom robot class with extra helper methods
    cozmo.conn.CozmoConnection.robot_factory = IFTTTRobot

    try:
        app_loop = asyncio.get_event_loop()  
        sdk_conn = cozmo.connect_on_loop(app_loop)

        # Wait for the robot to become available and add it to the app object.
        app['robot'] = app_loop.run_until_complete(sdk_conn.wait_for_robot())
    except cozmo.ConnectionError as e:
        sys.exit("A connection error occurred: %s" % e)

    web.run_app(app)

2. sport

此示例演示了如何使用“If This Then That”(http://ifttt.com)使Cozmo在您指定的团队进行游戏内或最终分数更新时做出响应。 以下说明将引导您在IFTTT网站上设置小程序。 当调用applet触发器(发送在此示例中启动的Web服务器接收的Web请求)时,Cozmo将播放动画,在他的脸上显示图像,并说出游戏内更新。

If This Then That" sports example This example demonstrates how "If This Then That" (http://ifttt.com) can be used make Cozmo respond when there is an in-game or final score update for the team you specify. Instructions below will lead you through setting up an applet on the IFTTT website. When the applet trigger is called (which sends a web request received by the web server started in this example), Cozmo will play an animation, show an image on his face, and speak the in-game update. Please place Cozmo on the charger for this example. When necessary, he will be rolled off and back on. Follow these steps to set up and run the example:     1) Provide a a static ip, URL or similar that can be reached from the "If This         Then That" server. One easy way to do this is with ngrok, which sets up         a secure tunnel to localhost running on your machine.         To set up ngrok:         a) Follow instructions here to download and install:             https://ngrok.com/download         b) Run this command to create a secure public URL for port 8080:             ./ngrok http 8080         c) Note the HTTP forwarding address shown in the terminal (e.g., http://55e57164.ngrok.io).             You will use this address in your applet, below.         WARNING: Using ngrok exposes your local web server to the internet. See the ngrok         documentation for more information: https://ngrok.com/docs     2) Set up your applet on the "If This Then That" website.         a) Sign up and sign into https://ifttt.com         b) Create an applet: https://ifttt.com/create         c) Set up your trigger.             1. Click "this".             2. Select "ESPN" as your service.             3. Under "Choose a Trigger", select “New in-game update".             4. In section "Complete Trigger Fields", enter your sport and team and click “Create Trigger".         d) Set up your action.             1. Click “that".             2. Select “Maker Webhooks" to set it as your action channel. Connect to the Maker channel if prompted.             3. Click “Make a web request" and fill out the fields as follows. Remember your publicly                 accessible URL from above (e.g., http://55e57164.ngrok.io) and use it in the URL field,                 followed by "/iftttSports" as shown below:                  URL: http://55e57164.ngrok.io/iftttSports                  Method: POST                  Content Type: application/json                  Body: {"AlertBody":"{{AlertBody}}"}             5. Click “Create Action" then “Finish".     3) Test your applet.         a) Run this script at the command line: ./ifttt_sports.py         b) On ifttt.com, on your applet page, click “Check now”. See that IFTTT confirms that the applet             was checked.         c) Wait for new in-game updates for your team and see Cozmo react! Cozmo should roll off the charger, raise             and lower his lift, show an image on his face and speak the in-game update.

代码语言:javascript
复制
import asyncio
import sys


try:
    from aiohttp import web
except ImportError:
    sys.exit("Cannot import from aiohttp. Do `pip3 install --user aiohttp` to install")

import cozmo

from common import IFTTTRobot


app = web.Application()


async def serve_sports(request):
    '''Define an HTTP POST handler for receiving requests from If This Then That.

    You may modify this method to change how Cozmo reacts to
    an in-game update from IFTTT.
    '''

    json_object = await request.json()

    # Extract the text for the in-game update.
    alert_body = json_object["AlertBody"]

    robot = request.app['robot']
    async def read_name():
        try:
            async with robot.perform_off_charger():
                '''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face.'''
                await robot.get_in_position()

                # First, have Cozmo play an animation
                await robot.play_anim_trigger(cozmo.anim.Triggers.ReactToPokeStartled).wait_for_completed()

                # Next, have Cozmo speak the text from the in-game update.
                await robot.say_text(alert_body).wait_for_completed()

                # Last, have Cozmo display a sports image on his face.
                robot.display_image_file_on_face("../face_images/ifttt_sports.png")

        except cozmo.RobotBusy:
            cozmo.logger.warning("Robot was busy so didn't read update: '" + alert_body +"'")

    # Perform Cozmo's task in the background so the HTTP server responds immediately.
    asyncio.ensure_future(read_name())

    return web.Response(text="OK")

# Attach the function as an HTTP handler.
app.router.add_post('/iftttSports', serve_sports)


if __name__ == '__main__':
    cozmo.setup_basic_logging()
    cozmo.robot.Robot.drive_off_charger_on_connect = False

    # Use our custom robot class with extra helper methods
    cozmo.conn.CozmoConnection.robot_factory = IFTTTRobot

    try:
        app_loop = asyncio.get_event_loop()  
        sdk_conn = cozmo.connect_on_loop(app_loop)

        # Wait for the robot to become available and add it to the app object.
        app['robot'] = app_loop.run_until_complete(sdk_conn.wait_for_robot())
    except cozmo.ConnectionError as e:
        sys.exit("A connection error occurred: %s" % e)

    web.run_app(app)

此IFTTT示例使用Flask Web框架并且是同步的,与其他异步的IFTTT示例不同并使用aiohttp。在此示例中,IFTTT Web请求由函数receive_ifttt_web_request接收。进入该方法的每个Web请求都会添加到Queue ifttt_queue,并且HTTP状态代码200或503会立即返回到IFTTT。该队列由worker函数检查,该函数在后台线程上运行(由run函数启动)。当worker函数在队列中找到新请求时,请求将从队列中删除并在方法then_that_action中处理。

与ifttt_sports.py示例一样,此示例演示了如何使用“If This Then That”(http://ifttt.com)使Cozmo在您指定的团队有游戏内或最终得分更新时做出响应。以下说明将引导您完成设置 在IFTTT网站上发布一个applet。当调用applet触发器(发送在此示例中启动的烧瓶服务器接收的Web请求)时,Cozmo将播放动画,在他的脸上显示图像,并说出游戏内更新。

If This Then That" sports example This IFTTT example uses the Flask web framework and is synchronous, unlike the other IFTTT examples which are asynchronous and use aiohttp. In this example, IFTTT web requests are received by function receive_ifttt_web_request. Each web request that comes into that method is added to Queue ifttt_queue and an HTTP status code of 200 or 503 is immediately returned to IFTTT. The queue is checked by the worker function, which is running on a background thread (started by the run function). When a new request is found on the queue by the worker function, the request is removed from the queue and processed in method then_that_action. Like the ifttt_sports.py example, this example demonstrates how "If This Then That" (http://ifttt.com) can be used make Cozmo respond when there is an in-game or final score update for the team you specify. Instructions below will lead you through setting up an applet on the IFTTT website. When the applet trigger is called (which sends a web request received by the flask server started in this example), Cozmo will play an animation, show an image on his face, and speak the in-game update. Please place Cozmo on the charger for this example. When necessary, he will be rolled off and back on. Follow these steps to set up and run the example:     1) Provide a a static ip, URL or similar that can be reached from the "If This         Then That" server. One easy way to do this is with ngrok, which sets up         a secure tunnel to localhost running on your machine.         To set up ngrok:         a) Follow instructions here to download and install:             https://ngrok.com/download         b) Run this command to create a secure public URL for port 8080:             ./ngrok http 8080         c) Note the HTTP forwarding address shown in the terminal (e.g., http://55e57164.ngrok.io).             You will use this address in your applet, below.         WARNING: Using ngrok exposes your local web server to the internet. See the ngrok         documentation for more information: https://ngrok.com/docs     2) Set up your applet on the "If This Then That" website.         a) Sign up and sign into https://ifttt.com         b) Create an applet: https://ifttt.com/create         c) Set up your trigger.             1. Click "this".             2. Select "ESPN" as your service.             3. Under "Choose a Trigger", select “New in-game update".             4. In section "Complete Trigger Fields", enter your sport and team and click “Create Trigger".         d) Set up your action.             1. Click “that".             2. Select “Maker" to set it as your action channel. Connect to the Maker channel if prompted.             3. Click “Make a web request" and fill out the fields as follows. Remember your publicly                 accessible URL from above (e.g., http://55e57164.ngrok.io) and use it in the URL field,                 followed by "/iftttSports" as shown below:                  URL: http://55e57164.ngrok.io/iftttSports                  Method: POST                  Content Type: application/json                  Body: {"AlertBody":"{{AlertBody}}"}             5. Click “Create Action" then “Finish".     3) Test your applet.         a) Run this script at the command line: ./ifttt_sports.py         b) On ifttt.com, on your applet page, click “Check now”. See that IFTTT confirms that the applet             was checked.         c) Wait for new in-game updates for your team and see Cozmo react! Cozmo should roll off the charger, raise             and lower his lift, show an image on his face and speak the in-game update.

代码语言:javascript
复制
import json
import queue
import sys
import threading

import cozmo
sys.path.append('../lib/')
import flask_helpers

from common import IFTTTRobot


try:
    from flask import Flask, request
except ImportError:
    sys.exit("Cannot import from flask: Do `pip3 install --user flask` to install")


flask_app = Flask(__name__)
ifttt_queue = queue.Queue()
robot = None


@flask_app.route('/iftttSports', methods=['POST'])
def receive_ifttt_web_request():
    '''Web request endpoint named "iftttSports" for IFTTT to call when a new in-game
        update for your team is posted on ESPN.

        In the IFTTT web request, in the URL field, specify this method
        as the endpoint. For instance, if your public url is http://my.url.com,
        then in the IFTTT web request URL field put the following:
        http://my.url.com/iftttSports. Then, this endpoint will be called when
        IFTTT checks and discovers that a new in-game update for your team is
        posted on ESPN.
    '''

    # Retrieve the data passed by If This Then That in the web request body.
    json_object = json.loads(request.data.decode("utf-8"))

    # Extract the text for the in-game update.
    alert_body = json_object["AlertBody"]

    # Add this request to the queue of in-game updates awaiting Cozmo's reaction.
    ifttt_queue.put((then_that_action, alert_body))

    # Return promptly so If This Then That knows that the web request was received
    # successfully.
    return ""


def then_that_action(alert_body):
    '''Controls how Cozmo responds to the in-game update.

    You may modify this method to change how Cozmo reacts to
    the update from IFTTT.
    '''

    try:
        with robot.perform_off_charger():
            '''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face.'''
            robot.get_in_position()

            # First, have Cozmo play an animation
            robot.play_anim_trigger(cozmo.anim.Triggers.ReactToPokeStartled).wait_for_completed()

            # Next, have Cozmo speak the text from the in-game update.
            robot.say_text(alert_body).wait_for_completed()

            # Last, have Cozmo display a sports image on his face.
            robot.display_image_file_on_face("../face_images/ifttt_sports.png")

    except cozmo.exceptions.RobotBusy:
        pass


def worker():
    while True:
        item = ifttt_queue.get()
        if item is None:
            break
        queued_action, action_args = item
        queued_action(action_args)


def run(sdk_conn):
    global robot
    robot = sdk_conn.wait_for_robot()

    threading.Thread(target=worker).start()

    # Start flask web server so that /iftttSports can serve as endpoint.
    flask_helpers.run_flask(flask_app, "127.0.0.1", 8080, False, False)

    # Putting None on the queue stops the thread. This is called when the
    # user hits Control C, which stops the run_flask call.
    ifttt_queue.put(None)


if __name__ == '__main__':
    cozmo.setup_basic_logging()
    cozmo.robot.Robot.drive_off_charger_on_connect = False

    # Use our custom robot class with extra helper methods
    cozmo.conn.CozmoConnection.robot_factory = IFTTTRobot

    try:
        cozmo.connect(run)
    except cozmo.ConnectionError as e:
        sys.exit("A connection error occurred: %s" % e)

3. stock

此示例演示了如何使用“If This Then That”(http://ifttt.com)使Cozmo在股票代码符号增加1%或更多时作出响应。 以下说明将引导您在IFTTT网站上设置小程序。 当调用applet触发器(发送在此示例中启动的Web服务器接收的Web请求)时,Cozmo将播放动画,说出公司名称和增加的百分比,并在他的脸上显示股票市场图像。

If This Then That" Stock example This example demonstrates how "If This Then That" (http://ifttt.com) can be used make Cozmo respond when a stock ticker symbol increases by 1% or more. Instructions below will lead you through setting up an applet on the IFTTT website. When the applet trigger is called (which sends a web request received by the web server started in this example), Cozmo will play an animation, speak the company name and the percentage increase, and show a stock market image on his face. Please place Cozmo on the charger for this example. When necessary, he will be rolled off and back on. Follow these steps to set up and run the example:     1) Provide a a static ip, URL or similar that can be reached from the If This         Then That server. One easy way to do this is with ngrok, which sets up         a secure tunnel to localhost running on your machine.         To set up ngrok:         a) Follow instructions here to download and install:             https://ngrok.com/download         b) Run this command to create a secure public URL for port 8080:             ./ngrok http 8080         c) Note the HTTP forwarding address shown in the terminal (e.g., http://55e57164.ngrok.io).             You will use this address in your applet, below.         WARNING: Using ngrok exposes your local web server to the internet. See the ngrok         documentation for more information: https://ngrok.com/docs     2) Set up your applet on the "If This Then That" website.         a) Sign up and sign into https://ifttt.com         b) Create an applet: https://ifttt.com/create         c) Set up your trigger.             1. Click "this".             2. Select "Stocks" as your service.             3. Under "Choose a Trigger", select “Today's price rises by percentage".             4. In section "Complete Trigger Fields", enter your ticker symbol and desired percentage,                 for instance:                 Ticker symbol: HOG                 Percentage increase: 1             5. Click “Create Trigger".         d) Set up your action.             1. Click “that".             2. Select “Maker Webhooks" to set it as your action channel. Connect to the Maker channel if prompted.             3. Click “Make a web request" and fill out the fields as follows. Remember your publicly                 accessible URL from above (e.g., http://55e57164.ngrok.io) and use it in the URL field,                 followed by "/iftttStocks" as shown below:                  URL: http://55e57164.ngrok.io/iftttStocks                  Method: POST                  Content Type: application/json                  Body: {"PercentageChange":"{{PercentageChange}}","StockName":"{{StockName}}"}             5. Click “Create Action" then “Finish".     3) Test your applet.         a) Run this script at the command line: ./ifttt_stocks.py         b) On ifttt.com, on your applet page, click “Check now”. See that IFTTT confirms that the applet             was checked.         c) Wait for your stock to increase and see Cozmo react! Cozmo should roll off the charger, raise             and lower his lift, announce the stock increase, and then show a stock market image on his face.

代码语言:javascript
复制
import asyncio
import sys


try:
    from aiohttp import web
except ImportError:
    sys.exit("Cannot import from aiohttp. Do `pip3 install --user aiohttp` to install")

import cozmo

from common import IFTTTRobot


app = web.Application()


async def serve_stocks(request):
    '''Define an HTTP POST handler for receiving requests from If This Then That.

    Controls how Cozmo responds to stock notification. You may modify this method
    to change how Cozmo reacts to the stock price increasing.
    '''

    json_object = await request.json()

    # Extract the company name for the stock ticker symbol.
    stock_name = json_object["StockName"]

    # Extract the percentage increase.
    percentage = str(json_object["PercentageChange"])

    robot = request.app['robot']
    async def read_name():
        try:
            async with robot.perform_off_charger():
                '''If necessary, Move Cozmo's Head and Lift to make it easy to see Cozmo's face.'''
                await robot.get_in_position()

                # First, have Cozmo play an animation
                await robot.play_anim_trigger(cozmo.anim.Triggers.ReactToPokeStartled).wait_for_completed()

                # Next, have Cozmo say that your stock is up by x percent.
                await robot.say_text(stock_name + " is up " + percentage + " percent").wait_for_completed()

                # Last, have Cozmo display a stock market image on his face.
                robot.display_image_file_on_face("../face_images/ifttt_stocks.png")

        except cozmo.RobotBusy:
            cozmo.logger.warning("Robot was busy so didn't read stock update: '"+ stock_name + " is up " + percentage + " percent'.")

    # Perform Cozmo's task in the background so the HTTP server responds immediately.
    asyncio.ensure_future(read_name())

    return web.Response(text="OK")

# Attach the function as an HTTP handler.
app.router.add_post('/iftttStocks', serve_stocks)


if __name__ == '__main__':
    cozmo.setup_basic_logging()
    cozmo.robot.Robot.drive_off_charger_on_connect = False

    # Use our custom robot class with extra helper methods
    cozmo.conn.CozmoConnection.robot_factory = IFTTTRobot

    try:
        app_loop = asyncio.get_event_loop()  
        sdk_conn = cozmo.connect_on_loop(app_loop)

        # Wait for the robot to become available and add it to the app object.
        app['robot'] = app_loop.run_until_complete(sdk_conn.wait_for_robot())
    except cozmo.ConnectionError as e:
        sys.exit("A connection error occurred: %s" % e)

    web.run_app(app)

Fin


本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云开发 CloudBase
云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档