0

在發送道活動機器人作爲每文檔這裏步驟directline V3給出錯誤400

https://docs.botframework.com/en-us/restapi/directline3/#navtitle

我應該通過此作爲POST請求的主體內容

{ 
    "type": "message", 
    "from": { 
    "id": "user1" 
    }, 
    "text": "hello" 
} 

我正在使用以下參數在python中發出POST請求,但它不起作用。

msg = {"type": "message","channelId": "directline","conversation":{"id": str(convId)},"from":{"id": "test_user1"},"text": "hello"} 
header = {"Authorization":"Bearer q1-Tr4sRrAI.cwA.BmE.n7xMxGl-QLoT7qvJ-tNIcwAd69V-KOn5see6ki5tmOM", "Content-Type":"application/json", "Content-Length": "512"} 
send2 = "https://directline.botframework.com/v3/directline/conversations/"+str(convId)+"/activities" 
rsa1 = requests.post(send2,data=msg, headers=header) 

這給了我這個錯誤:

{ 
    "error": { 
    "code": "MissingProperty", 
    "message": "Invalid or missing activities in HTTP body" 
    } 
} 

之前這一步一切工作正常。

編輯1:連我加入作爲更新代碼內容長度,它提供相同的錯誤

編輯2:如果我改變了MSG到json.dumps(MSG)

rsa1 = requests.post(send2,data=json.dumps(msg), headers=header) 

我得到的迴應是:

{u'error': {u'message': u'Failed to send activity: bot returned an error', u'code': u'ServiceError'}} 
{ 
    "error": { 
    "code": "ServiceError", 
    "message": "Failed to send activity: bot returned an error" 
    } 
} 

直線API不工作,在Skype客戶端上一切工作正常。

+0

可能的重複[MissingProperty錯誤在Microsoft Bot框架請求](https://stackoverflow.com/questions/41727907/missingproperty-error-in-microsoft-bot-framework-request) –

+0

不,我已添加內容 - 但它也不起作用。 –

回答

0

根據你已經發布的代碼,你的請求主體看起來是這樣的:

{ 
    "type": "message", 
    "channelId": "directline", 
    "conversation": { 
     "id": str(convId) 
    }, 
    "from": { 
     "id": "test_user1" 
    }, 
    "text": "hello" 
} 

我懷疑你的錯誤是由以下事實引起的,你包括會話ID值在請求正文中沒有用通過電報發送的JSON中的雙引號包圍。

爲了簡化(並消除你收到的錯誤),我建議大家儘量省略的channelID財產和請求主體的談話財產,因爲我不相信,無論是這些屬性是必要的(例如,您向Direct Line URI發出請求,所以Bot Framework知道Direct Line是通道,並且對話ID已經在請求URI中指定)。換句話說,試試這個作爲你的請求正文:

{ 
    "type": "message", 
    "from": { 
     "id": "test_user1" 
    }, 
    "text": "hello" 
} 
+0

我嘗試了所有的東西,首先我從他們的文檔中給出的例子中看到了活動的價值。沒有工作 –

0

傳遞一個字典時data它就會自動url編碼,因此API,因爲它預計JSON數據返回一個錯誤。

您可以使用json庫,或者最好將您的字典傳遞給參數json
使用json.dumps

rsa1 = requests.post(send2, data=json.dumps(msg), headers=header) 

使用requests只:

rsa1 = requests.post(send2, json=msg, headers=header) 

你也不必在header添加「內容長度」,如果你使用第二個例子中,你不必添加「Content-Type」。

+0

我嘗試了所有這些,他們都沒有工作。我現在陷入了這個問題一個月。我已經點擊了幾乎每個搜索引擎上的每個相關鏈接:P仍然沒有修復,但是,是的,當我按照您的建議操作時,錯誤將變爲502, {u'error':{u'message':u'無法發送活動:bot返回錯誤',u'code':u'ServiceError'}} rsa1 { 「error」:{ 「code」:「ServiceError」, 「message」:「無法發送活動:機器人返回了一個錯誤「 } } –

+0

這個問題是隻在直線API中,對於其他Rest API它工作正常 –