Teams Invoke_response 被 Teams 拒绝了?
Teams Invoke_response being rejected by Teams?
我在独立服务器上有一个团队应用程序 运行,并为该应用程序上的消息扩展设置了 AzureBot。我有一个 class 实现消息处理扩展 TeamsActivityHandler,这一切似乎都在工作。问题是当我使用 InvokeResponse 进行响应时。我将其放入正确类型的 activity 中,我没有收到任何错误,但 Teams 似乎拒绝了该消息,因为它没有给我包含响应的 POST 请求的 ID。创建 Activity 时我遗漏了什么吗?
async def handle(turn_context):
invoke_response = await message_handler.on_invoke_activity(turn_context)
# invoke_response is an instance of botbuilder.schema._models_py3.InvokeResponse
result = await turn_context.send_activity(Activity(type=ActivityTypes.invoke_response, value=invoke_response))
self.logger.info(result)
我完全不知道消息扩展遵循标准请求响应模型,而不是像机器人那样遵循请求请求模型。这是一个使用 flask 的完整工作示例 - ms 示例 here :
import asyncio
import json
from botbuilder.core.teams import TeamsActivityHandler
from botbuilder.schema import Activity, CardAction, HeroCard
from botbuilder.core import (
BotFrameworkAdapter,
BotFrameworkAdapterSettings, TurnContext, CardFactory, MessageFactory,
)
from botbuilder.schema.teams import (
MessagingExtensionAttachment,
MessagingExtensionQuery,
MessagingExtensionResult,
MessagingExtensionResponse,
)
from flask import request, Response
from flask_restful import Resource
class ActivityHandler(TeamsActivityHandler):
async def on_teams_messaging_extension_query(self, turn_context: TurnContext, query: MessagingExtensionQuery):
search_query = str(query.parameters[0].value).strip()
if search_query == '':
await turn_context.send_activity(
MessageFactory.text('You cannot enter a blank string for the search')
)
return
# TODO: Implement a search returning objects
search_results = self._get_search_results(search_query)
attachments = []
for obj in search_results:
hero_card = HeroCard(
title=obj['name'], tap=CardAction(type='invoke', value=obj)
)
attachment = MessagingExtensionAttachment(
content_type=CardFactory.content_types.hero_card,
content=HeroCard(title=obj['name']),
preview=CardFactory.hero_card(hero_card),
)
attachments.append(attachment)
return MessagingExtensionResponse(
compose_extension=MessagingExtensionResult(
type='result', attachment_layout='list', attachments=attachments
)
)
class TeamsMessageExtensionsBot(Resource):
def __init__(self):
self.event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.event_loop)
app_config = BotFrameworkAdapterSettings(app_id='YOUR-BOT-ID',
app_password='YOUR-BOT-PASSWORD')
self.bot_framework_adaptor = BotFrameworkAdapter(app_config)
def post(self):
message_handler = ActivityHandler()
if 'application/json' in request.headers['Content-Type']:
body = request.json
else:
return Response(status=415)
activity = Activity().deserialize(body)
auth_header = (
request.headers['Authorization'] if 'Authorization' in request.headers else ''
)
try:
task = self.event_loop.create_task(
self.bot_framework_adaptor.process_activity(activity, auth_header, message_handler.on_turn)
)
invoke_response = self.event_loop.run_until_complete(asyncio.gather(task))[0]
if invoke_response:
self.logger.info(invoke_response.body)
return Response(response=json.dumps(invoke_response.body),
status=invoke_response.status, mimetype='application/json')
return Response(status=201)
except Exception as exception:
raise exception
我在独立服务器上有一个团队应用程序 运行,并为该应用程序上的消息扩展设置了 AzureBot。我有一个 class 实现消息处理扩展 TeamsActivityHandler,这一切似乎都在工作。问题是当我使用 InvokeResponse 进行响应时。我将其放入正确类型的 activity 中,我没有收到任何错误,但 Teams 似乎拒绝了该消息,因为它没有给我包含响应的 POST 请求的 ID。创建 Activity 时我遗漏了什么吗?
async def handle(turn_context):
invoke_response = await message_handler.on_invoke_activity(turn_context)
# invoke_response is an instance of botbuilder.schema._models_py3.InvokeResponse
result = await turn_context.send_activity(Activity(type=ActivityTypes.invoke_response, value=invoke_response))
self.logger.info(result)
我完全不知道消息扩展遵循标准请求响应模型,而不是像机器人那样遵循请求请求模型。这是一个使用 flask 的完整工作示例 - ms 示例 here :
import asyncio
import json
from botbuilder.core.teams import TeamsActivityHandler
from botbuilder.schema import Activity, CardAction, HeroCard
from botbuilder.core import (
BotFrameworkAdapter,
BotFrameworkAdapterSettings, TurnContext, CardFactory, MessageFactory,
)
from botbuilder.schema.teams import (
MessagingExtensionAttachment,
MessagingExtensionQuery,
MessagingExtensionResult,
MessagingExtensionResponse,
)
from flask import request, Response
from flask_restful import Resource
class ActivityHandler(TeamsActivityHandler):
async def on_teams_messaging_extension_query(self, turn_context: TurnContext, query: MessagingExtensionQuery):
search_query = str(query.parameters[0].value).strip()
if search_query == '':
await turn_context.send_activity(
MessageFactory.text('You cannot enter a blank string for the search')
)
return
# TODO: Implement a search returning objects
search_results = self._get_search_results(search_query)
attachments = []
for obj in search_results:
hero_card = HeroCard(
title=obj['name'], tap=CardAction(type='invoke', value=obj)
)
attachment = MessagingExtensionAttachment(
content_type=CardFactory.content_types.hero_card,
content=HeroCard(title=obj['name']),
preview=CardFactory.hero_card(hero_card),
)
attachments.append(attachment)
return MessagingExtensionResponse(
compose_extension=MessagingExtensionResult(
type='result', attachment_layout='list', attachments=attachments
)
)
class TeamsMessageExtensionsBot(Resource):
def __init__(self):
self.event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.event_loop)
app_config = BotFrameworkAdapterSettings(app_id='YOUR-BOT-ID',
app_password='YOUR-BOT-PASSWORD')
self.bot_framework_adaptor = BotFrameworkAdapter(app_config)
def post(self):
message_handler = ActivityHandler()
if 'application/json' in request.headers['Content-Type']:
body = request.json
else:
return Response(status=415)
activity = Activity().deserialize(body)
auth_header = (
request.headers['Authorization'] if 'Authorization' in request.headers else ''
)
try:
task = self.event_loop.create_task(
self.bot_framework_adaptor.process_activity(activity, auth_header, message_handler.on_turn)
)
invoke_response = self.event_loop.run_until_complete(asyncio.gather(task))[0]
if invoke_response:
self.logger.info(invoke_response.body)
return Response(response=json.dumps(invoke_response.body),
status=invoke_response.status, mimetype='application/json')
return Response(status=201)
except Exception as exception:
raise exception