PythonからのWEBリクエスト実施方法(IFTTTのWebhooksへのリクエスト)
Webhooksは、WEB APIとなります。
Webhook(Webコールバック、HTTPプッシュAPIなど)はあるアプリケーションから別のアプリケーションに対してリアルタイムな情報提供を実現するための仕組みです。
Web/URLアクセスを行います。
IFTTTは、「if(もし) this(この状態) then(の場合は) that(それをする)」なので、
IFTTT処理の実行を開始させたい場合は、PythonよりWebhookを実行します。(PythonよりWebhookにURLアクセスします)
IFTTT処理でアクションさせたい処理がSynology側などにある場合は、WebhookよりSynologyへURLアクセスさせます。
トリガー(That)
外部よりWebhookにURLアクセスする必要があります。
その際のアクセスするURLや、アクセスに必要なアクセスキーは、Webhooksの「Documation」画面より入手可能です。
{event}はトリガーごとに設定するので、IFTTTでレシピを作成する際に指定します。
実際にThis部分を指定する画面は以下となります。
Event Nameを設定する部分があり、ここで、設定した文字列がURL部分{event}となります。
例:https://maker.ifttt.com/trigger/test/with/key/XXXXXXXXXXXXXXXXXXXXXX
トリガーアクセス時に変数を加える場合は、GETやPOSTで変数を与えます。
IFTTTのWebhooksへのアクション時Pythonコードサンプル
import urllib.request #params for IFTTT ifttt_api_key = '/with/key/@アクセスキー@' url_base = 'https://maker.ifttt.com/trigger/' #EVENT name for IFTTT ifttt_event = 'test' url = url_base + ifttt_event + ifttt_api_key params = {'value1': 123,'value2': 123,'value3': 123,} req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params))) with urllib.request.urlopen(req) as res: body = res.read()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib.request | |
#params for IFTTT | |
ifttt_api_key = '/with/key/@アクセスキー@' | |
url_base = 'https://maker.ifttt.com/trigger/' | |
#EVENT name for IFTTT | |
ifttt_event = 'test' | |
url = url_base + ifttt_event + ifttt_api_key | |
params = {'value1': 123,'value2': 123,'value3': 123,} | |
req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params))) | |
with urllib.request.urlopen(req) as res: | |
body = res.read() | |