仮想サーファーの波乗り

仮想サーファーの日常

プログラミング・エンジニアのスキルアップ・ブログ運営などに関してよく書く雑記ブログ

➡ Udemyで8/27(木)まで割引セール開催中! 1,200円〜で普段の90%以上OFF!

PythonでSlackBot開発④「仮想通貨ダッシュボード画像をSlack送信」


PythonでSlackBotを開発しよう企画の第4回目。今回は、仮想通貨情報を教えてくれる機能を実装していきます。「特定の仮想通貨の価格チャート推移画面スクリーンショットをSlack投稿してくれる」機能を実装していきます。


CoinGeckoダッシュボード画像Slack送信

*Botアプリケーションのファイルは、PythonでSlackBot開発③で実装した状態であることを前提にしています。

CoinGeckoのダッシュボードは登録した場合のみ利用できるっぽいので、以下のコードを動かしてみたい方は事前に登録しておきましょう。


今回はseleniumとphantomjsを利用したいので、以下のコマンドでライブラリをインストールして、requirements.txtにライブラリのバージョンを記載しておきます。

$ pip install selenium 
$ brew install phantomjs
$ pip freeze > requirements.txt


利用するライブラリのインストールができたら、run.pyファイルを編集しています。

run.py

# coding=utf-8
from slackbot.bot import Bot
from slackbot.bot import respond_to
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import requests

url_slackapi = 'https://slack.com/api/files.upload'

@respond_to('coinScreenD')
def coingecko_screenshot_dashboard(message):
    # Coingeckoにログインするためのメアド、パスワードを取得して、ログインします。
    COING_EMAIL_ADDRESS = os.environ['COING_EMAIL_ADDRESS']
    COING_PASSWORD = os.environ['COING_PASSWORD']
    # driver = webdriver.PhantomJS(executable_path='./vendor/')
    driver = webdriver.PhantomJS()
    driver.get('https://www.coingecko.com/account/sign_in')
    input_element_email = driver.find_element_by_id('user_email')
    input_element_password = driver.find_element_by_id('user_password')
    send_button = driver.find_element_by_name('commit')
    input_element_email.send_keys(COING_EMAIL_ADDRESS)
    input_element_password.send_keys(COING_PASSWORD)
    send_button.click()

    # ページが完全に読み込まれるまでの時間を加味して最大5秒間待ち、スクリーンショットを保存して、画像をpost。
    message.send("ダッシュボード読み込み中...ちょっと待ってくだせえ...")
    driver.set_page_load_timeout(5)
    driver.save_screenshot('screenShot.png')
    post_file('./screenShot.png')

def post_file(file_path):
    files = {'file': open(file_path, 'rb')}
    slackapi_params = {
        'token': os.environ['SLACKBOT_API_TOKEN'],
        'channels': 'general'
    }
    requests.post(url_slackapi, data=slackapi_params, files=files)

def main():
    bot = Bot()
    bot.run()

if __name__ == '__main__':
    main()

↑ Coingeckoにログインして、ダッシュボード画面のスクリーンショットを撮影してSlackに画像を送信しています。


COING_EMAIL_ADDRESSとCOING_PASSWORDの値をHerokuに登録していない状態だと変数が読み取れずエラーになってしまうので、Herokuの設定画面から登録していきます。

f:id:virtual-surfer:20180401231459p:plain


また、PhantomJSをHerokuのアプリケーション内に用意する必要があるので、PhantomJSとPythonの両方のbuildpackを使えるbuildpack-multiを使えるように以下のコマンドで設定をしておきます。

$ heroku config:add BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-multi.git
$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-multi.git
$ heroku config:add LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/lib:/app/vendor/phantomjs/lib

f:id:virtual-surfer:20180401231957p:plain

buildpackの設定が追加されていることが確認できました。


buildpackの設定ができたら、.buildpacksファイルを作成し、以下のように記述します。

.buildpacks

https://github.com/heroku/heroku-buildpack-python.git
https://github.com/stomita/heroku-buildpack-phantomjs.git


以上で実装は完了です。以下のコマンドでherokuにlocalの変更内容を反映させ、herokuアプリケーションを再起動します。

$ git add {変更したファイルパス名}
$ git commit -m "{コミット名}"
$ git push heroku master
$ heroku restart


これでアプリケーションにも反映されたはずです。Slackで実際に話しかけてみます。

f:id:virtual-surfer:20180401233141p:plain

読み込みが遅いのか、通貨の種類が全然表示されていませんが、なんとかスクショを送信することができています。

f:id:virtual-surfer:20180401233318p:plain

4つしか表示されないのはさすがに悲しすぎるので、いずれ直します...。今回はここまで!


では!