このガイドでは、Google Chat API を使用してユーザーのステータスとカスタム ステータスを管理する方法について説明します。
Chat ユーザーのステータスを読み取って更新するには、アプリがユーザー認証で認証されている必要があります。認証されたユーザーの空き状況のみにアクセスまたは変更できます。
前提条件
Node.js
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Node.js Cloud クライアント ライブラリをインストールします。
- デスクトップ アプリケーション用の
OAuth クライアント ID 認証情報を作成します。このガイドのサンプルを実行するには、認証情報を
credentials.jsonという名前の JSON ファイルとしてローカル ディレクトリに保存します。
- ユーザー認証をサポートする 認可スコープを選択します。
Python
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Python Cloud クライアント ライブラリをインストールします。
- デスクトップ アプリケーション用の
OAuth クライアント ID 認証情報を作成します。このガイドのサンプルを実行するには、認証情報を
credentials.jsonという名前の JSON ファイルとしてローカル ディレクトリに保存します。
- ユーザー認証をサポートする 認可スコープを選択します。
Java
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- Java Cloud クライアント ライブラリをインストールします。
- デスクトップ アプリケーション用の
OAuth クライアント ID 認証情報を作成します。このガイドのサンプルを実行するには、認証情報を
credentials.jsonという名前の JSON ファイルとしてローカル ディレクトリに保存します。
- ユーザー認証をサポートする 認可スコープを選択します。
Apps Script
- Google Chat へのアクセス権を持つ Business または Enterprise の Google Workspace アカウント。
- 環境を設定します。
- Google Cloud プロジェクトを作成します。
- OAuth 同意画面を構成する。
- Chat 用アプリの名前、アイコン、説明を使用して、Google Chat API を有効にして構成します。
- スタンドアロンの Apps Script プロジェクトを作成し、高度な Chat サービスを有効にします。
- ユーザー認証をサポートする 認可スコープを選択します。
ユーザーの空き情報を取得する
Google Chat ユーザーのステータスを取得するには、リクエストで次の情報を渡します。
chat.users.availability.readonlyまたはchat.users.availability認証スコープを指定します。GetAvailabilityメソッドを呼び出します。- 取得する可用性リソースの
nameを渡します。名前はusers/{user}/availabilityの形式にする必要があります。呼び出し元を参照するには、ユーザーのメールアドレスまたはmeエイリアスを使用できます。例:users/me/availability
ユーザーの空き状況を取得する方法は次のとおりです。
Node.js
const { ChatServiceClient } = require('@google-apps/chat').v1;
// Instantiates a client
const chatServiceClient = new ChatServiceClient();
async function getAvailability() {
const request = {
// The name of the availability resource to retrieve.
// Format: users/{user}/availability
// The 'me' alias can be used to refer to the calling user.
name: 'users/me/availability',
};
try {
const response = await chatServiceClient.getAvailability(request);
console.log(response);
} catch (err) {
console.error('Error retrieving availability:', err);
}
}
getAvailability();
Python
from google.apps import chat_v1 as google_chat
def get_availability():
# Instantiates a client
client = google_chat.ChatServiceClient()
# Prepare request
request = google_chat.GetAvailabilityRequest(
# Format: users/{user}/availability
# The 'me' alias refers to the calling user.
name="users/me/availability",
)
# Call the API
try:
response = client.get_availability(request=request)
print(response)
except Exception as e:
print(f"Error retrieving availability: {e}")
get_availability()
Java
import com.google.chat.v1.Availability;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.GetAvailabilityRequest;
public class GetAvailability {
public static void main(String[] args) throws Exception {
// Instantiates a client
try (ChatServiceClient chatServiceClient = ChatServiceClient.create()) {
GetAvailabilityRequest request = GetAvailabilityRequest.newBuilder()
// Format: users/{user}/availability
// The 'me' alias refers to the calling user.
.setName("users/me/availability")
.build();
Availability response = chatServiceClient.getAvailability(request);
System.out.println(response);
}
}
}
Apps Script
/**
* Retrieves the calling user's availability details.
*/
function getUserAvailability() {
const name = 'users/me/availability';
try {
const availability = Chat.Users.Availability.get(name);
console.log(availability);
} catch (err) {
console.error('Failed to get availability: ' + err.message);
}
}
Chat API は、ユーザーのプレゼンス状態とカスタム ステータスの詳細を示す Availability のインスタンスを返します。
カスタム ステータスを更新する
ユーザーのカスタム ステータスを更新するには、リクエストで次の情報を渡します。
chat.users.availability認可スコープを指定します。UpdateAvailabilityメソッドを呼び出します。- 新しい
customStatusの詳細を指定して、Availabilityリソースを渡します。 update_maskパラメータを設定して、custom_statusフィールドを含めます。
ユーザーのカスタム ステータスを更新する手順は次のとおりです。
Node.js
const { ChatServiceClient } = require('@google-apps/chat').v1;
// Instantiates a client
const chatServiceClient = new ChatServiceClient();
async function updateCustomStatus() {
const request = {
// The Availability resource to update.
availability: {
name: 'users/me/availability',
customStatus: {
text: 'In a meeting',
emoji: {
unicode: '📅'
}
}
},
// The fields to update. Must contain 'custom_status'.
updateMask: {
paths: ['custom_status']
}
};
try {
const response = await chatServiceClient.updateAvailability(request);
console.log(response);
} catch (err) {
console.error('Error updating status:', err);
}
}
updateCustomStatus();
Python
from google.apps import chat_v1 as google_chat
from google.protobuf import field_mask_pb2
def update_custom_status():
# Instantiates a client
client = google_chat.ChatServiceClient()
# Define custom status and emoji
custom_status = google_chat.CustomStatus(
text="In a meeting",
emoji=google_chat.Emoji(unicode="📅")
)
# Initialize availability object
availability = google_chat.Availability(
name="users/me/availability",
custom_status=custom_status
)
# Specify update mask
update_mask = field_mask_pb2.FieldMask(paths=["custom_status"])
# Prepare request
request = google_chat.UpdateAvailabilityRequest(
availability=availability,
update_mask=update_mask
)
# Call the API
try:
response = client.update_availability(request=request)
print(response)
except Exception as e:
print(f"Error updating status: {e}")
update_custom_status()
Java
import com.google.chat.v1.Availability;
import com.google.chat.v1.ChatServiceClient;
import com.google.chat.v1.CustomStatus;
import com.google.chat.v1.Emoji;
import com.google.chat.v1.UpdateAvailabilityRequest;
import com.google.protobuf.FieldMask;
public class UpdateCustomStatus {
public static void main(String[] args) throws Exception {
// Instantiates a client
try (ChatServiceClient chatServiceClient = ChatServiceClient.create()) {
CustomStatus customStatus = CustomStatus.newBuilder()
.setText("In a meeting")
.setEmoji(Emoji.newBuilder().setUnicode("📅"))
.build();
Availability availability = Availability.newBuilder()
.setName("users/me/availability")
.setCustomStatus(customStatus)
.build();
FieldMask updateMask = FieldMask.newBuilder()
.addPaths("custom_status")
.build();
UpdateAvailabilityRequest request = UpdateAvailabilityRequest.newBuilder()
.setAvailability(availability)
.setUpdateMask(updateMask)
.build();
Availability response = chatServiceClient.updateAvailability(request);
System.out.println(response);
}
}
}
Apps Script
/**
* Updates the calling user's custom status message.
*/
function updateCustomStatus() {
const name = 'users/me/availability';
const availability = {
customStatus: {
text: 'In a meeting',
emoji: {
unicode: '📅'
}
}
};
const updateMask = 'custom_status';
try {
const response = Chat.Users.Availability.patch(availability, name, {
updateMask: updateMask
});
console.log(response);
} catch (err) {
console.error('Failed to update status: ' + err.message);
}
}
Chat API は、新しいカスタム ステータスで Availability のインスタンスを更新して返します。
ユーザーのカスタム ステータスをクリアするには、空き状況を更新し、customStatus の詳細を省略します。
プレゼンスの状態を更新する
ターゲットのプレゼンス状態に応じて、次のいずれかのカスタム メソッドを呼び出して、ユーザーの可用性を更新できます。
- Active:
MarkAsActiveメソッドを呼び出して、プレゼンスをアクティブに設定します。有効期限の値(ttlまたはexpireTime)は省略可能です。 - Away:
MarkAsAwayメソッドを呼び出して、プレゼンスを Away に設定します。 - サイレント モード:
MarkAsDoNotDisturbメソッドを呼び出して通知をミュートします。
フィールドの説明やクエリ テンプレートなどの詳細については、各メソッドのリファレンス ガイドをご覧ください。
予定から空き状況の変更を特定する
Chat 用アプリは google.workspace.chat.availability.v1.updated イベントを登録して、ユーザーの空き状況が変更されたときに通知を受け取ることができます。空き状況の更新が発生すると、アプリは Availability リソースを含むペイロードを含むイベントを受け取ります。