# 直接送信メッセージ

> Send direct messages between users.

サービスにサインインしている個々のプレイヤーに対して、VivoxMessage を送信できます。この機能はしばしば DM と呼ばれます。

## 直接送信メッセージを送信する##send-directed-messages

[`VivoxService.Instance.SendDirectTextMessageAsync(string playerId, 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_SendDirectTextMessageAsync_System_String_System_String_Unity_Services_Vivox_MessageOptions_) を使用して、プレイヤーにメッセージを送信できます。playerId はメッセージ送信先のプレイヤーの playerId、message は送信するメッセージです。

以下のコードスニペットは、マネージチャンネルロースターから渡されたプレイヤーに、専用のメッセージ InputField からプルされたメッセージを送信するための実装例です。

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

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

## 直接送信メッセージを受信する##receive-directed-messages

他のユーザーからのメッセージを受信するには、[`VivoxService.Instance.DirectedMessageReceived`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageReceived) イベントにサブスクライブする必要があります。

直接送信 VivoxMessage は [チャンネルメッセージ](./channel-messages.md) とほぼ同じですが、`VivoxMessage.ChannelName` が null に設定され、`VivoxMessage.FromSelf` が false に設定されるという注意点があります。

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

```cs

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

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

## ダイレクトテキストメッセージの履歴##direct-text-message-history

また、Vivox では [`VivoxService.Instance.GetDirectTextMessageHistoryAsync(string playerId, 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_GetDirectTextMessageHistoryAsync_System_String_System_Int32_Unity_Services_Vivox_ChatHistoryQueryOptions_) を使用して、ユーザーが 2 人のプレイヤー間のダイレクトメッセージによる対話の履歴にアクセスできるようにします。playerId は履歴をリクエストするプレイヤーの ID、requestSize は返すメッセージの最大数 (デフォルトは 10) で、chatHistoryQueryOptions はメッセージに含まれる単語やフレーズ、クエリの対象とする時刻 (その時刻の前または後) の指定などができる任意のパラメーターです。ChatHistoryQueryOptions のより具体的な情報については、チャット履歴のクエリオプション のページを参照してください。

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

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

```cs
void FetchHistoryAsync()
{
    var historyMessages = await VivoxService.Instance.GetDirectTextMessageHistoryAsync(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}");
    }
}
```

## ダイレクトメッセージの編集##editing-direct-messages

Vivox では、[`VivoxService.Instance.EditDirectTextMessageAsync(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_EditDirectTextMessageAsync_System_String_System_String_) を使用して、ユーザーがチャンネルに送信したメッセージのテキストを編集できるようにします。messageId は変更するメッセージの ID、newMessage はメッセージの更新後のテキストです。

メッセージが正常に編集されると、ダイレクトメッセージに関連付けられているサインイン中のプレイヤーは、編集された VivoxMessage と更新後の MessageText を含む [`VivoxService.Instance.DirectedMessageEdited`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageEdited) アクションを受信します。

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

## ダイレクトメッセージの削除##deleting-direct-messages

Vivox では、[`VivoxService.Instance.DeleteDirectTextMessageAsync(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_DeleteDirectTextMessageAsync_System_String_) を使用して、ユーザーが送信したダイレクトメッセージを削除できるようにします。messageId は削除するメッセージの ID です。

メッセージが正常に削除されると、直接送信メッセージに関連付けられているサインイン中のプレイヤーは、削除された VivoxMessage を含む [`VivoxService.Instance.DirectedMessageDeleted`](https://docs.unity3d.com/Packages/com.unity.services.vivox@latest/index.html?subfolder=/api/Unity.Services.Vivox.IVivoxService.html#Unity_Services_Vivox_IVivoxService_DirectedMessageDeleted) アクションを受信します。

```cs
public async void DeleteDirectMessageAsync(VivoxMessage messageToDelete)
{
    await VivoxService.Instance.DeleteDirectTextMessageAsync(messageToDelete.MessageId)
}
```
