How to get webhook data in python - python

I have a webhook url from messagebird, and post requests are being sent from the LINE Messaging API to that url when some events occur. I need to get the data of those webhooks (JSON data). I'm using Python (it's a Django app).
How can I get the data from the webhook?

Related

How to forward a photo from Telegram group chat to Slack (using Webhooks and Telegram Bot)?

I'm trying to build a Telegram bot which forwards messages from a Telegram group chat to a Slack channel using Webhooks.
at the moment, I was able to forward all text messages and photo captions (caption only).
If a file (photo, video, etc.) is sent in the connected group, is there a way to get it's URL and forward it to the post request to Slack?
When asked, Slack support told me I can send a URL but I couldn't find it the the JSON update sent by the Telegram bot when a file was sent.
Thanks for the help
after searching and asking experts,
after receiving file_id in the webhook update from the Telegram bot,
in order to get the image url as Slack requires, one should GET request the path from Telegram api using this URL:
"https://api.telegram.org/bot/<BOT_TOKEN>/getFile?file_id=<FILE_ID>"
and accessing 'file_path' under json_dict['result']['file_path']
The next step is to use the path and add it to this URL:
"https://api.telegram.org/file/bot/<BOT_TOKEN>/<FILE_PATH>"
then send a POST request to the slack webhook URL using the pattern mentioned in the link: https://api.slack.com/messaging/webhooks
more info can be found at TelegramAPI manual website:
https://core.telegram.org/bots/api#getfile

Okta Flask Integration

I want to protect my Flask REST endpoints using Okta and OpenID connect.
I have seen how to do it for routes that are views of the application, but is there a way to integrate Okta with REST endpoints? When I make calls to my API, I get the html for Okta as a response, even if I'm logged in in the browser.
What HTML do you get from Okta? in one case you might get a HTML page with a self-submitting form that is meant to be sent back to your client, and that's normal.
But at the same time, the openid-connect implementation in the API is different from your client applications. The API should not deal with user-redirects, instead it should only listen for access-tokens in the authorize request header. The API should for the received tokens validate them against Okta.
To add token to request, then see this question Flask Rest API - How to use Bearer API token in python requests

Python Requests Programmatically get Dev Tools Form Data pre-formatted as a dictionary

I am trying to update an already saved form on a system using HTTP requests. Due to the server configuration for the third party app we use, updating by POST requires sending a fully filled out payload every single time.
I want to get round this by recovering the form data already present on the server and converting it into a dictionary. Then changing any values I need and reposting to make changes sever side.
The application we use sends a POST request when the save button is clicked for a particular form.
Here I send a post request with no payload.
[This simulates pressing the save button and is also the point where dev tools shows me a the payload I want to capture]
post_test = self.session.post(url_to_retrieve_from)
I thought that now I should be able to print the output, which should resemble what Google Dev tools Form data captures.
print(post_test.text)
This just gives me html found on the webpage.
If Dev Tools can get this from the server then I should also be able to?
Example of Data I am trying to get via requests:
Form Data
If Dev Tools can get this from the server then I should also be able to?
Yes, of course. In requests you pass form data in data keyword:
import requests
url = 'http://www.example.com'
data = {
'name': 'value',
}
response = requests.post(url, data=data)
You can get the data you sent with a request from the response in this way:
import requests
response = requests.post('http://your_url', data=data) # send request
body = response.request.body
parsed_data = dict(data.split('=') for data in body.split('&')) # parse request body
Here you can find more information about data argument
In the documentation, in the class requests.Response we can find the attribute:
request = None
The PreparedRequest object to which this is a response.
In requests.PreparedRequest class we can read:
body = None
request body to send to the server.

SOAP client in python, how to replicate with XML

I am using suds to send XML and I got my request working, but I'm really confused by how to replicate my results using XML. I have the XML request that my suds client is sending by using:
from suds.client import Client
ulr = "xxxxxxx"
client = Client(url)
...
client.last_received.str()
but I'm not sure where I would send that request to if I was using the requests library. How would I replicate the request from the suds client in a python request?
Most SOAP APIs are just over plain HTTP, use POST - and therefore are easily mimicked with any standard HTTP client such as Requests.
First look here to see how to view the headers and body that suds is sending - it is then a matter of replicating these headers/XML body and passing them into the Requests library.
One defining characteristic in 99% of all HTTP SOAP API's is that your request is going to the same end-point for each request (for example 'http://yyy.com:8080/Posting/LoadPosting.svc), and the actual action is specified in the header using SOAPAction header). Contrast this to a RESTful API where the action is implied with the verb + end-point you call (POST /user, GET /menu etc.)

multipart/form-data File upload request from Flash to GAE misses data

I have a google app engine request handler that processes multipart/form-data request from file uploads. It works fine with a html form but with the flash uploader it seems that no parameters are passed through.
Here is the data I have put together:
https://gist.github.com/martinheidegger/b8ee4982eb9580b9baa9
It contains the two http requests, one working, one broken together with the log output for logging.info("%s" % self.request.POST);
What am I missing? Why does the flash request not "arrive" at the GAE?
Are you receiving the POST request but missing all the POST data?
It could be due to the HTTP headers were not sent properly in Flash.
Try to read and parse directly from the HTTP request body and you should get something.

Categories

Resources