LoginSignup
2
1

More than 3 years have passed since last update.

LineとSlackが合体した話

Posted at

この記事はCyberAgent 20新卒 Advent Calendar 2019 の25日目になります!

メリークリスマス!

概要

僕が研究の際に作ったシステムを紹介したいと思います。

簡単に言うと、LineBotとslackBotを結合したアプリを作りました。

lineとslackで同時に物事を共有できるアプリです。

デモ

↑ デモ動画です〜

Line側

Line Developerサイトにて、Line Bot作成。
ここのリクエスト先を自作アプリのAPIに向ける

https://developers.line.biz/console/channel/自分の作成したLineBotのID/messaging-api

Webhook settings

Webhook URL に設定

Slack側

自作アプリ

自作アプリの簡単な紹介をします

環境

  • python3.7
  • Heroku

必要なpythonライブラリ

  • slackweb
  • flask
  • linebot

code

1. Lineのcallbackを処理して、Lineにレスポンス

  • 処理
@app.route("/callbackline", methods=['POST'])
def callback():
    signature = request.headers['X-Line-Signature']

    body = request.get_data(as_text=True)

    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        abort(400)

    return 'OK'
  • レスポンス

ここは、Lineからきた処理をSlackに送っている

@handler.add(MessageEvent, message=TextMessage)
def response_message(event):
    try:
        sourceGroup: SourceGroup = event.source
        userId = sourceGroup.user_id
        text: str = event.message.text
        # 細かいところは長くなるので省略しました
        attachments = [{
            "color": "#0000FF",
            "title": "LINEから通知されました",
            "text": '<!here>\n' + text
        }]
        profile = line_bot_api.get_profile(userId)
        slack.notify(attachments=attachments,
                         username=profile.display_name, icon_url=profile.picture_url)
    except Exception as e:
        print(e)
        raise

2. slackのcallbackを処理

ここは、slashコマンドで送られてきた処理をLineに送っている

@app.route("/callbackslack", methods=['POST'])
def callbackslack():
    '''
    * SLACK_VERIFICATION_TOKEN
    * LINE_GROUP_ID
    この辺は自分で設定
    '''
    if not request.form['token'] == SLACK_VERIFICATION_TOKEN:
        return
    try:
        user_name = request.form['user_name']
        slackMessage = request.form['text']
        toLineText = '【' + user_name + '】 ' + slackMessage
        line_bot_api.push_message(LINE_GROUP_ID, TextMessage(text=toLineText))
        # 細かい処理は長くなるので省略しました
    except Exception as e:
        print(e)
        raise

おわり

本当はクリスマスの投稿記事なので、クリスマスっぽい事したかったですが思いつかなかったので(笑)

このアプリで、エンジニア職とビジネス職とのコミュニケーションを繋ぐ架け橋になれれば良いですね!!!(名言)

それでは、良いクリスマスを!!!🎄

2
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
2
1