fkubota / Carrier-Owl

arxiv--> DeepL --> Slack/LINE
MIT License
329 stars 356 forks source link

Add message template #72

Closed acd1034 closed 2 years ago

acd1034 commented 2 years ago

素晴らしい機能をありがとうございます! よりよくできればと思い、変更を提案します。お時間のある時にみてください。

本PRの動機

1

54, #66 で送信メッセージに付与する情報を変更したい/増やしたいとの要望がありました。

そこで config.yaml にフィールドを増やして付与する情報を制御する案が出ていました。 例: https://github.com/fkubota/Carrier-Owl/issues/54#issuecomment-776439292 しかし config.yaml にフィールドを増やすのは、以下の点で得策ではないと考えています。

2

別の動機として、送信メッセージを装飾したいというニーズがあると思います。

方法

以上の要望を string.Template クラスの substitute メンバを用いて実装したいと考えます。 これは、文字列を辞書を用いて置換する機能です。

template = 'I am ${name}. I like ${fruit}.'
d = {'name': 'Alice'}
Template(template).substitute(d, fruit='apple')
# → 'I am Alice. I like apple.'

変更点

template フィールド

config.yamltemplate というフィールドを追加しました。 このフィールドは必須ではありません。 以下の設定で以下の結果が得られます。

# メッセージテンプレート
template: |
        *${title}*
        👤 *Authors*: ${authors}
        🔑 *Keywords*: ${words}
        ⚽ *Score*: ${score}
        🔗 *URL*: ${arxiv_url}
        📝 *Abstract*:
         ${summary_trans}
         ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
スクリーンショット 2022-03-16 16 22 49

template フィールドのデフォルト値

template を設定しない場合のデフォルト値とその結果を下に示します。

# メッセージテンプレートのデフォルト値
default_template = '\n score: `${score}`'\
                   '\n hit keywords: `${words}`'\
                   '\n url: ${arxiv_url}'\
                   '\n title:    ${title_trans}'\
                   '\n abstract:'\
                   '\n \t ${summary_trans}'\
                   '\n ${star}'
スクリーンショット 2022-03-16 16 22 17

template フィールドで使える変数

arXiv API 由来の変数

arXiv API 由来の変数は arXiv API で用いられている名称そのものを用いています。 詳細は Reference を確認してください。 よく使うものを以下に示します。

変数 説明
${arxiv_comment} 著者のコメント(あれば)
${arxiv_url} URL
${authors} 著者
${published} 公開日
${summary} 概要(アブストラクト)
${title} 論文のタイトル
${updated} 更新日

arXiv API 由来でない変数

翻訳された概要などの arXiv API 由来でない変数は以下のように設定しました。

変数 説明
${words} ヒットしたキーワード
${score} スコア(ヒットしたキーワードの重みの総和)
${title_trans} 翻訳されたタイトル
${summary_trans} 翻訳された概要
${star} '*'*80

実装の詳細

以下にコメントします。