[PYTHON] Use slackbot as a relay and return from bottle to slack in json format.

Overview

If you mention the bot created with slack and talk with the specified wording Linked to the wording spoken by the lightweight web framework bottle Returns information. The returned information will be posted to slack. Also, you can get the same information by accessing bottle with http, so You don't have to go through slack.

Result image

Image 1 Returns JSON according to the alphabet.

sample.png

When you talk to magi_casp with mention, It returns the information in JSON. In this example I love[A|B|C|D|E|F|G|H]On the condition If you enter the letters A to H after I love, It returns JSON according to the alphabet.

Image 2 Returns the weather

When you mention tenki to magi_casp, It will return the weather for the day and the next day.

About whether this is a good way

I do not understand. While studying bottle and slackbot, remembering docker a little I came up with the idea that it could be used in this way.

Premise

--Since docker is used, you need a server to host docker. The host OS is centos7. --There are 2 containers. --Container that runs slackbot --The container that runs the bottle ――I make it in Sakura's cloud. It is a plan of 7 yen per hour with 1 core 1 GB in the Ishikari region. --Assuming that the server on which docker runs is ready. --You have already added the Bot Integration to slack.

Preparation

clone.

Execute the following command to clone the git repository created by the author.

git clonehttps://github.com/tomokitamaki/slackbotWithbottle.git


 
## Create an image using Dockerfile
 1. Create a file that describes the Bot API token.
 In the folder called slackbot_bottle in the clone
 Create a file called APITOKEN.py in a folder called slackbot.

#### **`vi APITOKEN.py`**
```py

 Describe the Bot API token in the following format.
```apiapi = "api token"
Example) API API= "apitoookennnn"
  1. Create an image of a container in which slackbot runs In the folder called slackbot_bottle in the clone, in the folder called slackbot, build_slackbot_dockerfileBecause there is a docker fie called Move to the directory where the file is located and execute it with the following build command.

centos7 build_slackbot_Dockerfile .


 This will create an image. You can confirm that the image is created by executing the following command.
```sudo docker images```
  
 2. Create an image of the container that moves the bottle
 In the folder called bottle in the folder called slackbot_bottle in the clone,
```build_bottle_dockerfile```Because there is a docker fie called
 Move to the directory where the file is located and execute it with the following build command.

#### **`centos7 build_bottle_Dockerfile .  `**
```sudo docker build -t slackbot

 This will create an image. You can confirm that the image is created by executing the following command.
```sudo docker images```

## Launch the container from the image

 1. Start the container from the slackbot image.
 Start with the following command.

#### **`centos7`**
```sudo docker run -d --name slackbot slackbot

 2. Start the container from the image of bottle.
 Start with the following command.
`sudo docker run -d -p 3333:80 --name bottle bottle:centos7`  
 Since the bottle is listening on port80, the communication addressed to prt3333 of the host OS is connected to port80 of the container.

# Mention the bot.
 Mention the bot and talk to tenki,
```How is the weather today("Cloudy"),Tomorrow's weather("Partially cloudy")```
 If it comes back, it's OK.
# Take a look in your browser

 `http: // server IP: 3333 / tenki / tenki`
 Access with
```How is the weather today("Cloudy"),Tomorrow's weather("Partially cloudy")```
 It is perfect when it comes back.

# end
 If you do so far, it will return the weather and JSON that links the word and the alphabet,
 You will be able to use the BOT and the bottle that works with it.
 Please also see the README below.
https://github.com/tomokitamaki/slackbotWithbottle
# development
 If you add to the file in the plugins folder, you can increase the variation of the reply,
 You can do something a little more complicated by writing python code in hello_world.py.

 TIPS
 -[Stumbling] When communication from the slackbot container to the bottle container is not possible.
 Described in the file that defines the bot mention in the plugins folder,
 It didn't work if the URL was the host OS's global IP: port number (ex: http: // server IP: 3333), so
 It worked fine if I made the IP the container's local IP instead of the host's global IP.
 --How to install bottle
 Please proceed from the 2. Bottle installation section of http://tech.aainc.co.jp/archives/9826 to the end of the page.
 --How to install slackbot
 From the section on building a Slack Bot environment at http://blog.bitmeister.jp/?p=3892 to [Making a Slack bot (2)](http://blog.bitmeister.jp/?p=3911) in Python Proceed, please.
 It's very easy to understand.


Recommended Posts

Use slackbot as a relay and return from bottle to slack in json format.
Connect to postgreSQL from Python and use stored procedures in a loop.
Convert / return class object to JSON format in Python
[Python] Use JSON format data as a dictionary type object
Post a message from IBM Cloud Functions to Slack in Python
How to return the data contained in django model in json format and map it on leaflet
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
Try to extract specific data from JSON format data in object storage Cloudian/S3
Send a message from Python to Slack
How to use is and == in Python
Flutter in Docker-How to build and use a Flutter development environment inside a Docker container
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
Use dHash to locate on the course from a scene in a racing game
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
Send a message from Slack to a Python server
Image characters and post to slack (python slackbot)
How to create a JSON file in Python
How to generate a Python object from JSON
Use pygogo to get the log in json.
A story of trial and error trying to create a dynamic user group in Slack
Use API to mark a large number of unread emails in Gmail as read
How to use Docker to containerize your application and how to use Docker Compose to run your application in a development environment
How to use python multiprocessing (continued 3) apply_async in class with Pool as a member
Parse a JSON string written to a file in Python
A memorandum on how to use keras.preprocessing.image in Keras
How to display DataFrame as a table in Markdown
Convenient to use matplotlib subplots in a for statement
How to use Decorator in Django and how to make it
Python script to create a JSON file from a CSV file
Output a binary dump in binary and revert to a binary file
Get a participant's username and screen name in Slack
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
A note that deployed a Python application from Circle CI to Elastic Beanstalk and notified Slack
2015-12-26 python2> datetime> Implementation to take the difference in seconds from two ISO format datetime strings> Use .seconds ()
Use Heroku in python to notify Slack when a specific word is muttered on Twitter
A story about trying to use cron on a Raspberry Pi and getting stuck in space
It may be a problem to use Japanese for folder names and notebook names in Databricks
Started Python: Swap an array of values obtained from SQL results to a list type and use it in IN of another query