0

Im trying to create a script, either by Python or PHP, that can receive JSON from Dialogflow webhooks.

The data value will be from resolvedQuery.

I want to parse the data then send to the below URL:

https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?text=***JSON GOES HERE ***&deviceId=18c972b753ad&apikey=5b48aed7

The data from resolvedQuery needs to be sent to the about URL where *JSON GOES HERE * is.

Here is the Python code I have been trying:

from flask import Flask
from flask import request
from flask import make_response
import json
import logging as l
import requests

app = Flask(__name__)

@app.route('/')
def hello():
    """Return a friendly HTTP greeting."""
    l.info("reached the hello() module...")

    return 'Hello  Beautiful World!\n'

@app.route('/apiai', methods=['POST'])
def apiai_response():

requests_session = requests.session()
requests_session.headers.update({'Content-Type': 'application/json'})
requests_session.headers.update({'charset':'utf-8'})

post_data = '[{ "resolvedQuery": "" }]'

requests_response = requests_session.post(url="https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?text=***JSON GOES HERE ***&deviceId=18c972b753ad&apikey=5b48aed7", data=post_data)

print requests_response.content

@app.errorhandler(404)
def page_not_found(e):
    """Return a custom 404 error."""
    return 'Sorry, nothing at this URL.', 404
jwpfox
  • 5,124
  • 11
  • 45
  • 42
Null3rs
  • 19
  • 2
  • 1
    Please fix your identation and state what your problem is, precisely – gogaz Dec 07 '17 at 18:48
  • Welcome to stackoverflow. Url encoding a JSON string is not an easy problem, however this is how it is done in the python requests library. https://github.com/requests/requests/blob/master/requests/models.py#L60 – jmunsch Dec 07 '17 at 18:51
  • @Null3rs i'm wondering if the documentation allows for both cases where you can submit the post_data, as well as the query param? Where are the docs for the `sendPush` endpoint? – jmunsch Dec 07 '17 at 19:03
  • Also can you edit your question and add the spot in the documentation that you have been trying to figure out? also see: https://meta.stackexchange.com/questions/21788/how-does-editing-work && https://stackoverflow.com/tour && https://stackoverflow.com/help/how-to-ask – jmunsch Dec 07 '17 at 19:25

1 Answers1

0

I would try something like this:

import json
import requests

json_string = json.dumps({"hello": "world"})
these_params = {"text": json_string,
                "deviceId": "18c972b753ad&apikey=5b48aed7"}
this_url = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush"

r.get(url=this_url, params=these_params)

notice it is a get request and not a post request

It is a matter of opinion however using url encoded params on a GET request differ in how stuff gets read. As far as the data field is concerned on a post request, the handling is a bit different.

On a get request it will look something like part of the url as :

text=%7B%22hello%22%3A%22world%22%7D

Where as a post request the server will try to read in a application/json encoded body which expects the "data" to be valid json text.

also note:

jmunsch
  • 22,771
  • 11
  • 93
  • 114