使用IFTTT Webhook从Powershell传递值

disho6za  于 2022-12-13  发布在  Shell
关注(0)|答案(1)|浏览(129)

我在IFTTT中创建了一个Webhook,让我的服务器根据事件向我的Android手机发送通知。由于某种原因,这些值没有在Web请求中沿着。查看Webhook文档,它说:

To trigger an Event with 3 JSON values

Make a POST or GET web request to:

https://maker.ifttt.com/trigger/{event}/with/key/{MyKey}

With an optional JSON body of:

{ "value1" : "", "value2" : "", "value3" : "" }

The data is completely optional, and you can also pass value1, value2, and value3 as query parameters or form variables. This content will be passed on to the action in your Applet.

事件设置为显示Your test value is {{value1}}

然后,我尝试在Powershell中运行此代码:

$WebhookURL = "https://maker.ifttt.com/trigger/{MyEvent}/json/with/key/{MyKey}

Invoke-RestMethod -Method Get -Uri $($WebhookURL+"?value1=TESTVALUE") -ContentType "application/json"

然而,这会向我的手机发送一个通知,字面意思是Your test value is {{value1}}

因此,通知被正确触发,只是“value1”的值没有正确沿着。我也尝试过将该值作为-Body传递,但结果是一样的:

$Body = @{
    value1 = "TESTVALUE"
}

Invoke-RestMethod -Method Get -Uri $WebhookURL -Body $Body -ContentType "application/json"

$Body值传递给ConvertTo-Json -Compress会导致请求失败,并返回HTTP Error 403: Bad Request,所以也不是这样。本质上,我正在执行exactly like this other Stack post suggests,但它没有传递值。这里可能有什么问题?

**更新:**我还按照Mathias的建议尝试了以下方法:

$Body = @{
    value1 = "TESTVALUE"
} | ConvertTo-Json -Compress

Invoke-RestMethod -Method Post -Uri $WebhookURL -Body $Body -ContentType "application/json"

但是结果是一样的。我也试着把value2和value3加上空字符串到Body,但是结果是一样的。我试着用和不用-Compress,但是结果是一样的,在value1两边用和不用引号都不会改变什么:

**UPDATE2:**直接使用Curl会抛出错误:

curl -X POST https://maker.ifttt.com/trigger/MyEvent/json/with/key/MyKey -H "Content-Type: application/json" -d '{"value1": TESTVALUE}'

{"errors":[{"message":"Unexpected token v in JSON at position 1"}]}

我也试着用-UseDefaultCredentials运行Powershell Invoke-RestMethod,但结果是一样的。

**UPDATE3:**IFTTT日志显示Value1的值未被传递,因此请求中一定有问题:

cgh8pdjw

cgh8pdjw1#

解决方法:不要使用Invoke-RestMethod,而使用Curl:

$WebhookURL = "https://maker.ifttt.com/trigger/MyEventName/with/key/MyKey"
$Command = 'curl -X POST -H "Content-Type: application/json" -d "{""value1"":""' + $VariableWithMyValueHere + '"",""value2"":""Test2"",""value3"":""Test3""}" ' + $WebhookURL
cmd.exe /C $Command

我不知道为什么这个Json可以和Curl一起工作,而不能和Invoke-RestMethod一起工作,但是既然它可以工作,我就不进一步挖掘了。

相关问题