以前、Tableau Serverのダッシュボードをサブスクライブ機能を使って、Outlook経由でもって、Slackに投稿する記事を公開しました。
今回も同様処理を試みようと思いましたが、画像をアップロードするのに苦戦しています。
完璧ではございませんが、
とりあえず、画像情報付きのメッセージをTeamsに上げる方法を紹介します。
チャネルを作成し、Webhookを追加する
チャネルの作成
Teamsの「・・・」ボタンを押下し、チャネルを追加します。

チャネル名を設定し、追加。

Incoming Webhook アプリの追加
次にAppsから「Incoming Webhook」を追加します。
左下部の「アプリ」押下し、「webhook」で検索
webhook関連のアプリが表示されるので
「Incoming Webhook」を選択します。

「チームに追加」を押下します。

中央部でチャネルを選択し、「コネクタを設定」をクリックします。

名前を入力し、「作成」をクリックします。

すると以下の通り、URLボックスが表示されるのでコピーします。

チャネルに戻ると以下の通り、作成したアプリが追加されたことを確認できます。

PythonからTeamsに投稿する
前回のTableau ServerからSlackに連携する方法と同じ処理をします。
細かな説明はこちらをご確認下さい。
Outlookへのログイン
Microsoft 365へのログインの場合
from exchangelib import Credentials, Account, FileAttachment
credentials = Credentials(username='<outlookのログインID>', password='<outlookのログインパスワード>')
account = Account('<outlookのメールアドレス>', credentials=credentials, autodiscover=True)
Exchange Serverの場合
from exchangelib import DELEGATE, Account, Credentials, Configuration, FileAttachment
credentials = Credentials(username = '<ユーザー>',password = '<パスワード>')
config = Configuration(server='<サーバー情報>', credentials=credentials)
account = Account(
primary_smtp_address = '<メールアドレス>',
config = config,
autodiscover = False,
access_type = DELEGATE
)
Outlookに送信されたTableau Serverのダッシュボード画像をローカルに保存
import os
path = '<画像ファイルを保存するローカルパス>'
for item in account.inbox.filter('subject:<Tableau配信メールの件名>').order_by('-datetime_received')[:1]:
for attachment in item.attachments:
if isinstance(attachment, FileAttachment):
filepath = os.path.join(path, attachment.name)
with open(filepath, 'wb') as file:
file.write(attachment.content)
保存した画像をメッセージと共にTeamsに連携
import requests
import json
import base64
webhook = '<Teamsで取得したWebhookのURL>'
with open(filepath, "rb") as file:
base64image = base64.b64encode(file.read()).decode("ascii")
image = "![]" + "(" + f"data:image/png;base64,{base64image}" + ")"
data = {
"title": "<タイトル>",
"text": image
}
headers = { "Content-type": "multipart/form-data" }
requests.post(webhook, json = data, headers = headers)
画像情報はエンコードしています。
実行してみました。

・・・結論申しますと、こちらrequests上は200で正常処理されますが、画像情報はTeamsに投稿されませんでした。
というのも「HTTP error 413」で落ちているっぽいです。
つまり、画像情報をエンコードした際の文字列が長すぎたようです。
以下の小さな画像を用意して、試しに上記を実行してみました。
用意した画像

実行結果

送れた!と思ったら、表示枠切れてます。
こちらの画像、ダウンロードしたところ、画像自体は切れていませんでした。表示の問題だけのようですが。。。
とはいえ、これではTableauのダッシュボードの画像をTeamsに送信できたとしても使い物になりません。
困りました。
とりあえず、Microsoft Teamsに投稿する場合は制限を気にする必要があることが分かりました。
本来はTeams内である程度議論を完結させたかったですが、
今時点ではTableau ServerのURLも含めて送る等、工夫が必要です。
もうしばらく色々と調べてみます。
参考になる記事でなく、恐縮です。
では。