1
+ # Copyright (c) Microsoft Corporation. All rights reserved.
2
+ # Licensed under the MIT License.
3
+
4
+ from botbuilder .core import CardFactory , MessageFactory , TurnContext
5
+ from botbuilder .schema import ChannelAccount , ThumbnailCard , CardImage , HeroCard , Attachment , CardAction
6
+ from botbuilder .schema .teams import AppBasedLinkQuery , MessagingExtensionAttachment , MessagingExtensionQuery , MessagingExtensionResult , MessagingExtensionResponse
7
+ from botbuilder .core .teams import TeamsActivityHandler , TeamsInfo
8
+
9
+ from typing import List
10
+ import requests
11
+
12
+ class SearchBasedMessagingExtension (TeamsActivityHandler ):
13
+ async def on_message_activity (self , turn_context : TurnContext ):
14
+ await turn_context .send_activities (MessageFactory .text (f"Echo: { turn_context .activi
10000
ty .text } " ))
15
+
16
+ async def on_teams_messaging_extension_query (self , turn_context : TurnContext , query : MessagingExtensionQuery ):
17
+ search_query = str (query .parameters [0 ].value )
18
+ response = requests .get (f"http://registry.npmjs.com/-/v1/search" ,params = {"text" :search_query })
19
+ data = response .json ()
20
+
21
+ attachments = []
22
+
23
+ for obj in data ["objects" ]:
24
+ hero_card = HeroCard (
25
+ title = obj ["package" ]["name" ],
26
+ tap = CardAction (
27
+ type = "invoke" ,
28
+ value = obj ["package" ]
29
+ ),
30
+ preview = [CardImage (url = obj ["package" ]["links" ]["npm" ])]
31
+ )
32
+
33
+ attachment = MessagingExtensionAttachment (
34
+ content_type = CardFactory .content_types .hero_card ,
35
+ content = HeroCard (title = obj ["package" ]["name" ]),
36
+ preview = CardFactory .hero_card (hero_card )
37
+ )
38
+ attachments .append (attachment )
39
+ return MessagingExtensionResponse (
40
+ compose_extension = MessagingExtensionResult (
41
+ type = "result" ,
42
+ attachment_layout = "list" ,
43
+ attachments = attachments
44
+ )
45
+ )
46
+
47
+
48
+
49
+ async def on_teams_messaging_extension_select_item (self , turn_context : TurnContext , query ) -> MessagingExtensionResponse :
50
+ hero_card = HeroCard (
51
+ title = query ["name" ],
52
+ subtitle = query ["description" ],
53
+ buttons = [
54
+ CardAction (
55
+ type = "openUrl" ,
56
+ value = query ["links" ]["npm" ]
57
+ )
58
+ ]
59
+ )
60
+ attachment = MessagingExtensionAttachment (
61
+ content_type = CardFactory .content_types .hero_card ,
62
+ content = hero_card
63
+ )
64
+
65
+ return MessagingExtensionResponse (
66
+ compose_extension = MessagingExtensionResult (
67
+ type = "result" ,
68
+ attachment_layout = "list" ,
69
+ attachments = [attachment ]
70
+ )
71
+ )
72
+
73
+ def _create_messaging_extension_result (self , attachments : List [MessagingExtensionAttachment ]) -> MessagingExtensionResult :
74
+ return MessagingExtensionResult (
75
+ type = "result" ,
76
+ attachment_layout = "list" ,
77
+ attachments = attachments
78
+ )
79
+
80
+ def _create_search_result_attachment (self , search_query : str ) -> MessagingExtensionAttachment :
81
+ card_text = f"You said { search_query } "
82
+ bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU"
83
+
84
+ button = CardAction (
85
+ type = "openUrl" ,
86
+ title = "Click for more Information" ,
87
+ value = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview"
88
+ )
89
+
90
+ images = [CardImage (url = bf_logo )]
91
+ buttons = [button ]
92
+
93
+ hero_card = HeroCard (
94
+ title = "You searched for:" ,
95
+ text = card_text ,
96
+ images = images ,
97
+ buttons = buttons
98
+ )
99
+
100
+ return MessagingExtensionAttachment (
101
+ content_type = CardFactory .content_types .hero_card ,
102
+ content = hero_card ,
103
+ preview = CardFactory .hero_card (hero_card )
104
+ )
105
+
106
+ def _create_dummy_search_result_attachment (self ) -> MessagingExtensionAttachment :
107
+ card_text = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview"
108
+ bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU"
109
+
110
+ button = CardAction (
111
+ type = "openUrl" ,
112
+ title = "Click for more Information" ,
113
+ value = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview"
114
+ )
115
+
116
+ images = [CardImage (url = bf_logo )]
117
+
118
+ buttons = [button ]
119
+
120
+
121
+ hero_card = HeroCard (
122
+ title = "Learn more about Teams:" ,
123
+ text = card_text , images = images ,
124
+ buttons = buttons
125
+ )
126
+
127
+ preview = HeroCard (
128
+ title = "Learn more about Teams:" ,
129
+ text = card_text ,
130
+ images = images
131
+ )
132
+
133
+ return MessagingExtensionAttachment (
134
+ content_type = CardFactory .content_types .hero_card ,
135
+ content = hero_card ,
136
+ preview = CardFactory .hero_card (preview )
137
+ )
138
+
139
+ def _create_select_items_result_attachment (self , search_query : str ) -> MessagingExtensionAttachment :
140
+ card_text = f"You said { search_query } "
141
+ bf_logo = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtB3AwMUeNoq4gUBGe6Ocj8kyh3bXa9ZbV7u1fVKQoyKFHdkqU"
142
+
143
+ buttons = CardAction (
144
+ type = "openUrl" ,
145
+ title = "Click for more Information" ,
146
+ value = "https://docs.microsoft.com/en-us/microsoftteams/platform/concepts/bots/bots-overview"
147
+ )
148
+
149
+ images = [CardImage (url = bf_logo )]
150
+ buttons = [buttons ]
151
+
152
+ select_item_tap = CardAction (
153
+ type = "invoke" ,
154
+ value = {"query" : search_query }
155
+ )
156
+
157
+ hero_card = HeroCard (
158
+ title = "You searched for:" ,
159
+ text = card_text ,
160
+ images = images ,
161
+ buttons = buttons
162
+ )
163
+
164
+ preview = HeroCard (
165
+ title = card_text ,
166
+ text = card_text ,
167
+ images = images ,
168
+ tap = select_item_tap
169
+ )
170
+
171
+ return MessagingExtensionAttachment (
172
+ content_type = CardFactory .content_types .hero_card ,
173
+ content = hero_card ,
174
+ preview = CardFactory .hero_card (preview )
175
+ )
0 commit comments