このページでは、Google Chat アプリとしてコマンドを設定して応答する方法について説明します。
コマンドを使用すると、ユーザーは Chat アプリの主要な機能を見つけて使用できます。コマンドの内容を表示できるのは Chat アプリのみです。たとえば、ユーザーがスラッシュ コマンドを含むメッセージを送信した場合、そのメッセージはユーザーと Chat アプリにのみ表示されます。
コマンドを作成するかどうかを判断し、ユーザー インタラクションを設計する方法については、すべてのユーザー ジャーニーを定義するをご覧ください。
Chat アプリのコマンドの種類
Chat アプリのコマンドは、スラッシュ コマンド、クイック コマンド、メッセージ アクションとして作成できます。 各タイプのコマンドを使用するには、ユーザーは次の操作を行います。-
スラッシュ コマンド: ユーザーは、メニューからスラッシュ コマンドを選択するか、スラッシュ(
/)を入力してから、/aboutなどの事前定義されたテキストを入力します。通常、Chat アプリでは、スラッシュ コマンドの引数テキストが必要です。Chat アプリでユーザーからの追加の入力が必要な場合は、スラッシュ コマンドを作成します。たとえば、ユーザーが検索するフレーズ(
/search receiptsなど)を入力した後に実行される/searchというスラッシュ コマンドを作成できます。 -
クイック コマンド: ユーザーは、Chat メッセージの返信 エリアからメニューを開いてコマンドを使用します。コマンドを使用するには、[追加]
をクリックして、メニューからコマンドを選択します。
Chat アプリが追加の入力を待たずにユーザーにすぐに応答できる場合は、クイック コマンドを作成します。たとえば、画像ですぐに応答する Random image というクイック コマンドを作成できます。
-
メッセージ アクション: ( デベロッパー プレビュー) ユーザーは、メッセージにカーソルを合わせて その他メニューをクリックして、メッセージ アクションを使用します。コマンドを使用するには、その他メニューを開き、 コマンドをメニューから選択します。
Chat アプリがメッセージのコンテキストに基づいてアクションを実行できる場合は、メッセージ アクションを作成します。
次の図は、ユーザーがスラッシュ コマンド、クイック コマンド、メッセージ アクションのメニューを見つける方法を示しています。
前提条件
HTTP
Google Chat を拡張する Google Workspace アドオン。作成するには、 HTTP クイックスタートを完了します。
Apps Script
Google Chat を拡張する Google Workspace アドオン。作成するには、 Apps Script クイックスタートを完了します。
コマンドを設定する
このセクションでは、コマンドを設定する次の手順について説明します。
コマンドに名前を付けて説明する
コマンドの名前は、ユーザーが Chat アプリを呼び出すために入力または選択するものです。名前の下には簡単な説明も表示され、コマンドの使用方法についてユーザーにさらに説明します。
コマンドの名前と説明を選択する際は、次の推奨事項を考慮してください。
コマンドに名前を付けるには:
- 短く、説明的で、操作可能な単語やフレーズを使用して、コマンドを
ユーザーにわかりやすくします。たとえば、
Create a reminderという名前ではなく、Remind meを使用します。 - コマンドに一意の名前または一般的な名前を使用することを検討してください。コマンドが一般的なインタラクションや機能を説明している場合は、ユーザーが認識して期待する一般的な名前(
SettingsやFeedbackなど)を使用できます。それ以外の場合は、一意のコマンド名を使用してください。コマンド名が他の Chat アプリと同じ場合、ユーザーは類似のコマンドをフィルタして、自分のコマンドを見つけて使用する必要があります。
コマンドを説明するには:
- 説明を短くわかりやすくして、ユーザーがコマンドを使用したときに何が起こるかを把握できるようにします。
- コマンドに書式設定の要件がある場合は、ユーザーに知らせます。たとえば、引数テキストを必要とするスラッシュ コマンドを作成する場合は、説明を
Remind me to do [something] at [time]のように設定します。 - Chat アプリがスペース内の全員に返信するか、
コマンドを呼び出したユーザーに非公開で返信するかをユーザーに知らせます。たとえば、クイック コマンド
Aboutの場合は、Learn about this app (Only visible to you)と説明できます。
Google Cloud コンソールでコマンドを構成する
スラッシュ コマンド、クイック コマンド、メッセージ アクションを作成するには、Google Chat API の Chat アプリの構成でコマンドまたはアクションに関する情報を指定します。
Google Chat API でコマンドを構成する手順は次のとおりです。
Google Cloud コンソールで、メニュー アイコン > [API とサービス] > [有効な API とサービス] > [Google Chat API] をクリックします。
[構成] をクリックします。
[接続設定] で、[トリガー] に移動して、エンドポイント の詳細を指定します。次のセクションでこのトリガーを使用して、コマンドに応答する必要があります。
- HTTP エンドポイント URL: ここで一般的な HTTP エンドポイント URL を 1 つ指定できます。 または、トリガーごとに異なる HTTP エンドポイントを使用するには、[アプリコマンド] フィールドでエンドポイントを直接指定します。
- Apps Script: Apps Script のデプロイ ID を入力します。デフォルトでは、
onAppCommand関数が呼び出されます。別の Apps Script 関数を使用するには、[アプリコマンド] フィールドでカスタム関数名を指定します。
[コマンド] で、[コマンドを追加] をクリックします。
コマンドに関する次の情報を入力します。
- コマンド ID: Chat アプリがコマンドを認識してレスポンスを返すために使用する 1 ~ 1,000 の数値。
- 説明: コマンドの使用方法と書式設定方法を説明するテキスト。説明は 50 文字までです。
- **コマンドタイプ:** [**クイック コマンド**]、[**スラッシュ コマンド**]、[**メッセージ アクション**] のいずれかを選択します。
- コマンドの名前を指定します。
- クイック コマンド名: ユーザーがメニューから選択してコマンドを呼び出す表示名。50 文字以内で、特殊文字を含めることができます。例:
Remind me。 - スラッシュ コマンド名: ユーザーがメッセージでコマンドを呼び出すために入力するテキスト。スラッシュで始まり、テキストのみを含み、50 文字以内で指定する必要があります。例:
/remindMe。 - メッセージ アクション名:
( デベロッパー プレビュー)
ユーザーがメニューから選択してメッセージ アクションを呼び出す表示名。50 文字以内で、特殊文字を含めることができます。例:
Remind me。
- クイック コマンド名: ユーザーがメニューから選択してコマンドを呼び出す表示名。50 文字以内で、特殊文字を含めることができます。例:
省略可: 読み込み通知メッセージ: ( デベロッパー プレビュー) メッセージ アクションの実行中にユーザーに 表示するトースト通知メッセージ。ダイアログを開かないメッセージ アクションでのみ使用できます。
省略可: Chat アプリがダイアログでコマンドに応答するようにする場合は、[ダイアログを開く] チェックボックスをオンにします。
[保存] をクリックします。
これで、Chat アプリのコマンドが構成されました。
コマンドに応答する
ユーザーがコマンドを使用すると、Chat アプリは
イベント オブジェクトを受け取ります。
イベント ペイロードには、呼び出されたコマンドの詳細(コマンド
ID やコマンドタイプなど)を含む
appCommandPayload
オブジェクトが含まれているため、適切なレスポンスを返すことができます。
イベント オブジェクトは、
HTTP エンドポイントまたは Apps Script 関数に送信されます。これは、[**アプリコマンド**]
トリガーを構成したときに指定したものです。
/help に非公開で応答して、サポートを受ける方法を説明します。次のコードは、スラッシュ コマンド /about にテキスト メッセージで応答する Chat アプリの例を示しています。スラッシュ コマンドに応答するために、Chat アプリは [アプリコマンド] トリガーからのイベント オブジェクトを処理します。イベント オブジェクトのペイロードにスラッシュ
コマンド ID が含まれている場合、Chat アプリはアクション DataActions
と createMessageAction オブジェクトを返します。
Node.js
Python
Java
Apps Script
このコードサンプルを使用するには、ABOUT_COMMAND_ID を、Chat API でコマンドを構成したときに指定した
コマンド ID
に置き換えます。
メッセージ アクションに応答する
次のコードは、メッセージ アクション Remind me にテキスト メッセージで応答する Chat アプリの例を示しています。メッセージ アクションに応答するために、Chat アプリは [アプリコマンド] トリガーからのイベント オブジェクトを処理します。イベント オブジェクトのペイロードに
メッセージ アクション コマンド ID が含まれている場合、Chat アプリはアクション
DataActionsと createMessageAction
オブジェクトを返します。
Node.js
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @param {Object} res The HTTP response object.
* @return {Object} The JSON response message with a confirmation.
*/
function onAppCommand(event, res) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} =
event.chat.appCommandPayload.appCommandMetadata;
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.chat.appCommandPayload.message.text;
// Return a response that includes details from the original message.
return res.json({
"hostAppDataAction": {
"chatDataAction": {
"createMessageAction": {
"message": {
"text": `Setting a reminder for message: "${messageText}"`
}
}
}
}
});
}
}
Python
def on_app_command(event):
"""Responds to an APP_COMMAND interaction event from Google Chat.
Args:
event (dict): The interaction event from Google Chat.
Returns:
dict: The JSON response message with a confirmation.
"""
# Collect the command ID and type from the event metadata.
payload = event.get('chat', {}).get('appCommandPayload', {})
metadata = payload.get('appCommandMetadata', {})
if metadata.get('appCommandType') == 'MESSAGE_ACTION' and \
metadata.get('appCommandId') == REMIND_ME_COMMAND_ID:
# Message actions can access the context of the message they were
# invoked on, such as the text or sender of that message.
message_text = payload.get('message', {}).get('text')
# Return a response that includes details from the original message.
return {
"hostAppDataAction": {
"chatDataAction": {
"createMessageAction": {
"message": {
"text": f'Setting a reminder for message: "{message_text}"'
}
}
}
}
}
Java
/**
* Responds to an APP_COMMAND interaction event from Google Chat.
*
* @param event The interaction event from Google Chat.
* @param response The HTTP response object.
*/
void onAppCommand(JsonObject event, HttpResponse response) throws Exception {
// Collect the command ID and type from the event metadata.
JsonObject payload = event.getAsJsonObject("chat").getAsJsonObject("appCommandPayload");
JsonObject metadata = payload.getAsJsonObject("appCommandMetadata");
String appCommandType = metadata.get("appCommandType").getAsString();
if (appCommandType.equals("MESSAGE_ACTION")) {
int commandId = metadata.get("appCommandId").getAsInt();
if (commandId == REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
String messageText = payload.getAsJsonObject("message").get("text").getAsString();
// Return a response that includes details from the original message.
JsonObject responseMessage = new JsonObject();
responseMessage.addProperty("text", "Setting a reminder for message: " + messageText);
JsonObject createMessageAction = new JsonObject();
createMessageAction.add("message", responseMessage);
JsonObject chatDataAction = new JsonObject();
chatDataAction.add("createMessageAction", createMessageAction);
JsonObject hostAppDataAction = new JsonObject();
hostAppDataAction.add("chatDataAction", chatDataAction);
JsonObject finalResponse = new JsonObject();
finalResponse.add("hostAppDataAction", hostAppDataAction);
response.getWriter().write(finalResponse.toString());
}
}
}
Apps Script
/**
* Responds to an APP_COMMAND interaction event in Google Chat.
*
* @param {Object} event The interaction event from Google Chat.
* @return {Object} The JSON response message with a confirmation.
*/
function onAppCommand(event) {
// Collect the command ID and type from the event metadata.
const {appCommandId, appCommandType} =
event.chat.appCommandPayload.appCommandMetadata;
if (appCommandType === 'MESSAGE_ACTION' &&
appCommandId === REMIND_ME_COMMAND_ID) {
// Message actions can access the context of the message they were
// invoked on, such as the text or sender of that message.
const messageText = event.chat.appCommandPayload.message.text;
// Return a response that includes details from the original message.
return CardService.newChatResponseBuilder()
.setText("Setting a reminder for message: " + messageText)
.build();
}
}
このコードサンプルを使用するには、Chat API でコマンドを構成したときに指定したコマンド ID で REMIND_ME_COMMAND_ID を置き換えます。
コマンドをテストする
コマンドとコードをテストするには、 Google Chat アプリのインタラクティブ機能をテストするをご覧ください。
Chat UI でコマンドをテストして使用する方法については、 Google Chat でアプリを使用する (Google Chat ヘルプ ドキュメント)をご覧ください。