# チャンネルメッセージ

> Send, receive and manage text messages in Vivox channels.

TextOnly または TextAndAudio の ChatCapability を使用して、テキストが有効になっている特定の Vivox チャンネルに VivoxMessage を送信できます。

## チャンネルメッセージを送信する##send-channel-messages

[`VivoxService.Instance.SendChannelTextMessageAsync(string channelName, string message)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_SendChannelTextMessageAsync_System_String_System_String_Unity_Services_Vivox_MessageOptions_) を使用して、チャンネルにメッセージを送信できます。`channelName` はメッセージの送信先チャンネル、`message` は送信するメッセージです。

以下のコードスニペットは、設定された Lobby チャンネルにメッセージを送信し、MessageInputField InputField オブジェクトからテキストをプルするための実装例です。

```cs
public async void SendMessageAsync()
{
    if (string.IsNullOrEmpty(MessageInputField.text))
    {
        return;
    }

    VivoxService.Instance.SendChannelTextMessageAsync(LobbyChannelName, MessageInputField.text);
    MessageInputField.text = string.Empty;
}
```

## チャンネルメッセージを受信する##receive-channel-messages

テキストが有効になっているチャンネルからメッセージを受信するには、[`VivoxService.Instance.ChannelMessageReceived`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageReceived) アクションにサブスクライブする必要があります。

チャンネルに送信される VivoxMessage は [直接送信メッセージ](./directed-messages.md) とほぼ同じですが、`VivoxMessage.ChannelName` に ChannelName が追加され、メッセージが現在サインインしているユーザーから送信されたかどうかに基づいて `VivoxMessage.FromSelf` フィールドが設定されます。

以下のコードスニペットは、このアクションにサブスクライブする行の例と、受信時に VivoxMessage から利用可能なすべての情報をプルする例です。

```cs

//Subscribe to this event at some point in the Vivox initialization process,
//preferably before joining any channels.
. . .
VivoxService.Instance.ChannelMessageReceived += OnChannelMessageReceived;
. . .

void OnChannelMessageReceived(VivoxMessage message)
{
    var channelName = message.ChannelName;
    var senderName = message.SenderDisplayName;
    var senderId = message.SenderPlayerId;
    var messageText = message.MessageText;
    var timeReceived = message.ReceivedTime;
    var language = message.Language;
    var fromSelf = message.FromSelf;
    var messageId = message.MessageId;
}
```

## チャンネルテキストメッセージの履歴##channel-text-message-history

Vivox では、[`VivoxService.Instance.GetChannelTextMessageHistoryAsync(string channelName, int requestSize = 10, ChatHistoryQueryOptions chatHistoryQueryOptions = null)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_GetChannelTextMessageHistoryAsync_System_String_System_Int32_Unity_Services_Vivox_ChatHistoryQueryOptions_) を使用して、ユーザーがチャンネルのテキストアクティビティの履歴にアクセスできるようにします。`channelName` は履歴をリクエストするチャンネルの名前、`requestSize` は返すメッセージの最大数 (デフォルトは 10) で、`chatHistoryQueryOptions` はメッセージに含まれる単語やフレーズ、特定の PlayerId のメッセージ送信者、クエリの対象とする時刻 (その時刻の前または後) の指定などができる任意のパラメーターです。`ChatHistoryQueryOption` のより具体的な情報については、[チャット履歴のクエリオプション](../../text-chat-guide/chat-history-query-options) を参照してください。

`IReadOnlyCollection` で返されるメッセージは、最新のメッセージから最も古いメッセージへの順でリスト表示されます。

> **Note:**
>
> チャット履歴は 7 日間保存されます。[テキストエビデンス管理](../../text-chat-guide/text-evidence/text-evidence-management) が使用される場合、チャット履歴の保存期間が 30 日間に延長されます。

以下のコードスニペットは、設定された LobbyChannelName から最新の 25 件のメッセージをプルし、任意の ChatHistoryQueryOptions は使用せずに、送信者の表示名とメッセージを古いものから新しいものへの順でログ記録する例です。

```cs
public async void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetChannelTextMessageHistoryAsync(LobbyChannelName, 10);
    //Reversing the messages so they display from oldest to newest
    var reversedMessages = historyMessages.Reverse();
    foreach(VivoxMessage message in reversedMessages)
    {
        Debug.Log($"{message.SenderDisplayName}: {message.MessageText}");
    }
}
```

## チャンネルメッセージを編集する##edit-channel-messages

Vivox では、[`VivoxService.Instance.EditChannelTextMessageAsync(string channelName, string messageId, string newMessage)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_EditChannelTextMessageAsync_System_String_System_String_System_String_) を使用して、ユーザーがチャンネルに送信したメッセージのテキストを編集できるようにします。channelName はメッセージが送信されたチャンネルの名前、messageId は変更するメッセージの ID、newMessage はメッセージの更新後のテキストです。

チャンネル内の誰かによってメッセージが正常に編集されると、チャンネル内のすべてのユーザーは、編集された VivoxMessage と更新後の MessageText を含む [`VivoxService.Instance.ChannelMessageEdited`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageEdited) アクションを受信します。

```cs
public async void UpdateChannelMessageAsync(VivoxMessage messageToUpdate, string updatedMessageText)
{
    await VivoxService.Instance.EditChannelTextMessageAsync(messageToUpdate.ChannelName, messageToUpdate.MessageId, updatedMessageText);
}
```

## チャンネルメッセージを削除する##delete-channel-messages

Vivox では、[`VivoxService.Instance.DeleteChannelTextMessageAsync(string channelName, string messageId)`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DeleteChannelTextMessageAsync_System_String_System_String_) を使用して、ユーザーがチャンネルに送信したメッセージのテキストを削除できるようにします。channelName はメッセージが送信されたチャンネルの名前、messageId は削除するメッセージの ID です。

チャンネル内の誰かによってメッセージが正常に削除されると、チャンネル内のすべてのユーザーは、削除された VivoxMessage を含む [`VivoxService.Instance.ChannelMessageDeleted`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_ChannelMessageDeleted) アクションを受信します。

```cs
public async void DeleteChannelMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteChannelTextMessageAsync(messageToDelete.ChannelName, messageToDelete.MessageId)
}
```
