Making motions and conversations in Pepper can be very interesting, but it still makes me want to connect to a social network.
If you write Python in Python Box, you can do various things, but even if you look at the official document, there is no way to import an external library. ・ ・ ・ If you think HOW TO IMPORT PYTHON FILES IN YOUR PEPPER APPS? There was, so I will try it.
First of all, let's get an idea of what you need. To read Access Twitter API with Python, it seems that the following elements are required to access Twitter.
Also, I wonder if the following is required as a library imported from the script.
Based on these, we will consider realizing the following Twitter Tweet box.
As for the specifications of the Tweet box,
Feeling like that. As a procedure,
I will try in the order of.
First, create a box shape on Choregraphe.
Right-click on the flow diagram and select ** [Python ...] under [New Box] **
Enter ** Tweet in the name **
Change the onStart input type to a string. Enter ** Click the Settings button on Start [A] ** and then click the ** Type drop-down list [B] **
Then, a list of types will appear. ** Select [String] ** and click the [OK] button.
Since the setting dialog appears in the same way as input, enter ** onFailed in Name [A] ** and change Type to ** [number] [B] **
When you press the [OK] button, you can see that the Consumer Key has been added to the variable list.
Add Consumer Secret, Access Token, Access Token Secret in the same way as 5.
Click the [OK] button to create a ** Tweet box ** in the flow diagram.
Now the outside of the box is ready.
In the Tweet box, we will import the following libraries for OAuth authentication.
Looking at the requirements.txt of this library, I found that the following libraries are also needed, so these too I will include it.
Originally, I thought that I should use a package management tool properly, but first of all, I feel like using the above three libraries, and I will try to incorporate the library files into the project by the following procedure.
This time, select ** [Download ZIP] ** and zip the files in the repository.
Create a lib
folder in an appropriate folder, unzip the three downloaded ZIP files, and put them together in the following configuration.
lib
requests_oauthlib
__init__.py
oauthlib
__init__.py
requests
__init__.py
Click the ** Add [+] button in the [Project Contents] panel and select [Import Folder ...] **
The [Select Folder to Import] dialog opens. Select the lib
folder created in 2. and click the ** [Select Folder] button **.
The lib directory will be added to the file list, so expand the directory tree and make sure the files are imported.
You should now be able to import the Python library as a project file (should). Next, as a Python script for the Tweet box, write code that uses these libraries to tweet.
Double-click the Tweet box to open the script editor and paste the script below.
class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
def onUnload(self):
import sys
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
def onInput_onStart(self, p):
import sys, os
self.folderName = os.path.join(
self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
for moduleName in os.listdir(self.folderName):
#Module reload
if moduleName in sys.modules:
self.logger.info("Loaded: %s, %s" % (moduleName, sys.modules[moduleName].__file__))
reload(sys.modules[moduleName])
from requests_oauthlib import OAuth1Session
self.logger.info("Tweeting... %s" % p)
url = "https://api.twitter.com/1.1/statuses/update.json"
#Tweet body
params = {"status": p}
twitter = OAuth1Session(self.getParameter("Consumer Key"),
self.getParameter("Consumer Secret"),
self.getParameter("Access Token"),
self.getParameter("Access Token Secret"))
req = twitter.post(url, params = params)
if req.status_code == 200:
self.logger.info("OK")
self.onStopped()
else:
self.logger.warn("Failed: %d" % req.status_code)
self.onFailed(req.status_code)
def onInput_onStop(self):
self.onUnload() #it is recommended to reuse the clean-up as the box is stopped
self.onStopped() #activate the output of the box
The point of the code is ...
First, get a reference to ALFrameManager (used for path resolution of files in the project) at onLoad and initialize variables.
def onLoad(self):
self.framemanager = ALProxy("ALFrameManager")
self.folderName = None
The Python library added to [Project Contents] by sys.path
can be imported at the start of onStart processing.
def onInput_onStart(self, p):
import sys, os
self.folderName = os.path.join(
self.framemanager.getBehaviorPath(self.behaviorId), "../lib")
if self.folderName not in sys.path:
sys.path.append(self.folderName)
Load the Python library in the project and process it
from requests_oauthlib import OAuth1Session
self.logger.info("Tweeting... %s" % p)
...
After the process is completed, call the output by the return value of the Twitter API call.
...
if req.status_code == 200:
self.logger.info("OK")
self.onStopped()
else:
self.logger.warn("Failed: %d" % req.status_code)
self.onFailed(req.status_code)
When unloading, remove the path for the project's Python library from sys.path
def onUnload(self):
import sys
if self.folderName and self.folderName in sys.path:
sys.path.remove(self.folderName)
self.folderName = None
Feeling like. Personally, rewriting the sys.path
feels a bit brute force ...
I hope that pip will be well integrated in the future.
To try out the created Tweet box, create the following application.
Place Data Edit> Text Edit in the standard box library
Connect the boxes as follows
Set ** Consumer Key, Consumer Secret, Access Token, Access Token Secret ** obtained from Twitter in the parameter of Tweet box.
Enter the content you want to tweet in the Text Edit box
Now you have created a flow to tweet any message. It worked on a real Pepper machine or a virtual robot (Mac only). Cannot be executed by Windows virtual robot. It seems that the ssl module is not valid ...
You can make Pepper tweet like this. I was thinking that if I could take a picture of the camera and tweet it, Pepper would be able to handle the live hackathon work. I will continue to write if I have any material.
Recommended Posts