我是Python的新手,并一直在尝试使用传出的Webhook将其与MS Teams集成。我创建了一个简单的Web服务来使用Flask处理发布请求,并使用ngrok托管了该请求,并且ngrok生成的URL已用作团队中的外发Webhook回调URL。
我可以在服务中接收消息并发送自适应卡作为响应。但是,如果我填写了信息并单击“提交”,我会得到“发生错误。请重试”错误,我的服务没有收到任何内容。
我们将不胜感激。Screenshot for Adaptive card in Teams
from flask import Flask, request, jsonify
import hmac, hashlib, base64
app = Flask(__name__)
@app.route('/testjson',methods = ['POST', 'GET'])
def webhook():
try:
if request.method == 'POST':
# Reply
data = request.get_json()
channel = data['channelId']
message_type = data['type']
sender = data['from']['name']
sender_id = data['from']['id']
message_format = data['textFormat']
message = data['text']
conversation_id = data['conversation']['id']
# Authenticate
security_token = b"sWYCgxib4hRcuHSKcKJk6w8FF6dVOa3mgmgK6thBMBg="
request_data = request.get_data()
digest = hmac.new(base64.b64decode(security_token), msg=request_data,
digestmod=hashlib.sha256).digest()
signature = base64.b64encode(digest).decode()
# Verify that HMAC header == signature
if request.headers.get('Authorization').split(' ')[1] == signature:
return jsonify({
"type": "message",
"text": "hi",
"attachments": [{
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.0",
"body": [
{
"type": "TextBlock",
"text": "Present a form and submit it back to the originator"
},
{
"type": "Input.Text",
"id": "firstName",
"placeholder": "What is your first name?"
},
{
"type": "Input.Text",
"id": "lastName",
"placeholder": "What is your last name?"
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit",
"data": {
"msteams": {
"type": "messageBack",
"displayText": "I clicked this button",
"text": "text to bots",
"value": "{\"bfKey\": \"bfVal\", \"conflictKey\": \"from value\"}"
}
}
}
]
}
}]
})
else:
print("hi")
except Exception as ex:
print(ex)
if __name__ == '__main__':
app.run(debug=True)