[PYTHON] Create a SlackBot service on Pepper

I wanted to run SlackBot inside Pepper.

Until now, Slack and Pepper have been linked using Slack's Incoming Web Hook. I was posting using the request module from the work app, I wanted to be able to receive notifications from the Slack side, so I thought about installing SlackBot in Pepper.

I want to use it across all work apps.

What I wanted to do with SlackBot.

--Create a channel that collects Pepper bots. --Send various events to Pepper with a message addressed to the bot. --Send various events to all Pepper with a message addressed to @Channel. --Collect various sensor information of Pepper in Slack.

Also, I wanted to use it across the board regardless of the work application, so I implemented it as a service by referring to this article. http://qiita.com/yacchin1205/items/11301d79380d08d2dbf6

Development environment

Mac OS Sierra Python 2.7.10

Enable bots in Slack

キャプチャ.PNG

Environment

Install QiBuild

pip install qibuild

Install Python SDK

Please drop the following version instead of the latest version. The latest version (2.5.5 at the time of writing) seems to have many changes, and there was little information to be helpful, so I downloaded 2.4.3.

pynaoqi-python2.7-2.4.3.28-mac64

Leave it through path. Also, add it to .bash_profile.

$ export PYTHONPATH=${PYTHONPATH}:/path/to/pynaoqi-python2.7-2.4.3.28-mac64
$ export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/path/to/pynaoqi-python2.7-2.4.3.28-mac64

The following commands can be used if the path is correct.

$ python

>> import qi
>> import naoqi

Work tree preparation

The final required environment is as follows.

SlackBotService
 qiproject.xml
 manifest.xml
 slackbotservice.py
 slackbotservice.pml
 bot_settings.py
 lib
  backports
  slackbot
  slacker
  websocket
  six.py
  plugins
   __init__.py
   mention.py
   proxy.py

Execute the following command in the same hierarchy as the project directory. Various builds and packaging will be possible in the lower hierarchy.

$ pwd
/path/to/SlackBotService
$ qibuild init
$ qipy bootstrap

Install slackbot module

$ pip install slackbot

The required modules will be installed together. Copy the following installed modules under SlackBotService / lib.

$ pwd
/path/to/SlackBotService/lib
$ ls
backports/
slackbot/
slacker/
websocket/
six.py

Copy websocket / cacert.pem under SlackBotService / lib.

$ pwd
/path/to/SlackBotService/lib
$ ls
backports/
slackbot/
slacker/
websocket/
six.py
cacert.pem

Implement SlackBot

Describe the settings

Describe the settings of SlackBot.

SlackBotService/bot_settings.py
# -*- coding: utf-8 -*-

import os
from slackbot import settings

settings.API_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
settings.DEFAULT_REPLY = u"It is not well understood."
settings.PLUGINS = [
    'plugins',
]

os.environ['WEBSOCKET_CLIENT_CA_BUNDLE'] = os.path.join('./bin', 'cacert.pem')

The following part loads the above key file which is not bundled at the time of packaging. Of course, I get a warning, but I didn't want to pollute the environment of Pepper itself, so I did this once.

os.environ['WEBSOCKET_CLIENT_CA_BUNDLE'] = os.path.join('./bin', 'cacert.pem')

Implement the main script that is started as a service

Run SlackBot when running the script.

SlackBotService/slackbotservice.py
# -*- coding: utf-8 -*-

from slackbot.bot import Bot
from bot_settings import *


class SlackBotService:
    def __init__(self):
        self.bot = Bot()
        self.bot.run()


def main():
    slackbotservice = SlackBotService()


if __name__ == "__main__":
    main()

Implemented SlackBot plugin

If something is spoken, try to make Pepper speak.

SlackBotService/lib/plugins/mention.py
# -*- coding: utf-8 -*-

from slackbot.bot import respond_to, listen_to
from slacker import Slacker
from plugins import proxy
from bot_settings import settings

slack = Slacker(settings.API_TOKEN)


@respond_to(u'')
def respond_any_word(message):
    """
Receive any character.
    :param message:Received message object
    """
    _text = message.body.get('text', '')
    _ts = message.body.get('ts', '')
    _user = message.body.get('user', '')
    _team = message.body.get('team', '')
    _type = message.body.get('type', '')
    _channel = message.body.get('channel', '')
    proxy.animated_speech(_text)
    message.reply("done")

Implemented a proxy to make Pepper speak

SlackBotService/lib/plugins/proxy.py
# -*- coding: utf-8 -*-

from naoqi import ALProxy

animatedSpeechProxy = ALProxy("ALAnimatedSpeech", "127.0.0.1", 9559)


