LoginSignup
5
1

More than 3 years have passed since last update.

PythonでOjiChannel

Last updated at Posted at 2019-12-05

advent calendar6日目にして途絶えそうだったので3週目

これは

ojichatのslackbotが生息するOjiChannelをつくったのでそのログ
先駆者さん(参考文献4.)がいらっしゃるが、golangあまりわからんマンなのでpython版として無理やりやる

前提条件

  • 環境
    • Python 3.7.4
    • Docker version 19.03.2, build 6a30dfc
  • 要求
    • SlackBotの導入
      • 多くのわかりやすい記事があるので割愛
      • APIトークンの取得まで
  • モチベーション
    • slackで誰も返事してくれないときは、おじさんの絡みでもありがたいかもしれない

参考文献

  1. lins05/slackbot
  2. greymd/ojichat
  3. PythonのslackbotライブラリでSlackボットを作る
  4. 最近流行りのojichatをslackbotにした

できるもの

oji_demo.png

本編

インストール

lins05/slackbot

$ pip3 install slackbot

greymd/ojichat

おじさんで環境を汚したくない、Goの実行環境を持っていないなどの状況でもお手軽におじさんになるために、Dockerコンテナでもojichatを用意してある ( greymd/ojichat )。
https://github.com/greymd/ojichat

汚したくないので

$ docker pull greymd/ojichat

slackbot実行のための用意

ojichannel
├── plugins
│   ├── __init__.py
│   └── ojibot.py
├── run.py
└── slackbot_settings.py
run.py
from slackbot.bot import Bot

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

if __name__ == "__main__":
    print('start slackbot')
    main()
slackbot_settings.py
# botアカウントのトークン
API_TOKEN = "hoge"

# デフォルトの返答
DEFAULT_REPLY = "fuga"

# プラグインスクリプトを置いてあるサブディレクトリ名のリスト
PLUGINS = ['plugins']
ojibot.py
import subprocess
from slackbot.bot import listen_to

@listen_to('(.*)')
def greet_by_oji(message, something):
    user_info = message.user
    f_name = user_info["profile"]["first_name"]
    sp = subprocess.run( ["docker", "run", "--rm", "-i", "greymd/ojichat:latest", f_name], encoding='utf-8', stdout=subprocess.PIPE)
    # おじさんはチャンネルにポストする
    message.send(sp.stdout)
    # おじさんはスレッドにリプライする
    message.reply(sp.stdout, in_thread=True)

実行

$ python3 run.py

すべての発言に対してdocker runが動くのはよくなさそうだけど遊びアプリということで

おわり

Slack自治厨ちゃん、オハヨー❗😃✋😄オジサンは今日から栃木ヘ〜😃☀ (^_^)💗😚

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1