|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * GENERATED CODE WARNING |
| 20 | + * This file was automatically generated - do not edit! |
| 21 | + */ |
| 22 | + |
| 23 | +require_once __DIR__ . '/../../../vendor/autoload.php'; |
| 24 | + |
| 25 | +// [START chat_v1_generated_ChatService_SearchMessages_sync] |
| 26 | +use Google\ApiCore\ApiException; |
| 27 | +use Google\ApiCore\PagedListResponse; |
| 28 | +use Google\Apps\Chat\V1\Client\ChatServiceClient; |
| 29 | +use Google\Apps\Chat\V1\SearchMessageResult; |
| 30 | +use Google\Apps\Chat\V1\SearchMessagesRequest; |
| 31 | + |
| 32 | +/** |
| 33 | + * Searches for messages in Google Chat that the calling user has access to. |
| 34 | + * Returns a list of messages matching the search criteria. |
| 35 | + * |
| 36 | + * To search across all spaces the user has access to, set `parent` to |
| 37 | + * `spaces/-`. Using any other value for `parent` results in an |
| 38 | + * `INVALID_ARGUMENT` error. The returned messages have their `name` field |
| 39 | + * populated with the full resource name, which includes the specific `space` |
| 40 | + * in which the message resides. |
| 41 | + * |
| 42 | + * This API doesn't return all message types. The types of messages listed |
| 43 | + * below aren't included in the response. Use |
| 44 | + * [ListMessages][google.chat.v1.ChatService.ListMessages] to list all |
| 45 | + * messages. |
| 46 | + * |
| 47 | + * - Private Messages that are visible to the authenticated user. |
| 48 | + * - Messages posted by Chat apps in spaces or group chats. |
| 49 | + * - Messages in a Chat app DM. |
| 50 | + * - Messages from blocked users. |
| 51 | + * - Messages in spaces that the caller has muted. |
| 52 | + * |
| 53 | + * Requires [user |
| 54 | + * authentication](https://developers.google.com/workspace/chat/authenticate-authorize-chat-user) |
| 55 | + * with one of the following [authorization |
| 56 | + * scopes](https://developers.google.com/workspace/chat/authenticate-authorize#chat-api-scopes): |
| 57 | + * |
| 58 | + * - `https://www.googleapis.com/auth/chat.messages.readonly` |
| 59 | + * - `https://www.googleapis.com/auth/chat.messages` |
| 60 | + * |
| 61 | + * @param string $formattedParent The resource name of the space to search within. |
| 62 | + * |
| 63 | + * To search across all spaces the user has access to, set this field to |
| 64 | + * `spaces/-`. Using any other value for `parent` results in an |
| 65 | + * `INVALID_ARGUMENT` error. |
| 66 | + * |
| 67 | + * To limit the search to one or more spaces, use `space.name` or |
| 68 | + * `space.display_name` in the `filter`. Please see |
| 69 | + * {@see ChatServiceClient::spaceName()} for help formatting this field. |
| 70 | + * @param string $filter A search query. |
| 71 | + * |
| 72 | + * The query can specify one or more search keywords, which are used to filter |
| 73 | + * the results, |
| 74 | + * |
| 75 | + * You can also filter the results using the following message fields: |
| 76 | + * |
| 77 | + * - `create_time`: Accepts a timestamp in |
| 78 | + * [RFC-3339](https://www.rfc-editor.org/rfc/rfc3339) format and the |
| 79 | + * supported comparison operators are: `<` and `>=`. |
| 80 | + * - `sender.name`: The resource name of the sender (`users/{user}`). Only |
| 81 | + * supports `=`. You can use the e-mail as an alias for `{user}`. For |
| 82 | + * example, `users/example@gmail.com`, where `example@gmail.com` is the |
| 83 | + * e-mail of the Google Chat user. |
| 84 | + * - `space.name`: The resource name of the space where the message is posted. |
| 85 | + * (`spaces/{space}`). Only supports `=`. If this filter is not set, the |
| 86 | + * search is performed across all direct messages and spaces the user has |
| 87 | + * access to as a space member. |
| 88 | + * - `space.display_name`: Supports the operator `:` (has) and filters spaces |
| 89 | + * based on a partial match of their display name. Results are limited to |
| 90 | + * the top five space matches. For example, `space.display_name:Project` |
| 91 | + * searches for messages in the top five spaces that contain the word |
| 92 | + * "Project" in their display names. |
| 93 | + * - `attachment`: Supports the operator `:*` (has any) to check for the |
| 94 | + * presence of attachments. If `attachment:*` is specified, only messages |
| 95 | + * that have at least one attachment are returned. |
| 96 | + * - `annotations.user_mentions.user.name`: The resource name of the mentioned |
| 97 | + * user (`users/{user}`). Only supports `:` (has). For example: |
| 98 | + * `annotations.user_mentions.user.name:"users/1234567890"` returns only |
| 99 | + * messages that contain a mention to the specified user. Alternatively, the |
| 100 | + * alias `me` can be used to filter for messages that mention the caller |
| 101 | + * user, for example: `annotations.user_mentions.user.name:users/me`. You |
| 102 | + * can also use the e-mail as an alias for `{user}`, for example, |
| 103 | + * `users/example@gmail.com`. |
| 104 | + * |
| 105 | + * For advanced filtering, the following functions are also available: |
| 106 | + * |
| 107 | + * - `has_link()`: Returns only messages that have at least one hyperlink in |
| 108 | + * the message text. |
| 109 | + * - `is_unread()`: Filters out messages that have been read by the calling |
| 110 | + * user. |
| 111 | + * |
| 112 | + * Using the `space.display_name` filter requires that the calling credentials |
| 113 | + * include one of the following [authorization |
| 114 | + * scopes](https://developers.google.com/workspace/chat/authenticate-authorize#chat-api-scopes): |
| 115 | + * |
| 116 | + * - `https://www.googleapis.com/auth/chat.spaces.readonly` |
| 117 | + * - `https://www.googleapis.com/auth/chat.spaces` |
| 118 | + * |
| 119 | + * Using the `is_unread()` filter requires that the calling credentials |
| 120 | + * include one of the following [authorization |
| 121 | + * scopes](https://developers.google.com/workspace/chat/authenticate-authorize#chat-api-scopes): |
| 122 | + * |
| 123 | + * - `https://www.googleapis.com/auth/chat.users.readstate.readonly` |
| 124 | + * - `https://www.googleapis.com/auth/chat.users.readstate` |
| 125 | + * |
| 126 | + * |
| 127 | + * Across different fields, only `AND` operators are supported. A valid |
| 128 | + * example is `sender.name = "users/1234567890" AND is_unread()`. The word |
| 129 | + * `AND` is optional and is implied if omitted. For example, `sender.name = |
| 130 | + * "users/1234567890" is_unread()` is valid and is equivalent to the previous |
| 131 | + * example. An invalid example is `sender.name = "users/1234567890" OR |
| 132 | + * is_unread()` because `OR` is not supported between different fields. |
| 133 | + * |
| 134 | + * Among the same field: |
| 135 | + * |
| 136 | + * - `create_time` supports only `AND`, and can only be used to represent |
| 137 | + * an interval, such as `create_time >= "2022-01-01T00:00:00+00:00" AND |
| 138 | + * create_time < "2023-01-01T00:00:00+00:00"`. |
| 139 | + * - `sender.name` supports only the `OR` operator, for example: |
| 140 | + * `sender.name = "users/1234567890" OR sender.name = "users/0987654321"`. |
| 141 | + * - `space.name` supports only the `OR` operator, for example: |
| 142 | + * `space.name = "spaces/ABCDEFGH" OR space.name = "spaces/QWERTYUI"`. |
| 143 | + * - `space.display_name` supports the operators `AND` and `OR`, but not a |
| 144 | + * mix of both. For example: |
| 145 | + * `space.display_name:Project AND space.display_name:Tasks` returns |
| 146 | + * messages that are in spaces with display names containing both `Project` |
| 147 | + * and `Tasks`, whereas |
| 148 | + * `space.display_name:Project OR space.display_name:Tasks` returns messages |
| 149 | + * that are in spaces with display names containing either `Project` or |
| 150 | + * `Tasks` or both. |
| 151 | + * - `annotations.user_mentions.user.name` supports the operators `AND` and |
| 152 | + * `OR`, but not a mix of both. For example: |
| 153 | + * `annotations.user_mentions.user.name:"users/1234567890" AND |
| 154 | + * annotations.user_mentions.user.name:"users/0987654321"` returns only |
| 155 | + * messages that mentions both users, whereas |
| 156 | + * `annotations.user_mentions.user.name:"users/1234567890" OR |
| 157 | + * annotations.user_mentions.user.name:"users/0987654321"` returns messages |
| 158 | + * that mention either user or both. |
| 159 | + * |
| 160 | + * Parentheses are required to disambiguate operator precedence when combining |
| 161 | + * `AND` and `OR` operators in the same query. For example: |
| 162 | + * `(sender.name="users/me" OR sender.name="users/123456") AND is_unread()`. |
| 163 | + * Otherwise, parentheses are optional. |
| 164 | + * |
| 165 | + * The following example queries are valid: |
| 166 | + * |
| 167 | + * ``` |
| 168 | + * "Pending reports" AND create_time >= "2023-01-01T00:00:00Z" |
| 169 | + * |
| 170 | + * sender.name = "users/example@gmail.com" |
| 171 | + * |
| 172 | + * annotations.user_mentions.user.name:"users/0987654321" |
| 173 | + * |
| 174 | + * attachment:* AND space.name = "spaces/ABCDEFGH" |
| 175 | + * |
| 176 | + * tasks AND is_unread() AND sender.name = "users/1234567890" |
| 177 | + * |
| 178 | + * "things to do" "urgent" |
| 179 | + * |
| 180 | + * (sender.name = "users/1234567890") |
| 181 | + * AND (create_time < "2023-05-01T00:00:00Z") |
| 182 | + * |
| 183 | + * tasks AND space.name = "spaces/ABCDEFGH" AND has_link() |
| 184 | + * |
| 185 | + * "project one" is_unread() |
| 186 | + * |
| 187 | + * space.display_name:Project tasks |
| 188 | + * ``` |
| 189 | + * |
| 190 | + * The maximum query length is 1,000 characters. |
| 191 | + * |
| 192 | + * Invalid queries are rejected by the server with an `INVALID_ARGUMENT` |
| 193 | + * error. |
| 194 | + */ |
| 195 | +function search_messages_sample(string $formattedParent, string $filter): void |
| 196 | +{ |
| 197 | + // Create a client. |
| 198 | + $chatServiceClient = new ChatServiceClient(); |
| 199 | + |
| 200 | + // Prepare the request message. |
| 201 | + $request = (new SearchMessagesRequest()) |
| 202 | + ->setParent($formattedParent) |
| 203 | + ->setFilter($filter); |
| 204 | + |
| 205 | + // Call the API and handle any network failures. |
| 206 | + try { |
| 207 | + /** @var PagedListResponse $response */ |
| 208 | + $response = $chatServiceClient->searchMessages($request); |
| 209 | + |
| 210 | + /** @var SearchMessageResult $element */ |
| 211 | + foreach ($response as $element) { |
| 212 | + printf('Element data: %s' . PHP_EOL, $element->serializeToJsonString()); |
| 213 | + } |
| 214 | + } catch (ApiException $ex) { |
| 215 | + printf('Call failed with message: %s' . PHP_EOL, $ex->getMessage()); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +/** |
| 220 | + * Helper to execute the sample. |
| 221 | + * |
| 222 | + * This sample has been automatically generated and should be regarded as a code |
| 223 | + * template only. It will require modifications to work: |
| 224 | + * - It may require correct/in-range values for request initialization. |
| 225 | + * - It may require specifying regional endpoints when creating the service client, |
| 226 | + * please see the apiEndpoint client configuration option for more details. |
| 227 | + */ |
| 228 | +function callSample(): void |
| 229 | +{ |
| 230 | + $formattedParent = ChatServiceClient::spaceName('[SPACE]'); |
| 231 | + $filter = '[FILTER]'; |
| 232 | + |
| 233 | + search_messages_sample($formattedParent, $filter); |
| 234 | +} |
| 235 | +// [END chat_v1_generated_ChatService_SearchMessages_sync] |
0 commit comments