Skip to content

[Teams] add task module fetch scenario #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions scenarios/task-module/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import sys
from datetime import datetime

from aiohttp import web
from aiohttp.web import Request, Response, json_response
from botbuilder.core import (
BotFrameworkAdapterSettings,
TurnContext,
BotFrameworkAdapter,
)
from botbuilder.schema import Activity, ActivityTypes
from bots import TaskModuleBot
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)

# 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 the Bot
BOT = TaskModuleBot()


# 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=415)

activity = Activity().deserialize(body)
auth_header = req.headers["Authorization"] if "Authorization" in req.headers else ""

try:
invoke_response = await ADAPTER.process_activity(
activity, auth_header, BOT.on_turn
)
if invoke_response:
return json_response(
data=invoke_response.body, status=invoke_response.status
)
return Response(status=201)
except PermissionError:
return Response(status=401)
except Exception:
return Response(status=500)


APP = web.Application()
APP.router.add_post("/api/messages", messages)

if __name__ == "__main__":
try:
web.run_app(APP, host="localhost", port=CONFIG.PORT)
except Exception as error:
raise error
6 changes: 6 additions & 0 deletions scenarios/task-module/bots/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

from .teams_task_module_bot import TaskModuleBot

__all__ = ["TaskModuleBot"]
90 changes: 90 additions & 0 deletions scenarios/task-module/bots/teams_task_module_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Copyright (c) Microsoft Corp. All rights reserved.
# Licensed under the MIT License.

import json
from typing import List
import random
from botbuilder.core import CardFactory, MessageFactory, TurnContext
from botbuilder.schema import (
ChannelAccount,
HeroCard,
CardAction,
CardImage,
Attachment,
)
from botbuilder.schema.teams import (
MessagingExtensionAction,
MessagingExtensionActionResponse,
MessagingExtensionAttachment,
MessagingExtensionResult,
TaskModuleResponse,
TaskModuleResponseBase,
TaskModuleContinueResponse,
TaskModuleMessageResponse,
TaskModuleTaskInfo,
TaskModuleRequest,
)
from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo
from botbuilder.azure import CosmosDbPartitionedStorage
from botbuilder.core.teams.teams_helper import deserializer_helper

class TaskModuleBot(TeamsActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
reply = MessageFactory.attachment(self._get_task_module_hero_card())
await turn_context.send_activity(reply)

def _get_task_module_hero_card(self) -> Attachment:
task_module_action = CardAction(
type="invoke",
title="Adaptive Card",
value={"type": "task/fetch", "data": "adaptivecard"},
)
card = HeroCard(
title="Task Module Invocation from Hero Card",
subtitle="This is a hero card with a Task Module Action button. Click the button to show an Adaptive Card within a Task Module.",
buttons=[task_module_action],
)
return CardFactory.hero_card(card)

async def on_teams_task_module_fetch(
self, turn_context: TurnContext, task_module_request: TaskModuleRequest
) -> TaskModuleResponse:
reply = MessageFactory.text(
f"OnTeamsTaskModuleFetchAsync TaskModuleRequest: {json.dumps(task_module_request.data)}"
)
await turn_context.send_activity(reply)

# base_response = TaskModuleResponseBase(type='continue')
card = CardFactory.adaptive_card(
{
"version": "1.0.0",
"type": "AdaptiveCard",
"body": [
{"type": "TextBlock", "text": "Enter Text Here",},
{
"type": "Input.Text",
"id": "usertext",
"placeholder": "add some text and submit",
"IsMultiline": "true",
},
],
"actions": [{"type": "Action.Submit", "title": "Submit",}],
}
)

task_info = TaskModuleTaskInfo(
card=card, title="Adaptive Card: Inputs", height=200, width=400
)
continue_response = TaskModuleContinueResponse(type="continue", value=task_info)
return TaskModuleResponse(task=continue_response)

async def on_teams_task_module_submit(
self, turn_context: TurnContext, task_module_request: TaskModuleRequest
) -> TaskModuleResponse:
reply = MessageFactory.text(
f"on_teams_messaging_extension_submit_action_activity MessagingExtensionAction: {json.dumps(task_module_request.data)}"
)
await turn_context.send_activity(reply)

message_response = TaskModuleMessageResponse(type="message", value="Thanks!")
return TaskModuleResponse(task=message_response)
15 changes: 15 additions & 0 deletions scenarios/task-module/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os


class DefaultConfig:
""" Bot Configuration """

PORT = 3978
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get(
"MicrosoftAppPassword", ""
)
2 changes: 2 additions & 0 deletions scenarios/task-module/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
botbuilder-core>=4.4.0b1
flask>=1.0.3
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions scenarios/task-module/teams_app_manifest/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json",
"manifestVersion": "1.5",
"version": "1.0.0",
"id": "<<YOUR-MICROSOFT-APP-ID>>",
"packageName": "com.microsoft.teams.samples",
"developer": {
"name": "Microsoft",
"websiteUrl": "https://example.azurewebsites.net",
"privacyUrl": "https://example.azurewebsites.net/privacy",
"termsOfUseUrl": "https://example.azurewebsites.net/termsofuse"
},
"icons": {
"color": "color.png",
"outline": "outline.png"
},
"name": {
"short": "Task Module",
"full": "Simple Task Module"
},
"description": {
"short": "Test Task Module Scenario",
"full": "Simple Task Module Scenario Test"
},
"accentColor": "#FFFFFF",
"bots": [
{
"botId": "<<YOUR-MICROSOFT-APP-ID>>",
"scopes": [
"personal",
"team",
"groupchat"
],
"supportsFiles": false,
"isNotificationOnly": false
}
],
"permissions": [
"identity",
"messageTeamMembers"
]
}