|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https:#www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# [START add_ons_preview_link] |
| 15 | + |
| 16 | +from typing import Any, Mapping |
| 17 | +from urllib.parse import urlparse |
| 18 | + |
| 19 | +import flask |
| 20 | +import functions_framework |
| 21 | + |
| 22 | + |
| 23 | +@functions_framework.http |
| 24 | +def create_link_preview(req: flask.Request): |
| 25 | + """Responds to any HTTP request. |
| 26 | + Args: |
| 27 | + req: HTTP request context. |
| 28 | + Returns: |
| 29 | + The response object. |
| 30 | + """ |
| 31 | + event = req.get_json(silent=True) |
| 32 | + if event["docs"]["matchedUrl"]["url"]: |
| 33 | + return create_card(event["docs"]["matchedUrl"]["url"]) |
| 34 | + |
| 35 | + |
| 36 | +def create_card(url): |
| 37 | + """Creates a preview link card for either a case link or people link. |
| 38 | + Args: |
| 39 | + url: The matched url. |
| 40 | + Returns: |
| 41 | + A case link preview card or a people link preview card. |
| 42 | + """ |
| 43 | + parsed_url = urlparse(url) |
| 44 | + if parsed_url.hostname != "www.example.com": |
| 45 | + return {} |
| 46 | + |
| 47 | + if parsed_url.path.startswith("/support/cases/"): |
| 48 | + return case_link_preview(url) |
| 49 | + |
| 50 | + if parsed_url.path.startswith("/people/"): |
| 51 | + return people_link_preview() |
| 52 | + |
| 53 | + return {} |
| 54 | + |
| 55 | + |
| 56 | +# [START add_ons_case_preview_link] |
| 57 | + |
| 58 | + |
| 59 | +def case_link_preview(url): |
| 60 | + """A support case link preview. |
| 61 | + Args: |
| 62 | + url: The case link. |
| 63 | + Returns: |
| 64 | + A case link preview card. |
| 65 | + """ |
| 66 | + |
| 67 | + # Parses the URL to identify the case ID. |
| 68 | + segments = url.split("/") |
| 69 | + case_id = segments[-1] |
| 70 | + |
| 71 | + # Returns the card. |
| 72 | + # Uses the text from the card's header for the title of the smart chip. |
| 73 | + return { |
| 74 | + "header": {"title": f"Case {case_id}: Title bar is broken."}, |
| 75 | + "sections": [ |
| 76 | + { |
| 77 | + "widgets": [ |
| 78 | + { |
| 79 | + "textParagraph": { |
| 80 | + "text": "Customer can't view title on mobile device." |
| 81 | + } |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | +# [END add_ons_case_preview_link] |
| 90 | +# [START add_ons_people_preview_link] |
| 91 | + |
| 92 | + |
| 93 | +def people_link_preview(): |
| 94 | + """An employee profile link preview. |
| 95 | + Returns: |
| 96 | + A people link preview card. |
| 97 | + """ |
| 98 | + |
| 99 | + # Builds a preview card with an employee's name, title, email, and profile photo. |
| 100 | + # Returns the card. Uses the text from the card's header for the title of the smart chip. |
| 101 | + return { |
| 102 | + "header": {"title": "Rosario Cruz"}, |
| 103 | + "sections": [ |
| 104 | + { |
| 105 | + "widgets": [ |
| 106 | + { |
| 107 | + "image": { |
| 108 | + "imageUrl": "https:#developers.google.com/workspace/add-ons/images/employee-profile.png" |
| 109 | + } |
| 110 | + }, |
| 111 | + { |
| 112 | + "keyValue": { |
| 113 | + "icon": "EMAIL", |
| 114 | + "content": "rosario@example.com", |
| 115 | + "bottomLabel": "Case Manager", |
| 116 | + } |
| 117 | + }, |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | +# [END add_ons_people_preview_link] |
| 125 | +# [END add_ons_preview_link] |
0 commit comments