技術関連の覚書

案件でやったり自宅で試したことの覚書

Amazon LinuxでSlackbotを作ってみる

さっき作ったAWSインスタンスにSlackbotのプログラムを仕込んでみます

python環境の準備

[ec2-user@ip-10-0-10-50 ~]$ python --version
Python 2.7.12

python3系とpipをインストールする

[root@ip-10-0-10-50 ~]# yum search python3
Loaded plugins: priorities, update-motd, upgrade-helper
============================= N/S matched: python3 =============================
mod24_wsgi-python34.x86_64 : A WSGI interface for Python web applications in
                           : Apache
mod24_wsgi-python35.x86_64 : A WSGI interface for Python web applications in
                           : Apache
postgresql92-plpython27.x86_64 : The Python3 procedural language for PostgreSQL
python34.x86_64 : Version 3.4 of the Python programming language aka Python 3000
python34-devel.x86_64 : Libraries and header files needed for Python 3.4
                      : development
python34-docs.noarch : Documentation for the Python programming language
python34-libs.i686 : Python 3.4 runtime libraries
python34-libs.x86_64 : Python 3.4 runtime libraries
python34-pip.noarch : A tool for installing and managing Python packages
python34-setuptools.noarch : Easily build and distribute Python packages
python34-test.x86_64 : The test modules from the main python 3.4 package
python34-tools.x86_64 : A collection of tools included with Python 3.4
python34-virtualenv.noarch : Tool to create isolated Python environments
python35.x86_64 : Version 3.5 of the Python programming language aka Python 3000
python35-devel.x86_64 : Libraries and header files needed for Python 3.5
                      : development
python35-libs.i686 : Python 3.5 runtime libraries
python35-libs.x86_64 : Python 3.5 runtime libraries
python35-pip.noarch : A tool for installing and managing Python packages
python35-setuptools.noarch : Easily build and distribute Python packages
python35-test.x86_64 : The test modules from the main python 3.5 package
python35-tools.x86_64 : A collection of tools included with Python 3.5
python35-virtualenv.noarch : Tool to create isolated Python environments

  Name and summary matches only, use "search all" for everything.

python3.5をインストールする

sudo yum -y install python35
sudo yum -y install python3-pip

pip の後に[Tab]キーを押すと候補が

pip
pip      pip-2.7  pip-3.5 

のように出てくるので pip-3.5 を使ってslackbotのインストール

pip-3.5 install slackbot

pythonでプログラムを書く

PythonのslackbotライブラリでSlackボットを作る - Qiita

ここを参考にプログラムの構成を作る

API_TOKENの値を設定

ボットのsettingを押下して

f:id:boctok-ctpoba:20170717223722p:plain:w300

f:id:boctok-ctpoba:20170717224032p:plain:w300

このままだとSlackとの間の通信ができないのでHTTPSでSlackとアクセスできるようにする

EC2のダッシュボードで「セキュリティグループ」を選び タブの「インバウンド」を選択 編集を押下して 「ルールの追加」を押下

項目 設定値
タイプ HTTPS
ソース カスタムを選んでSlackのIPアドレスを登録(xxx.xxx.xxx.xxx/32)

上記を設定して保存