return await callback(context)
TypeError: 'coroutine' object is not callable
async def sendProactiveMessage(convoRef, filename, fileSize):
await ADAPTER.continue_conversation(convoRef,
# lambda TurnContext: TurnContext._send_file_card('Hello'),
BOT._send_file_card(TurnContext, filename, fileSize),
"----------------------------",
)
The following command works:
lambda TurnContext: TurnContext._send_file_card('Hello')
Send File Card To User

Add any other context about the problem here.
Hi @JoshBello,
Based on what you've written you've merged 3 samples (proactive messaging, files, and MS Teams) together. Can you help me understand what you're trying to do? I want to make sure you're using the best starting sample.
@Virtual-Josh Thanks for getting back to me. I am trying to send a html file to a user proactively. What is the best starting point?
@JoshBello The best one is probably the proactive sample. From there it should be fairly straight forward to add a file to the message.
So I've now used the provocative sample as my base. This is from my This is from app.py. This runs but every time I send a post to /notify I get a async def notify(req: Request) -> Response: # pylint: disable=unused-argument
filename = 'test.png'
fileSize = 10
await _send_proactive_message(filename, fileSize)
return Response(status=201, text="Proactive messages have been sent")
# Send a message to all conversation members.
# This uses the shared Dictionary that the Bot adds conversation references to.
async def _send_proactive_message():
for conversation_reference in CONVERSATION_REFERENCES.values():
return await ADAPTER.continue_conversation(
conversation_reference,
# lambda turn_context: turn_context.send_activity("proactive hello"),
BOT._send_file_card(TurnContext, filename, fileSize),
APP_ID,
)
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
APP.router.add_get("/api/notify", notify)
if __name__ == "__main__":
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
proactive_bot.py
from datetime import datetime
import os
import requests
from botbuilder.core import TurnContext
from botbuilder.core.teams import TeamsActivityHandler
from botbuilder.schema import (
Activity,
ChannelAccount,
ActivityTypes,
ConversationAccount,
Attachment,
)
from botbuilder.schema.teams import (
FileDownloadInfo,
FileConsentCard,
FileConsentCardResponse,
FileInfoCard,
)
from botbuilder.schema.teams.additional_properties import ContentType
class TeamsFileUploadBot(TeamsActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
message_with_file_download = (
False
if not turn_context.activity.attachments
else turn_context.activity.attachments[0].content_type == ContentType.FILE_DOWNLOAD_INFO
)
if message_with_file_download:
# Save an uploaded file locally
file = turn_context.activity.attachments[0]
file_download = FileDownloadInfo.deserialize(file.content)
file_path = "files/" + file.name
response = requests.get(file_download.download_url, allow_redirects=True)
open(file_path, "wb").write(response.content)
reply = self._create_reply(
turn_context.activity, f"Complete downloading <b>{file.name}</b>", "xml"
)
await turn_context.send_activity(reply)
else:
# Attempt to upload a file to Teams. This will display a confirmation to
# the user (Accept/Decline card). If they accept, on_teams_file_consent_accept
# will be called, otherwise on_teams_file_consent_decline.
filename = "teams-logo.png"
file_path = "files/" + filename
file_size = os.path.getsize(file_path)
await self._send_file_card(turn_context, filename, file_size)
async def _send_file_card(
self, turn_context: TurnContext, filename: str, file_size: int
):
"""
Send a FileConsentCard to get permission from the user to upload a file.
"""
consent_context = {"filename": filename}
file_card = FileConsentCard(
description="This is the file I want to send you",
size_in_bytes=file_size,
accept_context=consent_context,
decline_context=consent_context
)
as_attachment = Attachment(
content=file_card.serialize(), content_type=ContentType.FILE_CONSENT_CARD, name=filename
)
reply_activity = self._create_reply(turn_context.activity)
reply_activity.attachments = [as_attachment]
await turn_context.send_activity(reply_activity)
async def on_teams_file_consent_accept(
self,
turn_context: TurnContext,
file_consent_card_response: FileConsentCardResponse
):
"""
The user accepted the file upload request. Do the actual upload now.
"""
file_path = "files/" + file_consent_card_response.context["filename"]
file_size = os.path.getsize(file_path)
headers = {
"Content-Length": f"\"{file_size}\"",
"Content-Range": f"bytes 0-{file_size-1}/{file_size}"
}
response = requests.put(
file_consent_card_response.upload_info.upload_url, open(file_path, "rb"), headers=headers
)
if response.status_code != 200:
print(f"Failed to upload, status {response.status_code}, file_path={file_path}")
await self._file_upload_failed(turn_context, "Unable to upload file.")
else:
await self._file_upload_complete(turn_context, file_consent_card_response)
async def on_teams_file_consent_decline(
self,
turn_context: TurnContext,
file_consent_card_response: FileConsentCardResponse
):
"""
The user declined the file upload.
"""
context = file_consent_card_response.context
reply = self._create_reply(
turn_context.activity,
f"Declined. We won't upload file <b>{context['filename']}</b>.",
"xml"
)
await turn_context.send_activity(reply)
async def _file_upload_complete(
self,
turn_context: TurnContext,
file_consent_card_response: FileConsentCardResponse
):
"""
The file was uploaded, so display a FileInfoCard so the user can view the
file in Teams.
"""
name = file_consent_card_response.upload_info.name
download_card = FileInfoCard(
unique_id=file_consent_card_response.upload_info.unique_id,
file_type=file_consent_card_response.upload_info.file_type
)
as_attachment = Attachment(
content=download_card.serialize(),
content_type=ContentType.FILE_INFO_CARD,
name=name,
content_url=file_consent_card_response.upload_info.content_url
)
reply = self._create_reply(
turn_context.activity,
f"<b>File uploaded.</b> Your file <b>{name}</b> is ready to download",
"xml"
)
reply.attachments = [as_attachment]
await turn_context.send_activity(reply)
async def _file_upload_failed(self, turn_context: TurnContext, error: str):
reply = self._create_reply(
turn_context.activity,
f"<b>File upload failed.</b> Error: <pre>{error}</pre>",
"xml"
)
await turn_context.send_activity(reply)
def _create_reply(self, activity, text=None, text_format=None):
return Activity(
type=ActivityTypes.message,
timestamp=datetime.utcnow(),
from_property=ChannelAccount(
id=activity.recipient.id, name=activity.recipient.name
),
recipient=ChannelAccount(
id=activity.from_property.id, name=activity.from_property.name
),
reply_to_id=activity.id,
service_url=activity.service_url,
channel_id=activity.channel_id,
conversation=ConversationAccount(
is_group=activity.conversation.is_group,
id=activity.conversation.id,
name=activity.conversation.name,
),
text=text or "",
text_format=text_format or None,
locale=activity.locale,
)
The reason you're getting the 500 is because continue_conversation only takes 3 parameters, and you're passing in 4. The BOT._send_file_card(TurnContext, filename, fileSize) is the problem.
For the code below I added a hello.txt to the project just so I had an easy file to work with.
If this is your bot file:
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
from datetime import datetime
from typing import Dict
from botbuilder.core import ActivityHandler, TurnContext
from botbuilder.schema import ChannelAccount, ConversationAccount, ConversationReference, Activity, Attachment, ActivityTypes
from botbuilder.schema.teams import (
FileDownloadInfo,
FileConsentCard,
FileConsentCardResponse,
FileInfoCard,
)
from botbuilder.schema.teams.additional_properties import ContentType
class ProactiveBot(ActivityHandler):
def __init__(self, conversation_references: Dict[str, ConversationReference]):
self.conversation_references = conversation_references
async def on_conversation_update_activity(self, turn_context: TurnContext):
self._add_conversation_reference(turn_context.activity)
return await super().on_conversation_update_activity(turn_context)
async def on_members_added_activity(
self, members_added: [ChannelAccount], turn_context: TurnContext
):
for member in members_added:
if member.id != turn_context.activity.recipient.id:
await turn_context.send_activity(
"Welcome to the Proactive Bot sample. Navigate to "
"http://localhost:3978/api/notify to proactively message everyone "
"who has previously messaged this bot."
)
async def on_message_activity(self, turn_context: TurnContext):
self._add_conversation_reference(turn_context.activity)
return await turn_context.send_activity(
f"You sent: {turn_context.activity.text}"
)
def _add_conversation_reference(self, activity: Activity):
"""
This populates the shared Dictionary that holds conversation references. In this sample,
this dictionary is used to send a message to members when /api/notify is hit.
:param activity:
:return:
"""
conversation_reference = TurnContext.get_conversation_reference(activity)
self.conversation_references[
conversation_reference.user.id
] = conversation_reference
async def _send_file_card(
self, turn_context: TurnContext, filename: str, file_size: int
):
"""
Send a FileConsentCard to get permission from the user to upload a file.
"""
consent_context = {"filename": filename}
file_card = FileConsentCard(
description="This is the file I want to send you",
size_in_bytes=file_size,
accept_context=consent_context,
decline_context=consent_context
)
as_attachment = Attachment(
content=file_card.serialize(), content_type=ContentType.FILE_CONSENT_CARD, name=filename
)
reply_activity = self._create_reply(turn_context.activity)
reply_activity.attachments = [as_attachment]
await turn_context.send_activity(reply_activity)
def _create_reply(self, activity, text=None, text_format=None):
return Activity(
type=ActivityTypes.message,
timestamp=datetime.utcnow(),
from_property=ChannelAccount(
id=activity.recipient.id, name=activity.recipient.name
),
recipient=ChannelAccount(
id=activity.from_property.id, name=activity.from_property.name
),
reply_to_id=activity.id,
service_url=activity.service_url,
channel_id=activity.channel_id,
conversation=ConversationAccount(
is_group=activity.conversation.is_group,
id=activity.conversation.id,
name=activity.conversation.name,
),
text=text or "",
text_format=text_format or None,
locale=activity.locale,
)
and your app.py is this:
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import os
import sys
import traceback
import uuid
from datetime import datetime
from http import HTTPStatus
from typing import Dict
from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
BotFrameworkAdapterSettings,
TurnContext,
BotFrameworkAdapter,
MessageFactory,
)
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.schema import Activity, ActivityTypes, ConversationReference
from bots import ProactiveBot
from config import DefaultConfig
CONFIG = DefaultConfig()
# Create adapter.
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
SETTINGS = BotFrameworkAdapterSettings(CONFIG.APP_ID, CONFIG.APP_PASSWORD)
ADAPTER = BotFrameworkAdapter(SETTINGS)
# Catch-all for errors.
async def on_error(context: TurnContext, error: Exception):
# This check writes out errors to console log .vs. app insights.
# NOTE: In production environment, you should consider logging this to Azure
# application insights.
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
traceback.print_exc()
# Send a message to the user
await context.send_activity("The bot encountered an error or bug.")
await context.send_activity(
"To continue to run this bot, please fix the bot source code."
)
# Send a trace activity if we're talking to the Bot Framework Emulator
if context.activity.channel_id == "emulator":
# Create a trace activity that contains the error object
trace_activity = Activity(
label="TurnError",
name="on_turn_error Trace",
timestamp=datetime.utcnow(),
type=ActivityTypes.trace,
value=f"{error}",
value_type="https://www.botframework.com/schemas/error",
)
# Send a trace activity, which will be displayed in Bot Framework Emulator
await context.send_activity(trace_activity)
ADAPTER.on_turn_error = on_error
# Create a shared dictionary. The Bot will add conversation references when users
# join the conversation and send messages.
CONVERSATION_REFERENCES: Dict[str, ConversationReference] = dict()
# If the channel is the Emulator, and authentication is not in use, the AppId will be null.
# We generate a random AppId for this case only. This is not required for production, since
# the AppId will have a value.
APP_ID = SETTINGS.app_id if SETTINGS.app_id else uuid.uuid4()
# Create the Bot
BOT = ProactiveBot(CONVERSATION_REFERENCES)
# Listen for incoming requests on /api/messages.
async def messages(req: Request) -> Response:
# Main bot message handler.
if "application/json" in req.headers["Content-Type"]:
body = await req.json()
else:
return Response(status=HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
activity = Activity().deserialize(body)
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""
response = await ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
if response:
return json_response(data=response.body, status=response.status)
return Response(status=HTTPStatus.OK)
# Listen for requests on /api/notify, and send a messages to all conversation members.
async def notify(req: Request) -> Response: # pylint: disable=unused-argument
await _send_proactive_message()
return Response(status=HTTPStatus.OK, text="Proactive messages have been sent")
# Send a message to all conversation members.
# This uses the shared Dictionary that the Bot adds conversation references to.
async def _send_proactive_message():
filename = "hello.txt"
file_path = filename
file_size = os.path.getsize(file_path)
for conversation_reference in CONVERSATION_REFERENCES.values():
await ADAPTER.continue_conversation(
conversation_reference,
lambda turn_context: BOT._send_file_card(turn_context, filename, file_size),
APP_ID,
)
APP = web.Application(middlewares=[aiohttp_error_middleware])
APP.router.add_post("/api/messages", messages)
APP.router.add_get("/api/notify", notify)
if __name__ == "__main__":
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
then it works assuming you're starting from the base proactive_bot sample.
Thanks, @Virtual-Josh you're a hero!