このガイドでは、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メソッドを呼び出します。 Availabilityリソースを渡し、新しいcustomStatusの詳細を指定します。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 の詳細を省略します。
プレゼンス ステータスを更新する
ターゲットのプレゼンス ステータスに応じて、次のいずれかのカスタム メソッドを呼び出してユーザーの利用可能ステータスを更新できます。
- アクティブ:
MarkAsActiveメソッドを呼び出して、プレゼンスをアクティブに設定します。有効期限の値(ttlまたはexpireTime)を指定することもできます。 - 不在:
MarkAsAwayメソッドを呼び出して、プレゼンスを不在に設定します。 - サイレント モード:
MarkAsDoNotDisturbメソッドを呼び出して、通知をミュートします。
フィールドの説明やクエリ テンプレートなどの詳細については、各メソッドのリファレンス ガイドをご覧ください。
イベントから利用可能ステータスの変更を特定する
Chat アプリは、google.workspace.chat.availability.v1.updated イベントに登録して、ユーザーの利用可能ステータスが変更されたときに通知を受け取ることができます。利用可能ステータスが更新されると、アプリは Availability リソースを含むペイロードを含むイベントを受信します。
関連トピック
- Google Chat ユーザーを特定して指定する。
- Google Chat のイベントに登録する。