8000 adding conversation update scenario · yosshy/botbuilder-python@21876ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 21876ca

Browse files
committed
adding conversation update scenario
1 parent 140372d commit 21876ca

File tree

21 files changed

+536
-202
lines changed

21 files changed

+536
-202
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# coding=utf-8
2+
# --------------------------------------------------------------------------
3+
# Copyright (c) Microsoft Corporation. All rights reserved.
4+
# Licensed under the MIT License. See License.txt in the project root for
5+
# license information.
6+
# --------------------------------------------------------------------------
7+
8+
from .teams_activity_handler import TeamsActivityHandler
9+
10+
__all__ = ["TeamsActivityHandler"]

libraries/botbuilder-core/botbuilder/core/Teams/teams_activity_handler.py

Lines changed: 71 additions & 156 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class TeamInfo:
2+
def __init__(self, id = "", name = ""):
3+
self.id = id
4+
self.name = name
5+
6+
@property
7+
def id():
8+
return self.id
9+
10+
@property
11+
def name():
12+
return self.name
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# EchoBot
2+
3+
Bot Framework v4 echo bot sample.
4+
5+
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.
6+
7+
## Running the sample
8+
- Clone the repository
9+
```bash
10+
git clone https://github.com/Microsoft/botbuilder-python.git
11+
```
12+
- Activate your desired virtual environment
13+
- Bring up a terminal, navigate to `botbuilder-python\samples\02.echo-bot` folder
14+
- In the terminal, type `pip install -r requirements.txt`
15+
- In the terminal, type `python app.py`
16+
17+
## Testing the bot using Bot Framework Emulator
18+
[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.
19+
20+
- Install the Bot Framework emulator from [here](https://github.com/Microsoft/BotFramework-Emulator/releases)
21+
22+
### Connect to bot using Bot Framework Emulator
23+
- Launch Bot Framework Emulator
24+
- Paste this URL in the emulator window - http://localhost:3978/api/messages
25+
26+
## Further reading
27+
28+
- [Bot Framework Documentation](https://docs.botframework.com)
29+
- [Bot Basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
30+
- [Activity processing](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import asyncio
5+
import sys
6+
from datetime import datetime
7+
from types import MethodType
8+
9+
from flask import Flask, request, Response
10+
from botbuilder.core import BotFrameworkAdapterSettings, TurnContext, BotFrameworkAdapter
11+
from botbuilder.schema import Activity, ActivityTypes
12+
13+
from bots import ConversationUpdateBot
14+
15+
# Create the loop and Flask app
16+
LOOP = asyncio.get_event_loop()
17+
APP = Flask(__name__, instance_relative_config=True)
18+
APP.config.from_object("config.DefaultConfig")
19+
20+
# Create adapter.
21+
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
22+
SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"])
23+
ADAPTER = BotFrameworkAdapter(SETTINGS)
24+
25+
26+
# Catch-all for errors.
27+
async def on_error(self, context: TurnContext, error: Exception):
28+
# This check writes out errors to console log .vs. app insights.
29+
# NOTE: In production environment, you should consider logging this to Azure
30+
# application insights.
31+
print(f"\n [on_turn_error] unhandled error: {error}", file=sys.stderr)
32+
33+
# Send a message to the user
34+
await context.send_activity("The bot encountered an error or bug.")
35+
await context.send_activity("To continue to run this bot, please fix the bot source code.")
36+
# Send a trace activity if we're talking to the Bot Framework Emulator
37+
if context.activity.channel_id == 'emulator':
38+
# Create a trace activity that contains the error object
39+
trace_activity = Activity(
40+
label="TurnError",
41+
name="on_turn_error Trace",
42+
timestamp=datetime.utcnow(),
43+
type=ActivityTypes.trace,
44+
value=f"{error}",
45+
value_type="https://www.botframework.com/schemas/error"
46+
)
47+
# Send a trace activity, which will be displayed in Bot Framework Emulator
48+
await context.send_activity(trace_activity)
49+
50+
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
51+
52+
# Create the Bot
53+
BOT = ConversationUpdateBot()
54+
55+
# Listen for incoming requests on /api/messages.s
56+
@APP.route("/api/messages", methods=["POST"])
57+
def messages():
58+
# Main bot message handler.
59+
if "application/json" in request.headers["Content-Type"]:
60+
body = request.json
61+
else:
62+
return Response(status=415)
63+
64+
activity = Activity().deserialize(body)
65+
auth_header = (
66+
request.headers["Authorization"] if "Authorization" in request.headers else ""
67+
)
68+
69+
try:
70+
task = LOOP.create_task(
71+
ADAPTER.process_activity(activity, auth_header, BOT.on_turn)
72+
)
73+
LOOP.run_until_complete(task)
74+
return Response(status=201)
75+
except Exception as exception:
76+
raise exception
77+
78+
79+
if __name__ == "__main__":
80+
try:
81+
APP.run(debug=False, port=APP.config["PORT"]) # nosec debug
82+
except Exception as exception:
83+
raise exception
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from .conversation_update_bot import ConversationUpdateBot
5+
6+
__all__ = ["ConversationUpdateBot"]
Lines changed: 36 additions & 0 deletions
9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from botbuilder.core import ActivityHandler, MessageFactory, TurnContext, CardFactory
5+
from botbuilder.core.teams import TeamsActivityHandler
6+
from botbuilder.schema import ChannelAccount
7+
from botbuilder.schema.teams import ChannelInfo, TeamInfo, TeamsChannelAccount
8+
+
10+
class ConversationUpdateBot(TeamsActivityHandler):
11+
async def on_teams_channel_created_activity(self, channel_info: ChannelInfo, teamInfo: TeamInfo, turn_context: TurnContext):
12+
return await turn_context.send_activity(MessageFactory.text(f"The new channel is {channel_info.name}. The channel id is {channel_info.id}"))
13+
14+
async def on_teams_channel_deleted_activity(self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext):
15+
return await turn_context.send_activity(MessageFactory.text(f"The deleted channel is {channel_info.name}"))
16+
17+
async def on_teams_channel_renamed_activity(self, channel_info: ChannelInfo, team_info: TeamInfo, turn_context: TurnContext):
18+
return await turn_context.send_activity(MessageFactory.text(f"The new channel name is {channel_info.name}"))
19+
20+
async def on_teams_team_renamed_activity(self, team_info: TeamInfo, turn_context: TurnContext):
21+
return await turn_context.send_activity(MessageFactory.text(f"The new team name is {team_info.name}"))
22+
23+
async def on_teams_members_added_activity(self, teams_members_added: [TeamsChannelAccount], turn_context: TurnContext):
24+
for member in teams_members_added:
25+
await turn_context.send_activity(MessageFactory.text(f"Welcome your new team member {member.id}"))
26+
return
27+
28+
async def on_teams_members_removed_activity(self, teams_members_removed: [TeamsChannelAccount], turn_context: TurnContext):
29+
for member in teams_members_removed:
30+
await turn_context.send_activity(MessageFactory.text(f"Say goodbye to your team member {member.id}"))
31+
return
32+
33+
34+
35+
36+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License.
4+
5+
import os
6+
7+
""" Bot Configuration """
8+
9+
10+
class DefaultConfig:
11+
""" Bot Configuration """
12+
13+
PORT = 3978
14+
APP_ID = os.environ.get("MicrosoftAppId", "5396829c-e0a1-4698-9746-848ca0ba2892")
15+
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "x@YOwxU3rqAPzc.nxdRx?Zc.Z96OiHt4")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
botbuilder-core>=4.4.0b1
2+
flask>=1.0.3
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json",
3+
"manifestVersion": "1.5",
4+
"version": "1.0.0",
5+
"id": "",
6+
"packageName": "com.teams.sample.conversationUpdate",
7+
"developer": {
8+
"name": "ConversationUpdatesBot",
9+
"websiteUrl": "https://www.microsoft.com",
10+
"privacyUrl": "https://www.teams.com/privacy",
11+
"termsOfUseUrl": "https://www.teams.com/termsofuser"
12+
},
13+
"icons": {
14+
"color": "color.png",
15+
"outline": "outline.png"
16+
},
17+
"name": {
18+
"short": "ConversationUpdatesBot",
19+
"full": "ConversationUpdatesBot"
20+
},
21+
"description": {
22+
"short": "ConversationUpdatesBot",
23+
"full": "ConversationUpdatesBot"
24+
},
25+
"accentColor": "#FFFFFF",
26+
"bots": [
27+
{
28+
"botId": "",
29+
"scopes": [
30+
"groupchat",
31+
"team",
32+
"personal"
33+
],
34+
"supportsFiles": false,
35+
"isNotificationOnly": false
36+
}
37+
],
38+
"permissions": [
39+
"identity",
40+
"messageTeamMembers"
41+
],
42+
"validDomains": []
43+
}
Loading
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .team_info import TeamInfo
2+
from .notification_info import NotificationInfo
3+
from .tenant_info import TenantInfo
4+
from .channel_info import ChannelInfo
5+
from .teams_channel_data import TeamsChannelData
6+
from .teams_channel_account import TeamsChannelAccount
7+
8+
__all__ = [
9+
"TeamInfo",
10+
"ChannelInfo",
11+
"TeamsChannelData",
12+
"TeamsChannelData",
13+
"TenantInfo",
14+
"NotificationInfo"
15+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License. See License.txt in the project root for
3+
# license information.
4+
#
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is
7+
# regenerated.
8+
9+
class ChannelInfo:
10+
def __init__(self, id = "", name = ""):
11+
self._id = id
12+
self._name = name
13+
14+
@property
15+
def id(self):
16+
return self._id
17+
18+
@id.setter
19+
def id(self, id):
20+
self._id = id
21+
22+
@property
23+
def name(self):
24+
return self._name
25+
26+
@name.setter
27+
def name(self, name):
28+
self._name = name
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License. See License.txt in the project root for
3+
# license information.
4+
#
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is
7+
# regenerated.
8+
9+
class NotificationInfo():
10+
def __init__(self, alert: bool = False):
11+
self._alert = alert
12+
13+
@property
14+
def alert():
15+
return self._alert
16+
17+
@alert.setter
18+
def alert(alert):
19+
self._alert = alert
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License. See License.txt in the project root for
3+
# license information.
4+
#
5+
# Code generated by Microsoft (R) AutoRest Code Generator.
6+
# Changes may cause incorrect behavior and will be lost if the code is
7+
# regenerated.
8+
9+
class TeamInfo:
10+
def __init__(self, id = "", name = "", aadGroupId = ""):
11+
self._id = id
12+
self._name = name
13+
self._aad_group_id = aadGroupId
14+
15+
def get_id(self):
16+
return self._id
17+
18+
def set_id(self, id):
19+
self._id = id
20+
21+
def get_name(self):
22+
return self._name
23+
24+
def set_name(self, name):
25+
self._name = name
26+
27+
def get_aad_group_id(self):
28+
return self._aad_group_id
29+
30+
def set_aad_group_id(self, aad_group_id):
31+
self._aad_group_id= aad_group_id
32+
33+
id = property(get_id, set_id)
34+
name = property(get_name, set_name)
35+
aad_group_id = property(get_aad_group_id, set_aad_group_id)
36+

0 commit comments

Comments
 (0)
0