This is a continuation of Previous article.
Raspberry Pi 3B + (including SD card) RGB LED matrix (6mm pitch 32 x 32) 6 sheets Connector for LED matrix MAXWELL switching power supply 3 power cords for LED panel 6 IDC flat cables 50 jump wires, male and female, and 50 male and female bread board
I want to switch the display of the electric bulletin board from Slack on my smartphone and create an environment where I can easily operate it without having to operate the screen of Raspberry Pi.
Introduce Slackbot to operate Raspberry Pi from Slack. With Slackbot, you can make it work according to the message sent to Slack.
First, install the library to handle Slackbot with python.
$ sudo pip install slackbot
First, create a new workspace in Slack. After that, please create a bot from this site. When you press "Add bot integration", an API token will be issued. Use this API token.
Once the bot account is created, it will be added to the app as shown below. Invite this app to your channel.
Set the Slackbot directory structure as follows. Make only slackbot_settings.py with this name. Any name is fine for the other two.
slackbot
├ bot.py #Run slackbot
├ slackbot_settings.py #Slackbot settings
└ botmodule.py #Description of operation content
slackbot_settings.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 「API_Please describe the API token obtained from Slack in "TOKEN"
API_TOKEN = "Obtained API token"
#Default response when mentioning unknown words
DEFAULT_REPLY = "There is no such command."
#Read an external file. botmodule.Load py
PLUGINS = [
'slackbot.plugins',
'botmodule',
]
In slackbot_settings.py, write the API token you got. Also, describe the file that describes the operation as a plug-in. In DEFAULT_REPLY, you can set a response when an unregistered word arrives by mentioning the bot.
bot.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from slackbot.bot import Bot
from slacker import Slacker
import slackbot_settings
#Launch the bot
def main():
#from here
text="Send a command to this channel to operate the scoreboard.\n"\
"You can operate it by sending the following command.\n"\
"<Command name>\n → command"
sla=Slacker("Obtained API token")
sla.chat.post_message('The name of the channel you want to post', text, as_user=True)
#Up to this point, describe only if you want to post at startup
bot = Bot()
bot.run()
if __name__ == "__main__":
main()
The bot works by executing bot.py. If you want to send a message when the bot starts, use Slacker. Slacker also works with the acquired API token. Please specify the channel you want to post.
botmodule.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from slackbot.bot import respond_to
from slackbot.bot import listen_to
import io,sys
import subprocess
import text_image_ledmatrix
#sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import requests
import slackbot_settings
import time
def processmatrix():
f=open('clear.txt')
line=f.readline()
while line:
ans=line
line=f.readline()
f.close()
ans=ans.strip()
if ans=='on':
return True
else:
return False
@listen_to('-print(.*)')
def print(message, something):
if processmatrix():
text_image_ledmatrix.add_text('clear')
time.sleep(3)
message.react('ok')
message.reply('[{0}]It is displayed'.format(something))
text_image_ledmatrix.ledprint(something)
@listen_to('-scroll(.*)')
def print(message,something):
if processmatrix():
text_image_ledmatrix.add_text('clear')
time.sleep(3)
message.react('ok')
message.reply('[{0}]Scroll'.format(something))
text_image_ledmatrix.textscroll(something)
text_image_ledmatrix.py can be found in Previous article. Listen_to receives words other than mentions posted in the channel. It works when it receives the words in (). I'm using a regular expression later in (), which is the second argument defined in the next line. If you don't need any arguments, you don't need a regular expression. Stamp with message.react (). You can also mention the post with message.reply (). If you post at once, a bug will occur, so please do not post the next post until there is a reaction.
I actually moved it.
python3 bot.py
Let's talk to LEDmatrix in Slack.
It went well! Now you can easily switch the display of LED matrix.
This time, I referred to the following site. https://qiita.com/undo0530/items/2139a1e8b73b3eee6e00/ http://www.denzow.me/entry/2017/12/16/225241/ https://qiita.com/minase_tetsuya/items/dba79cfe12db4557cefc https://qiita.com/o_s_t/items/f05057aefb297393a69a
This time, we succeeded in combining LED matrix with Slack. From the next time, we will summarize the diversification of display functions and program startup when the power is turned on.
Recommended Posts