A Python code used to get the parent message of a slack thread not working - slack-api

I wanted to make an API call to get the parent message of a thread using the thread ID on zapier
This is the code I got by chatgpt to do that, I am getting a no output error and I am not sure why it happens
import requests
def main(input_data):
# Set the API endpoint, headers, and Slack API token for the request
api_endpoint = "https://slack.com/api/conversations.replies"
headers = {
"Authorization": "Bearer xoxb-3482548707638-4570295651095-8J4mDrROrcYV0U3L5FZhJtTf",
"Content-Type": "application/json; charset=utf-8"
}
slack_api_token = "xoxb-3482548707638-4570295651095-8J4mDrROrcYV0U3L5FZhJtTf"
# Set the request parameters
params = {
"channel": input_data["thread_id"],
"ts": input_data["thread_id"],
"inclusive": "true",
"limit": 1
}
# Make a GET request to the API endpoint
response = requests.get(api_endpoint, headers=headers, params=params)
# Get the first message in the result
message = response.json()["messages"][0]
# Print the message text to the console
print(message["text"])
This my first time working with API's or code, so I am pretty sure I mus have done something stupid. Could anyone point out what I did wrong
PS: After this, what should I learn to operate such codes, I find this interesting

Related

Envoy Lua Filter - How to make HTTP request?

I would like to make http request from my lua filter to an external server.
According to Envoy documentation, http call can be done using request_handle:httpCall:
function envoy_on_request(request_handle)
-- Make an HTTP call to an upstream host with the following headers, body, and timeout.
local headers, body = request_handle:httpCall(
"lua_cluster",
{
[":method"] = "POST",
[":path"] = "/",
[":authority"] = "lua_cluster"
},
"hello world",
5000)
I have created a cluster called lua_cluster in my envoy.yaml file as needed, but the request doesn't reach my server and I'm getting 400 response.
Possible solution??
When changing the authority header from [":authority"] = "lua_cluster" to [":authority"] = "<cluster's url hostname>", the request arrived to the server and I got 200 response from the server. Can someone explain this? Is it a valid thing to do?

POST request errors out when CURL works

I’m trying to use a cloudflare worker (Pasted below) to send an SMS message via the Twilio API. The CURL request (also pasted below) I’m basing the worker off of works.
Based on the 400 error from the worker the message body isn’t passed in correctly
{"code": 21602, "message": "Message body is required.", "more_info": "https://www.twilio.com/docs/errors/21602", "status": 400}
but the code looks fine to me. We can at least confirm the header is passed correctly because messing with the authorization value changes the error.
I also looked at the example POST request in the template gallery and can’t see a reason for the failure.
https://developers.cloudflare.com/workers/templates/pages/post_json/
What do i need to change in my worker code to make the POST request work?
Note: i recognize i shouldn’t put the Twilio Auth token in the body but I’ll rotate the key later.
async function handleRequest(request) {
const init = {
body: JSON.stringify(body),
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': "Basic " + btoa('[account id]:[Authtoken]'),
},
}
return await fetch(url, init)
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest(event.request))
})
const url = 'https://api.twilio.com/2010-04-01/Accounts/[Account id]/Messages.json'
const body = {
Body:"Hello World",
From:"+[some number]",
To:"+[some number]]",
}
curl 'https://api.twilio.com/2010-04-01/Accounts/[Account id]/Messages.json' -X POST \
--data-urlencode 'To=+[some number]' \
--data-urlencode 'From=+[some number]' \
--data-urlencode 'Body=Hello World' \
-u [account id]:[auth token]
because Twilio requires application/x-www-form-urlencoded.
REST API: Your Request
Creating or updating a resource involves performing an HTTP PUT or
HTTP POST to a resource URI. In the PUT or POST, you represent the
properties of the object you wish to update as form urlencoded
key/value pairs. Don't worry, this is already the way browsers encode
POSTs by default. But be sure to set the HTTP Content-Type header to
"application/x-www-form-urlencoded" for your requests if you are
writing your own client.

Shopify API HTTP POST redirecting instead of POSTing

