尝试使用Microsoft BotFram的直连API发送消息时出现错误500

2024-09-26 22:09:41 发布

您现在位置:Python中文网/ 问答频道 /正文

不知道我的问题在哪里。我会在docs页面中使用Try-It-Out选项,但似乎没有任何方式提供令牌/机密。

当我试图从Jupyter笔记本向我的机器人发送消息时,我一直收到错误500。我的代码是:

class DirectLineConversation:

"""A conversation with a bot via the Direct Line API."""

def __init__(self, direct_line_secret):
    self._direct_line_secret = direct_line_secret        
    self._base_url = 'https://directline.botframework.com/api'    
    self._initiate_conversation()

def _initiate_conversation(self):
    self._init_headers()
    requests.post("https://directline.botframework.com/api/tokens/conversation", headers=self._headers)
    r = requests.post("https://directline.botframework.com/api/conversations", headers=self._headers)
    json_ = r.json()
    self.conversationId = json_['conversationId']
    self.token = json_['token']

def _init_headers(self):
    headers = {'Content-Type': 'application/json'}
    headers.update(self._get_authorization_pair())
    self._headers = headers

def _get_authorization_pair(self):
    value = ' '.join(['BotConnector', self._direct_line_secret])
    return {'Authorization': value}

def send_message(self, text):
    url = '/'.join([self._base_url, 'conversations', self.conversationId, 'messages'])
    print(url)
    return requests.post(url, headers=self._headers, json={'conversationId': self.conversationId,
                                                           'text': text})


convo = DirectLineConversation(_DIRECT_LINE_SECRET)
response = convo.send_message("Hello")

print(response.status_code)
print(response.json())

根据文档,模式中的所有内容都是可选的,所以我假设传递json={'text':text}可以很好地工作,但是我也尝试过将所有其他值都设置为null,并且使用conversationId,就像我在其他几个so答案中看到的那样。

输出如下:

https://directline.botframework.com/api/conversations/3BpMMKssV6P/messages 500 {'message': 'failed to send message'}


Tags: texthttpsselfcomjsonurlsecretdef

热门问题