nakamurakko’s blog

仕事で覚えたこと、勉強したことを自分のメモ代わりに書いていこうかなと。

Azure Logic Appを使ってTwitterのメンションをメール通知する

Azure Logic AppとAzure Functionsで、Twitterのメンションを拾って、メールで通知できるように作ってみた。

環境

  • Microsoft Azure
    • Logic App
    • Function App (Azure Functions)

やりたいこと

  • Twitterのつぶやきを監視する
  • 条件と一致した場合
    • Twitterのテキストを編集する
    • メールで通知する

f:id:nakamurakko:20200223094230p:plain

下準備 Function Appの用意

Twitterのテキストを編集するためにFunction Appを作成する。

f:id:nakamurakko:20200223094322p:plain

関数の作成

Twitterのつぶやきをメール通知前に編集する関数を作成したいので、Function AppでHttp Triggerを追加する。今回言語はJavaScriptを選択して作成した。(Azure Functions の JavaScript 開発者向けガイド)

Function Appの引数「data」には下記を設定する前提で関数を実装する。

  • doby.tweetby → ツイートした人
  • body.tweettext → ツイートテキスト

戻り値は、戻り値をつなげた文字列を返して、後でメールの本文として使用する。

module.exports = function (context, data) {
    // ツイートした人とツイートテキストを返す。
    var text = data.body.tweetby + 'さんからのツイート\n'
             + '-----\n'
             + data.body.tweettext;

    context.res = text;

    context.done();
};

関数の統合を開き、HTTPメソッドはPOSTのみを選択する。

f:id:nakamurakko:20200223094445p:plain

Function Appの公開

Logic Appから呼び出せるように、Function App → API定義を開き、「API定義テンプレートを生成する」をクリックする。

f:id:nakamurakko:20200223094530p:plain

Swaggerで記載されたテンプレートを下記のように編集。(参考:OpenAPI Specification)

swagger: '2.0'
info:
  title: AutomaticCalculationEquipment
  description: Tweetを編集して返す
  version: 1.0.0
host: automaticcalculationequipment.azurewebsites.net
basePath: /
schemes:
  - https
  - http
paths:
  /api/AutomaticCalculationEquipment:
    # HTTPメソッドのPOSTの定義
    post:
      operationId: /api/AutomaticCalculationEquipment/post
      produces:
        - application/json
      consumes:
        - application/json
      parameters:
        # リクエストパラメータ(tweet)は、bodyで指定する。
        # (ただし、Swaggerの仕様では、inにbodyを指定して良いと書かれていない…)
        - name: tweet
          in: body
          description: ツイート
          required: true
          schema:
            # definitionsで定義したTweetDataを使用する。
            $ref: '#/definitions/TweetData'
      responses:
        '200':
          description: 成功時のレスポンス
      security:
        - apikeyQuery: []
definitions:
  # TweetDataを定義
  TweetData:
    description: ツイートデータ
    type: object
    required:
      - tweetby
      - tweettext
    properties:
      tweetby:
        description: ツイートした人
        type: string
      tweettext:
        description: ツイートテキスト
        type: string
securityDefinitions:
  apikeyQuery:
    type: apiKey
    name: code
    in: query

Logic App作成

Logic Appを新規作成して、編集していく。

① トリガーは「新しいツイートが投稿されたら」

今回の設定は下記の通り。

  • 検索テキストは「nakamurakkobot(Twitterのアカウント名)」
  • 頻度は5分間

検索テキストに「@」を使えないので、次の条件で設定する。

f:id:nakamurakko:20200223094603p:plain

② 条件

条件の追加で、下記の条件を設定する。

  1. ツイートテキストが
  2. 次の値を含む
  3. @nakamurakkobot

f:id:nakamurakko:20200223094644p:plain

③ 条件と一致した場合の処理

③-1 Function Appの呼び出し

まず、Function AppにJSON形式でツイートした人、ツイートテキストを渡す。

f:id:nakamurakko:20200223094737p:plain

要求本文(body)に設定する式は下記の通り、{"tweetby": ツイートした人, "tweettext": ツイートテキスト}となるように作成している。

json(concat('{ "tweetby": "', triggerBody()?['TweetedBy'], '", "tweettext": "', triggerBody()?['TweetText'], '"}'))

③-2 メールの送信

続けて、メール送信を追加する。

  • 本文にFunction Appの本文(編集した結果)を設定
  • 件名を適宜
  • 宛先に通知したいメールアドレスを設定

f:id:nakamurakko:20200223094757p:plain

確認

つぶやいてみる。

f:id:nakamurakko:20200223094827p:plain

作成したLogic Appを通してメールが届いていることを確認。

f:id:nakamurakko:20200223094847p:plain

備考

本当はLogic Appの機能を使ってTwitterの返信をやりたかったけど、Twitter コネクターを確認すると「Mentioning a @user while posting a tweet is not supported. Specifically, the "@" characters will be stripped while posting a tweet.」という記述の通り、@が取り除かれるため返信できなかった。返信したい場合、Function Appなどで直接Twitter APIを呼び出すしかないようだ。

Azure Functionsの機能を使わずに、Logic Appだけで済ませることも可能だけど、Swaggerの勉強にもなったから、良しとしよう。