8000 pushing current version of teams conversation bot · guptarohan41/botbuilder-python@7942978 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7942978

Browse files
committed
pushing current version of teams conversation bot
1 parent 64a4ed9 commit 7942978

File tree

9 files changed

+191
-15
lines changed

9 files changed

+191
-15
lines changed

libraries/botbuilder-core/botbuilder/core/teams/teams_activity_extensions.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
from botbuilder.schema import Activity
22
from botbuilder.schema.teams import NotificationInfo, TeamsChannelData, TeamInfo
33

4-
5-
def dummy():
6-
return 1
7-
8-
94
def teams_get_channel_id(activity: Activity) -> str:
105
if not activity:
116
return None

libraries/botbuilder-schema/botbuilder/schema/_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ def __init__(self, **kwargs):
11101110
super(Mention, self).__init__(**kwargs)
11111111
self.mentioned = kwargs.get("mentioned", None)
11121112
self.text = kwargs.get("text", None)
1113-
self.type = kwargs.get("type", None)
1113+
self.type = kwargs.get("type", "mention")
11141114

11151115

11161116
class MessageReaction(Model):

libraries/botbuilder-schema/botbuilder/schema/_models_py3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ class Mention(Model):
13161316
}
13171317

13181318
def __init__(
1319-
self, *, mentioned=None, text: str = None, type: str = None, **kwargs
1319+
self, *, mentioned=None, text: str = None, type: str = "mention", **kwargs
13201320
) -> None:
13211321
super(Mention, self).__init__(**kwargs)
13221322
self.mentioned = mentioned

samples/57.teams-conversation-bot/app.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
import sys
66
import uuid
77
from datetime import datetime
8-
from typing import Dict
98

109
from flask import Flask, request, Response
1110
from botbuilder.core import (
1211
BotFrameworkAdapterSettings,
1312
TurnContext,
1413
BotFrameworkAdapter,
1514
)
16-
from botbuilder.schema import Activity, ActivityTypes, ConversationReference
15+
from botbuilder.schema import Activity, ActivityTypes
1716

18-
from bots import ProactiveBot
17+
from bots import TeamsConversationBot
1918

2019
# Create the loop and Flask app
2120
LOOP = asyncio.get_event_loop()
@@ -57,17 +56,13 @@ async def on_error(context: TurnContext, error: Exception):
5756

5857
ADAPTER.on_turn_error = on_error
5958

60-
# Create a shared dictionary. The Bot will add conversation references when users
61-
# join the conversation and send messages.
62-
CONVERSATION_REFERENCES: Dict[str, ConversationReference] = dict()
63-
6459
# If the channel is the Emulator, and authentication is not in use, the AppId will be null.
6560
# We generate a random AppId for this case only. This is not required for production, since
6661
# the AppId will have a value.
6762
APP_ID = SETTINGS.app_id if SETTINGS.app_id else uuid.uuid4()
6863

6964
# Create the Bot
70-
BOT = ProactiveBot(CONVERSATION_REFERENCES)
65+
BOT = TeamsConversationBot(APP.config["APP_ID"], APP.config["APP_PASSWORD"])
7166