def animated_speech(text):
    _text = text if isinstance(text, str) else text.encode("utf-8")
    configuration = {"bodyLanguageMode": "contextual"}
    animatedSpeechProxy.say(_text, configuration)

Package your app

qiproject.xml Define the module to bundle. All necessary modules such as libraries and scripts are defined.

<project version="3">
    <qipython name="slackbotservice">
        <package name="backports" src="lib" />
        <package name="slackbot" src="lib" />
        <package name="slacker" src="lib" />
        <package name="websocket" src="lib" />
        <package name="plugins" src="lib" />
        <script src="lib/six.py" />
        <script src="lib/cacert.pem" />
        <script src="slackbotservice.py" />
        <script src="bot_settings.py" />
    </qipython>
</project>

manifest.xml Define the service. The \ tag defines information as a service recognized within Pepper.

<package uuid="slackbotservice" version="0.1.0">
    <services>
        <service name="SlackBotService" autorun="true"
            execStart="/bin/bash ./python bin/slackbotservice.py" />
        <executableFiles>
            <file path="python" />
        </executableFiles>
    </services>
    <requirements>
        <naoqiRequirement minVersion="2.3"/>
        <robotRequirement model="JULIETTE"/>
    </requirements>
</package>

.pml file

Define the package.

<?xml version="1.0" encoding="UTF-8" ?>
<Package name="slackbotservice" format_version="4">
    <Manifest src="manifest.xml" />
    <qipython name="slackbotservice" />
</Package>

Project packaging

$ qipy install slackbotservice
$ qipkg make-package slackbotservice.pml

Deploy package

$ qipkg deploy-package slackbotservice-0.1.0.pkg --url nao@<Pepper IP>

Check the operation

キャプチャ.PNG

You can now have Pepper speak via Slack!

from now on

It was very convenient because it became possible to call various AL modules via Slack. At the moment, it is not registered as a service, so you cannot operate SlackBot from various work apps. By registering as a service, you will be able to start and stop SlackBot itself, exchange messages with work apps, and so on.

I think that if you use SlackBot well, you can use it for cooperation between work apps and communication between Pepper.

Recommended Posts

Create a SlackBot service on Pepper
Create a classroom on Jupyterhub
Create a Python environment on Mac (2017/4)
Create a Linux environment on Windows 10
Create a python environment on centos
Create a python environment on your Mac
Make Jupyter Notebook a service on CentOS
Make Unity Accelerator a service on Linux
Create a Linux virtual machine on Windows
Create a web service with Docker + Flask
Create a web service in Flask-SQLAlchemy + PostgreSQL
[Venv] Create a python virtual environment on Ubuntu
Try to create a new command on linux
Create a Python execution environment on IBM i
Create a Python virtual development environment on Windows
Create a GUI on the terminal using curses
Create a Django schedule
Create a Python module
Looking back on creating a web service with Django 1
Create a QR code for the URL on Linux
Create a comfortable Python 3 (Anaconda) development environment on windows
Looking back on creating a web service with Django 2
Create a Bootable LV
Create a Python environment
[kotlin] Create a real-time image recognition app on android
Create a decent shell and python environment on Windows
Create a Python development environment on OS X Lion
Create a shape on the trajectory of an object
Create a slack bot
Create a PythonBox that outputs with Random after PEPPER Input
Create a new csv with pandas based on the local csv
Create a Python (pyenv / virtualenv) development environment on Mac (Homebrew)
Create a list in Python with all followers on twitter
[Python] Create a linebot that draws any date on a photo
Set up a Google Cloud service account key on heroku
Create a Python script for Wake on LAN (NAT traversal Wake on LAN [5])
Create a virtual environment for python on mac [Very easy]
Create a Wox plugin (Python)
Create a local pypi repository
Create a function in Python
Create a dictionary in Python
A comment on Boruta algorithm
Create a (simple) REST server
Create a homepage with django
Create command shortcuts on Ubuntu 16.04
Pepper Tutorial (5): Using a Tablet
Create a python numpy array
Create a dummy data file
Make Responder a daemon (service)
Create a Django login screen
Create a heatmap with pyqtgraph
Create a simple textlint server
Create a directory with python
Create a rudimentary ELF packer
Create a Python environment for professionals in VS Code on Windows
I tried to create a server environment that runs on Windows 10
[MariaDB] Install MariaDB on Linux and create a DB and an operating user.
[AWS Hands-on] Let's create a celebrity identification service with a serverless architecture!
Create a word cloud with only positive / negative words on Twitter
Create a Docker container image with JRE8 / JDK8 on Amazon Linux
I want to create a machine learning service without programming! WebAPI