콘텐츠로 바로가기
티스토리 수익 글 보기
티스토리 수익 글 보기
티스토리 수익 글 보기
add-ons-samples/python/chat/contact-form-app/main.py at main · googleworkspace/add-ons-samples · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
googleworkspace
add-ons-samples
Repository navigation
Code
Issues
12
(12)
Pull requests
100
(100)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
add-ons-samples
/
python
/
chat
/
contact-form-app
/
main.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
193 lines (166 loc) · 6.64 KB
main
Breadcrumbs
add-ons-samples
/
python
/
chat
/
contact-form-app
/
main.py
Copy path
Top
File metadata and controls
Code
Blame
193 lines (166 loc) · 6.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Google Chat app with dialog card interactions in Python App Engine
"""
from
typing
import
Any
,
Mapping
from
datetime
import
datetime
from
flask
import
Flask
,
request
,
json
app
=
Flask
(
__name__
)
FUNCTION_URL
=
"your-function-url"
@
app
.
route
(
'/'
,
methods
=
[
'POST'
])
def
post
()
->
Mapping
[
str
,
Any
]:
"""Handle requests from Google Chat
Returns:
Mapping[str, Any]: the response
"""
# Stores the Google Chat event
chat_event
=
request
.
get_json
().
get
(
'chat'
)
# Handle user interaction with multiselect.
if
chat_event
.
get
(
'messagePayload'
)
is
not
None
:
return
json
.
jsonify
(
handle_message
())
elif
chat_event
.
get
(
'buttonClickedPayload'
)
is
not
None
:
return
json
.
jsonify
(
handle_button_clicked
(
request
.
get_json
()))
# [START subsequent_steps]
def
handle_message
()
->
Mapping
[
str
,
Any
]:
"""Responds to a message in Google Chat.
Returns:
Mapping[str, Any]: the response that handles dialogs.
"""
return
{
'hostAppDataAction'
: {
'chatDataAction'
: {
'createMessageAction'
: {
'message'
: {
'text'
:
"To add a contact, use the `ADD CONTACT` button below."
,
'accessoryWidgets'
: [
# [START open_dialog_from_button]
{
'buttonList'
: {
'buttons'
: [{
'text'
:
"ADD CONTACT"
,
'onClick'
: {
'action'
: {
'function'
:
FUNCTION_URL
,
'interaction'
:
"OPEN_DIALOG"
,
'parameters'
: [
{
'key'
:
"actionName"
,
'value'
:
"openInitialDialog"
}
]
}}
}]}}
# [END open_dialog_from_button]
]
}}}}}
def
handle_button_clicked
(
event
:
Mapping
[
str
,
Any
])
->
Mapping
[
str
,
Any
]:
"""Responds to a button clicked in Google Chat.
Args:
Mapping[str, Any] event: the event object from the Google Workspace add-on
Returns:
Mapping[str, Any]: the response depending on the button clicked.
"""
# Initial dialog form page
if
"openInitialDialog"
==
event
[
'commonEventObject'
][
'parameters'
][
'actionName'
]:
return
open_initial_dialog
()
# Confirmation dialog form page
elif
"openConfirmationDialog"
==
event
[
'commonEventObject'
][
'parameters'
][
'actionName'
] :
return
open_confirmation_dialog
(
event
)
# Submission dialog form page
elif
"submitDialog"
==
event
[
'commonEventObject'
][
'parameters'
][
'actionName'
]:
return
submit_dialog
(
event
)
# [START open_initial_dialog]
def
open_initial_dialog
()
->
Mapping
[
str
,
Any
]:
"""Opens the initial step of the dialog that lets users add contact details.
Returns:
Mapping[str, Any]: open the dialog.
"""
return
{
'action'
: {
'navigations'
: [{
'pushCard'
: {
'sections'
: [{
'widgets'
: [
{
'textInput'
: {
'name'
:
"contactName"
,
'label'
:
"First and last name"
,
'type'
:
"SINGLE_LINE"
}},
{
'dateTimePicker'
: {
'name'
:
"contactBirthdate"
,
'label'
:
"Birthdate"
,
'type'
:
"DATE_ONLY"
}},
{
'selectionInput'
: {
'name'
:
"contactType"
,
'label'
:
"Contact type"
,
'type'
:
"RADIO_BUTTON"
,
'items'
: [
{
'text'
:
"Work"
,
'value'
:
"Work"
,
'selected'
:
False
},
{
'text'
:
"Personal"
,
'value'
:
"Personal"
,
'selected'
:
False
}
]
}},
{
'buttonList'
: {
'buttons'
: [{
'text'
:
"NEXT"
,
'onClick'
: {
'action'
: {
'function'
:
FUNCTION_URL
,
'parameters'
: [
{
'key'
:
"actionName"
,
'value'
:
"openConfirmationDialog"
}
]
}}
}]}}
]}]}}]}}
# [END open_initial_dialog]
def
open_confirmation_dialog
(
event
:
Mapping
[
str
,
Any
])
->
Mapping
[
str
,
Any
]:
"""Opens the second step of the dialog that lets users confirm details.
Args:
Mapping[str, Any] event: the event object from the Google Workspace add-on
Returns:
Mapping[str, Any]: update the dialog.
"""
name
=
event
.
get
(
'commonEventObject'
).
get
(
'formInputs'
)[
"contactName"
].
get
(
'stringInputs'
).
get
(
'value'
)[
0
]
birthdateEpoch
=
event
.
get
(
'commonEventObject'
).
get
(
'formInputs'
)[
"contactBirthdate"
].
get
(
'dateInput'
).
get
(
'msSinceEpoch'
)
birthdate
=
datetime
.
fromtimestamp
(
int
(
birthdateEpoch
)
/
1000.0
).
strftime
(
"%Y-%m-%d"
)
type
=
event
.
get
(
'commonEventObject'
).
get
(
'formInputs'
)[
"contactType"
].
get
(
'stringInputs'
).
get
(
'value'
)[
0
]
# Display the input values for confirmation
return
{
'action'
: {
'navigations'
: [{
'pushCard'
: {
'sections'
: [{
'widgets'
: [
{
'textParagraph'
: {
'text'
:
"Confirm contact information and submit:"
}},
{
'textParagraph'
: {
'text'
:
"<b>Name:</b> "
+
name
}},
{
'textParagraph'
: {
'text'
:
"<b>Birthday:</b> "
+
birthdate
}},
{
'textParagraph'
: {
'text'
:
"<b>Type:</b> "
+
type
}},
# [START set_parameters]
{
'buttonList'
: {
'buttons'
: [{
'text'
:
"SUBMIT"
,
'onClick'
: {
'action'
: {
'function'
:
FUNCTION_URL
,
'parameters'
: [
{
'key'
:
"actionName"
,
'value'
:
"submitDialog"
},
# Pass input values as parameters for last dialog step (submission)
{
'key'
:
"contactName"
,
'value'
:
name
},
{
'key'
:
"contactBirthdate"
,
'value'
:
birthdate
},
{
'key'
:
"contactType"
,
'value'
:
type
}
]
}}
}]}}
# [END set_parameters]
]}]}}]}}
# [END subsequent_steps]
def
submit_dialog
(
event
:
Mapping
[
str
,
Any
])
->
Mapping
[
str
,
Any
]:
"""Handles submission and closes the dialog.
Args:
Mapping[str, Any] event: the event object from the Google Workspace add-on
Returns:
Mapping[str, Any]: close the dialog with a status in text notification or message.
"""
# [START status_notification]
# Validate the parameters.
if
event
.
get
(
'commonEventObject'
).
get
(
'parameters'
)[
"contactName"
]
==
""
:
return
{
'action'
: {
'navigations'
: [{
'endNavigation'
: {
'action'
:
"CLOSE_DIALOG"
}}],
'notification'
: {
'text'
:
"Failure, the contact name was missing!"
}
}}
# [END status_notification]
# [START status_message]
return
{
'hostAppDataAction'
: {
'chatDataAction'
: {
'createMessageAction'
: {
'message'
: {
'text'
:
"✅ "
+
event
.
get
(
'commonEventObject'
).
get
(
'parameters'
)[
"contactName"
]
+
" has been added to your contacts."
}}}}}
# [END status_message]
if
__name__
==
'__main__'
:
# This is used when running locally. Gunicorn is used to run the
# application on Google App Engine. See entrypoint in app.yaml.
app
.
run
(
host
=
'127.0.0.1'
,
port
=
8080
,
debug
=
True
)
You can’t perform that action at this time.