From 2af32302b22a713a2d19419bbbe36da4fd4da5be Mon Sep 17 00:00:00 2001 From: virtual-josh Date: Tue, 28 Jan 2020 10:26:31 -0800 Subject: [PATCH 1/2] adding scenario for testing new helper --- scenarios/create-thread-in-channel/README.md | 30 ++++++ scenarios/create-thread-in-channel/app.py | 92 ++++++++++++++++++ .../create-thread-in-channel/bots/__init__.py | 6 ++ .../bots/create_thread_in_teams_bot.py | 24 +++++ scenarios/create-thread-in-channel/config.py | 13 +++ .../create-thread-in-channel/requirements.txt | 2 + .../teams_app_manifest/color.png | Bin 0 -> 1229 bytes .../teams_app_manifest/manifest.json | 43 ++++++++ .../teams_app_manifest/outline.png | Bin 0 -> 383 bytes 9 files changed, 210 insertions(+) create mode 100644 scenarios/create-thread-in-channel/README.md create mode 100644 scenarios/create-thread-in-channel/app.py create mode 100644 scenarios/create-thread-in-channel/bots/__init__.py create mode 100644 scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py create mode 100644 scenarios/create-thread-in-channel/config.py create mode 100644 scenarios/create-thread-in-channel/requirements.txt create mode 100644 scenarios/create-thread-in-channel/teams_app_manifest/color.png create mode 100644 scenarios/create-thread-in-channel/teams_app_manifest/manifest.json create mode 100644 scenarios/create-thread-in-channel/teams_app_manifest/outline.png diff --git a/scenarios/create-thread-in-channel/README.md b/scenarios/create-thread-in-channel/README.md new file mode 100644 index 000000000..40e84f525 --- /dev/null +++ b/scenarios/create-thread-in-channel/README.md @@ -0,0 +1,30 @@ +# EchoBot + +Bot Framework v4 echo bot sample. + +This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that accepts input from the user and echoes it back. + +## Running the sample +- Clone the repository +```bash +git clone https://github.com/Microsoft/botbuilder-python.git +``` +- Activate your desired virtual environment +- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder +- In the terminal, type `pip install -r requirements.txt` +- In the terminal, type `python app.py` + +## Testing the bot using Bot Framework Emulator +[Microsoft Bot Framework Emulator](https://github.com/microsoft/botframework-emulator) is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel. + +- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases) + +### Connect to bot using Bot Framework Emulator +- Launch Bot Framework Emulator +- Paste this URL in the emulator window - http://localhost:3978/api/messages + +## Further reading + +- [Bot Framework Documentation](https://docs.botframework.com) +- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0) +- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0) diff --git a/scenarios/create-thread-in-channel/app.py b/scenarios/create-thread-in-channel/app.py new file mode 100644 index 000000000..3c55decbe --- /dev/null +++ b/scenarios/create-thread-in-channel/app.py @@ -0,0 +1,92 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import asyncio +import sys +from datetime import datetime +from types import MethodType + +from flask import Flask, request, Response +from botbuilder.core import ( + BotFrameworkAdapterSettings, + TurnContext, + BotFrameworkAdapter, +) +from botbuilder.schema import Activity, ActivityTypes + +from bots import CreateThreadInTeamsBot + +# Create the loop and Flask app +LOOP = asyncio.get_event_loop() +APP = Flask(__name__, instance_relative_config=True) +APP.config.from_object("config.DefaultConfig") + +# Create adapter. +# See https://aka.ms/about-bot-adapter to learn more about how bots work. +SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"]) +ADAPTER = BotFrameworkAdapter(SETTINGS) + + +# Catch-all for errors. +async def on_error( # pylint: disable=unused-argument + self, 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 = MethodType(on_error, ADAPTER) + +# Create the Bot +BOT = CreateThreadInTeamsBot(APP.config["APP_ID"]) + +# Listen for incoming requests on /api/messages.s +@APP.route("/api/messages", methods=["POST"]) +def messages(): + # Main bot message handler. + 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 = LOOP.create_task( + ADAPTER.process_activity(activity, auth_header, BOT.on_turn) + ) + LOOP.run_until_complete(task) + return Response(status=201) + except Exception as exception: + raise exception + + +if __name__ == "__main__": + try: + APP.run(debug=False, port=APP.config["PORT"]) # nosec debug + except Exception as exception: + raise exception diff --git a/scenarios/create-thread-in-channel/bots/__init__.py b/scenarios/create-thread-in-channel/bots/__init__.py new file mode 100644 index 000000000..f5e8a121c --- /dev/null +++ b/scenarios/create-thread-in-channel/bots/__init__.py @@ -0,0 +1,6 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from .create_thread_in_teams_bot import CreateThreadInTeamsBot + +__all__ = ["CreateThreadInTeamsBot"] diff --git a/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py b/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py new file mode 100644 index 000000000..8b2f8fccc --- /dev/null +++ b/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py @@ -0,0 +1,24 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +from botbuilder.core import MessageFactory, TurnContext +from botbuilder.core.teams import ( + teams_get_channel_id, + TeamsActivityHandler, + TeamsInfo +) + + +class CreateThreadInTeamsBot(TeamsActivityHandler): + def __init__(self, id): + self.id = id + + async def on_message_activity(self, turn_context: TurnContext): + message = MessageFactory.text("first message") + channel_id = teams_get_channel_id(turn_context.activity) + result = await TeamsInfo.send_message_to_teams_channel(turn_context, message, channel_id) + + await turn_context.adapter.continue_conversation(result[0], self._continue_conversation_callback, self.id) + + async def _continue_conversation_callback(self, t): + await t.send_activity(MessageFactory.text("second message")) diff --git a/scenarios/create-thread-in-channel/config.py b/scenarios/create-thread-in-channel/config.py new file mode 100644 index 000000000..6b5116fba --- /dev/null +++ b/scenarios/create-thread-in-channel/config.py @@ -0,0 +1,13 @@ +#!/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", "") diff --git a/scenarios/create-thread-in-channel/requirements.txt b/scenarios/create-thread-in-channel/requirements.txt new file mode 100644 index 000000000..7e54b62ec --- /dev/null +++ b/scenarios/create-thread-in-channel/requirements.txt @@ -0,0 +1,2 @@ +botbuilder-core>=4.4.0b1 +flask>=1.0.3 diff --git a/scenarios/create-thread-in-channel/teams_app_manifest/color.png b/scenarios/create-thread-in-channel/teams_app_manifest/color.png new file mode 100644 index 0000000000000000000000000000000000000000..48a2de13303e1e8a25f76391f4a34c7c4700fd3d GIT binary patch literal 1229 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD1|%QND7OGojKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCe1|JzX3_D&pSWuFnWfl{x;g|9jrEYf8Vqrkk2Ba|%ol3OT){=#|7ID~|e{ zODQ{kU&ME#@`*-tm%Tukt_gFr+`F?$dx9wg-jad`^gsMn2_%Kh%WH91&SjKq5 zgkdI|!exdOVgw@>>=!Tjnk6q)zV*T8$FdgRFYC{kQ7``NOcl@R(_%_8e5e0E;>v0G zEM9kb)2itgOTSfH7M=b3-S61B?PiazMdwXZwrS)^5UUS#HQjaoua5h_{Gx*_Zz|XK z$tf0mZ&=tpf2!!Q)!A_l&o_$g*|JM$VZa~F^0{x1T{=QFu*x$`=V%~jUW=G`iqqp=lquB-`P{Qjw`=zEu3cMc_x7m2f#9m}uoFBMMQ^+%cOL)F_)N@JZ}Axoxi1y= zeebq`y==e!nl+?cK-PhOec!3%|IupShHrcjW8sSt)F1>NW*{ zW%ljk2)nk%-}+F&?gi=7^$L#VeX3@kp%f{n}fR z`}uZ>", + "packageName": "com.teams.sample.conversationUpdate", + "developer": { + "name": "MentionBot", + "websiteUrl": "https://www.microsoft.com", + "privacyUrl": "https://www.teams.com/privacy", + "termsOfUseUrl": "https://www.teams.com/termsofuser" + }, + "icons": { + "color": "color.png", + "outline": "outline.png" + }, + "name": { + "short": "MentionBot", + "full": "MentionBot" + }, + "description": { + "short": "MentionBot", + "full": "MentionBot" + }, + "accentColor": "#FFFFFF", + "bots": [ + { + "botId": "<>", + "scopes": [ + "groupchat", + "team", + "personal" + ], + "supportsFiles": false, + "isNotificationOnly": false + } + ], + "permissions": [ + "identity", + "messageTeamMembers" + ], + "validDomains": [] +} \ No newline at end of file diff --git a/scenarios/create-thread-in-channel/teams_app_manifest/outline.png b/scenarios/create-thread-in-channel/teams_app_manifest/outline.png new file mode 100644 index 0000000000000000000000000000000000000000..dbfa9277299d36542af02499e06e3340bc538fe7 GIT binary patch literal 383 zcmV-_0f7FAP)Px$IY~r8R5%gMlrc`jP!L3IloKFoq~sFFH5|cdklX=R08T)}71BhaN8$`AsNf0_ zq>WNhAtCd|-nBlTU=y5zl_vXlXZ~bkuaYENMp>3QSQ_#zuYZ+eQh*OIHRxP~s(}ic zN2J4$u=AQcPt)|>F3zZLsjtP;Tajkugx;NcYED2~JVBlVO>{`uAY?Q4O|AA z=16}CJieK^5P_TKnou!zGR`$!PUC)DqtkO;?!`p!+9v3lP_mu=%Vt3BkoWsq%;FN1sp58w*zfr-z^7tIb*q>!yncCjrzLuOk3N+d&~^Cxd| z Date: Tue, 28 Jan 2020 10:39:07 -0800 Subject: [PATCH 2/2] updating name for snake case --- .../bots/create_thread_in_teams_bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py b/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py index 8b2f8fccc..6feca9af4 100644 --- a/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py +++ b/scenarios/create-thread-in-channel/bots/create_thread_in_teams_bot.py @@ -20,5 +20,5 @@ async def on_message_activity(self, turn_context: TurnContext): await turn_context.adapter.continue_conversation(result[0], self._continue_conversation_callback, self.id) - async def _continue_conversation_callback(self, t): - await t.send_activity(MessageFactory.text("second message")) + async def _continue_conversation_callback(self, turn_context): + await turn_context.send_activity(MessageFactory.text("second message"))