2017-12-21 21 views
0

ダイアログフローを使用してGoogleアシスタントアクションを開発しようとしていました。私はwebhookを開発しているときにいくつかの問題に遭遇しました。私はPythonを使用しています。コードは次のとおりです。ダイアログフローのWebフックを構築する際の問題

import json 
import os 
import urllib 
from flask import Flask 
from flask import request 
from flask import make_response 
app=Flask(__name__) 
@app.route('/webhook',methods=['POST']) 
def webhook(): 
    req = request.get_json(silent=True,force=True) 
    print("Request:") 
    print(json.dumps(req,indent=4)) 
    res={ 
     "speech": "Complete", 
     "displayText": "Complete", 
     "source": "Myself" 
    } 
    res=json.dumps(res,indent=4) 
    r=make_response(res) 
    r.headers['Content-Type']='application/json' 
    return r 

if __name__ == '__main__': 
    port=int(os.getenv('PORT',8080)) 
    app.run(port=port,host='localhost',ssl_context='adhoc') 

問題は、スクリプトによって返されたJSONオブジェクトが常に空であることです。 ngrok使用して、私は、オブジェクトの履行キーで次のようなものだ:

"fulfillment": { 
     "speech": "", 
     "messages": [] 
    } 

を、私は理由を理解することはできませんよ。どんな助けもありがとう。

+0

解決方法を使用して投稿のタイトルを変更しないでください。答えがあなたの問題解決に役立つ場合は、それを正しいとマークしてください。あなた自身の回答があればそれを作成し、正しい。 – eyllanesc

答えて

0
from flask import Flask, request, jsonify 

app = Flask(__name__) 

base_response = { 
       'speech':"sample response", 

       'source' : 'Manual'} 


@app.route('/',methods=['GET','POST']) 
def index(): 
    if request.method == 'GET': 
     text = """WELCOME to RBG<br> 
     /testing -> red testing<br>""" 
     return text 
    else: 
     req_body = request.get_json() 
     print(req_body) 
     response = base_response.copy() 
     return jsonify(response) 

if __name__ == '__main__': 
    app.run(host='0.0.0.0',port=5000,debug=True) 

これは私に役立ちました。お役に立てれば。私は私のラズベリーパイからngrokも使いました。

0

Changlineこれらの行:

port=int(os.getenv('PORT',8080) 
app.run(port=port,host='localhost',ssl_context) 

app.run(port=8080,host='localhost') 

には私のために問題を解決しました。

関連する問題