I have code (Classic ASP) which was recently working POSTing orders to Shopify but has now stopped POSTing and either creates an error "A Redirection problem has occurred" or redirects to the admin area of the Shopify site, depending on which XMLHTTP component I employ. The code below still works on older OS but not on Server 2016 where I am working.
I can't find much on Google but there was an indication in the Shopify Forum that the problem was a result of cookies (I have set none) and that this is overcome by sending a header containing X-Shopify-Access-Token:. I tried this using the "Authorize" setRequestHeader but it made no difference, or I got the syntax wrong or something. I used
xmlhttp.setRequestHeader "Authorization","X-Shopify-Access-Token=<token>"
Below is the code that worked a few weeks back. Variable jsondata contains valid JSON to send to create an order.
Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.3.0")
xmlhttp.Open "POST", "https://<api key>:<passowrd>#<sitename>.myshopify.com/admin/orders.json", false, "<api key>", "<password>"
xmlhttp.setRequestHeader "Content-Type", "application/json; charset=utf-8"
xmlhttp.setRequestHeader "Content-Length", Len(jsondata)
xmlhttp.Send jsondata
Set xmlhttp = nothing
I expect a POST and a JSON order response but this is not happening - just a redirection to https://<sitename>.myshopify.com/admin. Any ideas anyone?

Rest Assured :- Getting 404 response from Post request created using Pathparam and FormParam

I am a newbie to Rest Assured and need your help on the following issue.
I want to trigger a POST request which is as follows:-
Response resp = RestAssured.given().pathParam("build", bulid).
formParam("file", "https://unsplash.com/photos/Bcv4wZSMtIA").// Cast
formParam("type", "front").
formParam("auto_start", false).
then().post("https://example.com/{build}");
Where build= abc/xyz
Thus. should result in https://example.com/abc/xyz as endpoint and body as:
{
"file" : "https://unsplash.com/photos/Bcv4wZSMtIA",
"type" : "front",
"auto_start" : false
}
But when triggered it gives 404, whereas when instead of using pathparam I hardcode the value of build in post request then it works fine.
Can someone please advise what am I doing wrong here.
Found out that the url was being encoded when sent as request uri which when disabled using used urlEncodingEnabled(false) resolved my issue.

Post Method with NSDictionary Values using Swift

I'm completely new toSwift. I need to hit a Post Method webservice with NSDictionary parameters & get the JSON response. I tried usingAlamofire & also NSMutableUrlRequest. Nothing seems to workout for me. I either get 'JSON text did not start with array or object and option to allow fragments not set' error or 'Undefined Variable' response from the server. The same service works fine when I try using Objective-C. As I said earlier, I am completely new toSwift & need your assistance.
My base url: http://myofficeit.in/bizfeed/webservices/client.php
Parameter I wanna Pass:
Parameter =
{
UserName = xyz;
deviceModel = iPhone;
deviceToken = "949264bc cd9c6c851ee64cc74db9078770dd7d971618ec20ce91d2e6eb9f155e";
emailid = "xyz#gmail.com";
location = Asia;
userMobileNo = 1234567890;
};
functionName = register;
The code I used for hitting the service is: http://pastebin.com/aaT4uhS7
Thanks
you can use like
let param: [String:AnyObject] = [
"UserName": iPhone,
"deviceToken": "949264bc cd9c6c851ee64cc74db9078770dd7d971618ec20ce91d2e6eb9f155e",
"emailid": "xyz#gmail.com",
"location": Asia,
"userMobileNo": 1234567890
]
Alamofire.request(.POST, "http://myofficeit.in/bizfeed/webservices/client.php/register", parameters: param).responseJSON { (req, res, json, error) in
print(req)
print(res)
print(json)
print(error)
}
for sample request in Alamofire
As broad as your question is, the broad will be my answer:
The first thing to do, is to get a clear idea about the web service API, which also requires a basic knowledge of the HTTP protocol. So, what you need to understand is, what the server expects in HTTP terminology.
You eventually will find out, how the server will expect its "parameters". Note, that there is no term like "parameters" in the HTTP protocol. So, you need to map them into something the HTTP protocol provides.
Most likely, in a POST request, "parameters" are transferred as the body of the HTTP message, as a content-type which is application/x-www-form-urlencoded, multipart/form-data or application/json.
According to the needs of the server, and with your basic knowledge of HTTP and NSURLSession, NSURLComponents etc., you compose the URL and the body of the request, set Content-Type header and possibly other headers and you are ready to go.
How this eventually looks like is given in the answer of #AnbyKarthik, which used Alamofire, and a command that composes a POST request whose parameters are send in the body whose content-type is x-www-form-urlencoded.

Resources