Sending a message to discord webhook with lua - lua

So I am trying to send a message to a discord webhook in Lua.
Currently I have this code:
local http = require("socket.http")
ltn12 = require("ltn12")
local payload = [[ {"username":"NAME","avatar_url":"","content":"MESSAGE"} ]]
http.request
{
url = "https://discordapp.com/api/webhooks/<redacted>",
method = "POST",
headers =
{
["Content-Type"] = "application/json",
["Content-Length"] = payload:len()
},
source = ltn12.source.string(payload),
}
That I found here: http://forum.micasaverde.com/index.php?topic=32035.0
But the message never arrives. What am I doing wrong?
Edit: I tested a bit and it seems like I get a 301 error code when I send this to the discord webhook.

Related

How to send recaptcha token in POST request (python)

I'm trying to login to my leetcode account via a POST request (for a project). This is what I have so far:
import requests
import os
login\_url = "https://leetcode.com/accounts/login/"
s = requests.Session()
res = s.get('https://leetcode.com/accounts/login/')
cookies = dict(res.cookies)
csrftoken = cookies\['csrftoken'\]
headers = {'Referer': url}
data = {
'csrfmiddlewaretoken': csrftoken,
'login': os.environ\['LEETCODE\_USERNAME'\],
'password': os.environ\['LEETCODE\_PASSWORD'\],
'next': '/problems/two-sum/',
'recaptcha\_token': (token from screenshot)
}
x = requests.post(login\_url, data=data, headers=headers, cookies=cookies)
print(x.text)
This screenshot shows the payload:
link
I tried to copy+paste that recaptcha_token in my data dictionary but I'm still getting this error:
{"recaptcha": {"error_type": "recaptcha_token_invalid", "error": "Something went wrong with reCAPTCHA."}}

Kinesis Firehose HTTP_Endpoint destination Response format

What is the right format of the Response for Kinesis Firehose with http_endpoint as destination. Have already gone through the aws link:
https://docs.aws.amazon.com/firehose/latest/dev/httpdeliveryrequestresponse.html#responseformat
I have used the below lambda code in python(integrated in api) as well as with many other options, but keep getting the below error message. The test is performed using the "Test with Demo Data" option
sample code:
def lambda_handler(event, context):
data ={}
headersD = {}
headersD['content-length'] = 0
headersD['content-type'] = 'application/json'
data['requestId'] = 'ed4acda5-034f-9f42-bba1-f29aea6d7d8f'
data['timestamp'] = '1578090903599'
bodyDetail= {}
data['body'] = ''
data['headers'] =headersD
data['statusCode']=200
resp = json.dumps(data)
return resp
error response as seen in the logs:
The response received from the endpoint is invalid. See Troubleshooting HTTP Endpoints in the Firehose documentation for more information. Reason:. Response for request 'request-Id' is not recognized as valid JSON or has unexpected fields. Raw response received: 200 "HttpEndpoint.InvalidResponseFromDestination"
Here is the sample output that worked(in python):
responseBody = {
"requestId": "requestId",
"timestamp": 123456
}
resp = {
"headers": {"Content-Type": "application/json", "Content-Length": 100},
"body": json.dumps(responseBody),
"statusCode": 200
}
return resp

Lua request from Tado thermostat api

I'm building a so called 'Quickapp' in Home Center 3 (From Fibaro) in the 'Lua' programming language. I want to fetch some data from the Tado api, but it's poorly documented. I keep getting the following message from the console:
Full authentication is required to access this resourceunauthorized
I think that's because I need do assign the Bearer token from the request, but i'm a little lost how...
This is what i have so far:
function QuickApp:fetchTadoData(username,password,client_secret)
local url = "https://auth.tado.com/oauth/token"
local postdata = {
["client_id"] = "tado-web-app",
["grant_type"] = "password",
["scope"] = "home.user",
["username"] = username,
["password"] = password,
["client_secret"] = client_secret
}
local extraheaders = {
["content-type"] = "application/json"
}
self.http:request(url, {
options={
headers = extraheaders,
data = json.encode(postdata),
method = "POST"
},
success = function(status)
self:debug(status.data)
end,
error = function(error)
errorlog("Error getting data: "..error)
self:debug("hallo")
end
})
end
I know the Curl code to get the 'Bearer token' response:
curl -s "https://auth.tado.com/oauth/token" -d client_id=tado-web-app -d grant_type=password -d scope=home.user -d username="you#example.com" -d password="Password123" -d client_secret=wZa
But I don't know how to translate this to the above Lua code. Any help is appreciated!
https://manuals.fibaro.com/home-center-3-quick-apps
Looks OK, the main thing I'm noticing is this:
"self.http must have previously been created by net.HTTPClient"
function QuickApp :fetchTadoData( username, password, client_secret )
self .http = net .HTTPClient( { timeout = 5000 } ) -- 5 seconds
local url = "https://auth.tado.com/oauth/token"
local requestBody = {
action = 'create',
params = {
["client_id"] = "tado-web-app",
["grant_type"] = "password",
["scope"] = "home.user",
["username"] = username,
["password"] = password,
["client_secret"] = client_secret
}
}
local extraheaders = {
["content-type"] = "application/json",
["accept"] = "application/json"
}
self .http :request( url, {
options = {
headers = extraheaders,
data = json .encode( requestBody ),
method = "POST"
},
success = function( response )
self :debug( response .status )
self :debug( response .data )
end, -- success
error = function( msg )
self :debug( 'Error: ' ..msg )
end -- error
})
end -- :fetchTadoData()

Segmentation fault on http.request

Using the Lua Socket library I want to send a get request to an API, but everytime I do so it just returns segmentation fault and I can't figure out why!
local http = require("socket.http")
local data = {
id = "1234"
}
local response = http.request {
method = "GET",
headers = {["Content-Type"]="application/json"},
url = 'http://example.com/api/',
data = data
}
local tokenResponse = json.parse(response.content)
Doesn't matter how I try it, always returns segmentation fault. Any clues why?
I'm running on Debian 8.6 Jessie.

Slack Incoming Webhook API

I'm able to POST to the Slack incoming API endpoint via CURL, but when trying with the below its not working. I'm assuming the formatting if off. How can I fix this?
parms = {text: text_for_slack, channel: "#customer_sessions", username: "SessionBot", icon_emoji: ":raised_hands:"}
x = Net::HTTP.post_form(URI.parse(ENV['SessionSlackURL'].to_s), parms.to_s)
You can post using two methods (text from slack configuration for incoming webhook) :
You have two options for sending data to the Webhook URL above:
Send a JSON string as the payload parameter in a POST request
Send a JSON string as the body of a POST request
json in the body.
require "net/http"
require "uri"
require "json"
parms = {
text: text_for_slack,
channel: "#customer_sessions",
username: "SessionBot",
icon_emoji: ":raised_hands:"
}
uri = URI.parse(ENV['SessionSlackURL'])
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request.body = parms.to_json
response = http.request(request)
json as parameter
parms_form = {
"payload" => {
text: text_for_slack,
channel: "#customer_sessions",
username: "SessionBot",
icon_emoji:":raised_hands:"
}.to_json
}
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(parms_form)

Resources