7267
# Listen for incoming requests on /api/messages.
7368
@APP.route("/api/messages", methods=["POST"])
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .teams_conversation_bot import TeamsConversationBot
2+
3+
__all__ = ["TeamsConversationBot"]
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from botframework.connector.auth import MicrosoftAppCredentials
2+
from botbuilder.core import BotFrameworkAdapter, CardFactory, TurnContext, MessageFactory
3+
from botbuilder.core.teams import TeamsActivityHandler, TeamsInfo
4+
from botbuilder.core.teams.teams_activity_extensions import teams_get_channel_id
5+
from botbuilder.schema import CardAction, ConversationParameters, HeroCard, Mention
6+
from botbuilder.schema._connector_client_enums import ActionTypes
7+
8+
9+
class TeamsConversationBot(TeamsActivityHandler):
10+
def __init__(self, app_id: str, app_password: str):
11+
self._app_id = app_id
12+
self._app_password = app_password
13+
14+
async def on_message_activity(self, turn_context: TurnContext):
15+
TurnContext.remove_recipient_mention(turn_context.activity)
16+
turn_context.activity.text = turn_context.activity.text.strip()
17+
18+
if turn_context.activity.text == "MentionMe":
19+
await self._mention_activity(turn_context)
20+
return
21+
22+
if turn_context.activity.text == "UpdateCardAction":
23+
await self._update_card_activity(turn_context)
24+
return
25+
26+
if turn_context.activity.text == "Delete":
27+
await self._delete_card_activity(turn_context)
28+
return
29+
30+
card = HeroCard(
31+
title="Welcome Card",
32+
text="Click the buttons to update this card",
33+
buttons=[
34+
CardAction(
35+
type=ActionTypes.message_back,
36+
title="Update Card",
37+
text="UpdateCardAction",
38+
value={"count": 0}
39+
),
40+
CardAction(
41+
type=ActionTypes.message_back,
42+
title="Message all memebers",
43+
text="MessageAllMembers"
44+
)
45+
]
46+
)
47+
await turn_context.send_activity(MessageFactory.attachment(CardFactory.hero_card(card)))
48+
return
49+
50+
async def _mention_activity(self, turn_context: TurnContext):
51+
mention = Mention(
52+
mentioned=turn_context.activity.from_property,
53+
text=f"<at>{turn_context.activity.from_property.name}</at>",
54+
type="mention"
55+
)
56+
57+
reply_activity = MessageFactory.text(f"Hello {mention.text}")
58+
reply_activity.entities = [Mention().deserialize(mention.serialize())]
59+
await turn_context.send_activity(reply_activity)
60+
61+
async def _update_card_activity(self, turn_context: TurnContext):
62+
data = turn_context.activity.value
63+
data["count"] += 1
64+
65+
card = CardFactory.hero_card(HeroCard(
66+
title="Welcome Card",
67+
text=f"Updated count - {data['count']}",
68+
buttons=[
69+
CardAction(
70+
type=ActionTypes.message_back,
71+
title='Update Card',
72+
value=data,
73+
text='UpdateCardAction'
74+
),
75+
CardAction(
76+
type=ActionTypes.message_back,
77+
title='Message all members',
78+
text='MessageAllMembers'
79+
80+
),
81+
CardAction(
82+
type= ActionTypes.message_back,
83+
title='Delete card',
84+
text='Delete'
85+
)
86+
]
87+
)
88+
)
89+
90+
updated_activity = MessageFactory.attachment(card)
91+
updated_activity.id = turn_context.activity.reply_to_id
92+
await turn_context.update_activity(updated_activity)
93+
94+
async def _message_all_members(self, turn_context: TurnContext):
95+
teams_channel_id = teams_get_channel_id(turn_context.activity)
96+
service_url = turn_context.activity.service_url
97+
creds = MicrosoftAppCredentials(app_id=self._app_id, password=self._app_password)
98+
conversation_reference = None
99+
100+
team_members = await TeamsInfo.get_members(turn_context)
101+
102+
for member in team_members:
103+
proactive_message = MessageFactory.text(f"Hello {member.given_name} {member.surname}. I'm a Teams conversation bot.")
104+
105+
ref = TurnContext.get_conversation_reference(turn_context.activity)
106+
ref.user = member
107+
108+
async def get_ref(tc1: TurnContext):
109+
ref2 = TurnContext.get_conversation_reference(t1.activity)
110+
await BotFrameworkAdapter(t1.adapter).continue_conversation(ref2, lambda t2: await t2.send_activity(proactive_message))
111+
112+
await BotFrameworkAdapter(turn_context.adapter).create_conversation(ref, )
113+
114+
115+
116+
async def _delete_card_activity(self, turn_context: TurnContext):
117+
await turn_context.delete_activity(turn_context.activity.reply_to_id)
Loading
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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": "<<YOUR-MICROSOFT-BOT-ID>>",
6+
"packageName": "com.teams.sample.teamsconversationbot",
7+
"developer": {
8+
"name": "teamsConversationBot",
9+
"websiteUrl": "https://www.mic F438 rosoft.com",
10+
"privacyUrl": "https://www.teams.com/privacy",
11+
"termsOfUseUrl": "https://www.teams.com/termsofuser"
12+
},
13+
"icons": {
14+
"outline": "outline.png",
15+
"color": "color.png"
16+
},
17+
"name": {
18+
"short": "TeamsConversationBot",
19+
"full": "TeamsConversationBot"
20+
},
21+
"description": {
22+
"short": "TeamsConversationBot",
23+
"full": "TeamsConversationBot"
24+
},
25+
"accentColor": "#FFFFFF",
26+
"bots": [
27+
{
28+
"botId": "<<YOUR-MICROSOFT-BOT-ID>>",
29+
"scopes": [
30+
"personal",
31+
"groupchat",
32+
"team"
33+
],
34+
"supportsFiles": false,
35+
"isNotificationOnly": false,
36+
"commandLists": [
37+
{
38+
"scopes": [
39+
"personal",
40+
"groupchat",
41+
"team"
42+
],
43+
"commands": [
44+
{
45+
"title": "MentionMe",
46+
"description": "Sends message with @mention of the sender"
47+
},
48+
{
49+
"title": "Show Welcome",
50+
"description": "Shows the welcome card"
51+
},
52+
{
53+
"title": "MessageAllMembers",
54+
"description": "Send 1 to 1 message to all members of the current conversation"
55+
}
56+
]
57+
}
58+
]
59+
}
60+
],
61+
"permissions": [
62+
"identity",
63+
"messageTeamMembers"
64+
],
65+
"validDomains": []
66+
}
Loading

0 commit comments

Comments
 (0)
0