[Twilio+Unity] Twilio API を使用した SMS 送信

[Twilio+Unity] Twilio API を使用した SMS 送信

Twilio API を使用して Unity アプリケーションから SMS を送信する手順を紹介します。 Twilio アカウントが作成済みである想定です。
参考ページ: https://www.twilio.com/docs/messaging/quickstart/csharp-dotnet-framework

作業環境

バージョン

OS: Windows 11 (23H2)
Unity: 6000.0.34f1
Api Compatibility Level: .NET Standard 2.1
NuGetForUnity: 4.3.0
NuGet Twilio package: 7.8.5

Twilio CLI のインストール

Scoop を使用して Windows 環境に Twilio CLI をインストールします。 Powershell を管理者権限で起動し、次のコマンドを実行します。

$ scoop bucket add twilio-scoop https://github.com/twilio/scoop-twilio-cli
$ scoop install twilio
Installing 'twilio' (5.22.11) [64bit] from 'twilio-scoop' bucket
Loading twilio-win32-x64.tar.xz from cache
Checking hash of twilio-win32-x64.tar.xz ... ok.
Extracting twilio-win32-x64.tar.xz ... done.
Linking ~\scoop\apps\twilio\current => ~\scoop\apps\twilio\5.22.11
Creating shim for 'twilio'.
Running post_install script...
*************************************************************************
*                                                                       *
* To get started, please create a twilio-cli profile:                   *
*                                                                       *
*     twilio login                                                      *
*                                                                       *
*     OR                                                                *
*                                                                       *
*     twilio profiles:create                                            *
*                                                                       *
*************************************************************************

 » If you’re using autocomplete, you’ll need to run 'twilio autocomplete' after the install and then open a new terminal window. The CLI needs to re-build its cache.
done.
'twilio' (5.22.11) was installed successfully!

twilio login コマンドでログインします。

$ twilio login
? The Account SID for your Twilio Account or Subaccount: ****** # Account SID を入力します
? Your Twilio Auth Token for your Twilio Account or Subaccount: ****** # Auth Token を入力します
? Shorthand identifier for your profile: unity-sample # 任意の名前を入力します

Twilio C# SDK のインストール

Unity では NuGet パッケージを直接インストールできない ため、手動で Twilio SDK を導入する必要があります。NuGetForUnity を導入し、Twilio パッケージをインストールします。

nuget-for-unity-window

Twilio API を利用し SMS 送信

今回は例として次のような UI を作成しました。

3

この例では、「SMS 送信」 のボタンに、次のスクリプトをアタッチしています。

ButtonSend.cs

using UnityEngine;
using TMPro;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

public class ButtonSend : MonoBehaviour
{
    private const string accountSid = "****";  // Twilio のアカウント SID
    private const string authToken = "****";  // Twilio の Auth Token
    private const string fromPhoneNumber = "****";  // Twilio で購入した電話番号

    // 送信先電話番号
    [SerializeField]
    private TMP_InputField inputToNumber;

    // 送信メッセージ
    [SerializeField]
    private TMP_InputField inputMessageBody;

    public void SendSms()
    {
        TwilioClient.Init(accountSid, authToken);

        var messageResponse = MessageResource.Create(
            body: inputMessageBody.text,
            from: new Twilio.Types.PhoneNumber(fromPhoneNumber),
            to: new Twilio.Types.PhoneNumber(inputToNumber.text)
        );

        Debug.Log($"SMS sent: {messageResponse.Sid}");
    }
}

動作確認

送信先電話番号とメッセージを入力してからボタンを押下し、 SMS が送信されることを確認しました。

test-send-button

Unity Editor のログ

SMS sent: SMaed5881279330ee09bf55f98d61fc8ba
UnityEngine.Debug:Log (object)
ButtonSend:SendSms () (at Assets/Scripts/ButtonSend.cs:30)
UnityEngine.EventSystems.EventSystem:Update () (at ./Library/PackageCache/com.unity.ugui/Runtime/UGUI/EventSystem/EventSystem.cs:530)

受信結果

result

おわりに

Twilio API を使用することで、非常に簡単に SMS 送信を実装することができることが分かりました。今後も Twilio+Unity の組合せによる実装を紹介していく予定です。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.