メニュー

Expand
ページを評価:

Programmable SMS Quickstart for C# with .NET Core

Looking for .NET Framework? We have a quickstart for that too!

With just a few lines of code, your .NET Core application can send and receive text messages with Twilio Programmable SMS.

This C# SMS Quickstart will teach you how to do this using our Communications REST API and the Twilio helper library for .NET Core.

このクイックスタートでは、下記のことを学んでいきます:

  1. Twilioにサインアップして、SMS機能を持ったはじめてのTwilio電話番号を入手する
  2. メッセージを送受信できるように開発環境をセットアップする
  3. 最初のSMSを送信する
  4. テキストメッセージを受信する
  5. 受信メッセージに対してSMSで返信する

ビデオを見ながらの入門がお好みですか? YouTube上のC# SMSクイックスタートビデオ(英語)をご覧ください。

Show me how it's done!

Already have a Twilio account? Go ahead and skip this section.

You can sign up for a free Twilio trial account here.

  • When you sign up, you'll be asked to verify your personal phone number. This helps Twilio verify your identity and also allows you to send test messages to your phone from your Twilio account while in trial mode.
  • Once you verify your number, you'll be asked a series of questions to customize your experience.
  • Once you finish the onboarding flow, you'll arrive at your project dashboard in the Twilio Console. This is where you'll be able to access your Account SID, authentication token, find a Twilio phone number, and more.
I've got an account! What's next?

Install the Twilio CLI

We'll need to use the Twilio CLI (command line interface) for a few tasks, so let's install that next.

One of the easiest ways to install the CLI on Mac OS X is to use Homebrew. If you don't already have it installed, visit the Homebrew site for installation instructions and then return here.

Once Homebrew is installed, simply run the following command to install the CLI:

brew tap twilio/brew && brew install twilio

Updating

If you already installed the CLI with brew and want to upgrade to the latest version, run:

brew upgrade twilio

Warning for Node.js developers

If you have installed Node.js version 10.12 or higher on your Mac, you can avoid potential Node.js version conflicts by installing the CLI using npm:

npm install twilio-cli -g

Before we can install, we need to make sure you have Node.js installed (version 10.12 or above). To see if you have node installed, try running this command:

node -v

If your system reports v10.12.0 or above, you can skip the next step.

Installing Node.js on Windows

Using the Windows Installer (.msi) is the recommended way to install Node.js on Windows. You can download the installer from the Node.js download page.

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.

Updating

If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Before we can install, we need to make sure you have Node.js installed (version 10.12 or above). Even if you already installed Node yourself, the CLI works best when you install it using nvm. Here's how to get nvm installed on most Linux systems:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash

Please visit the nvm installation instructions for additional options and troubleshooting steps. Once you have nvm installed, run the following to install and use the most recent LTS release of Node.js:

nvm install --lts
nvm use <insert version reported from above>

Installing other Twilio CLI prerequisites for Linux

Depending on your distribution, you will need to run one of the following commands:

  • Debian/Ubuntu: sudo apt-get install libsecret-1-dev
  • Red Hat-based: sudo yum install libsecret-devel
  • Arch Linux: sudo pacman -S libsecret

Installing Twilio CLI

The CLI is installed with npm (Node Package Manager), which comes with Node.js. To install the CLI run the following command:

npm install twilio-cli -g

Note the -g option is what installs the command globally so you can run it from anywhere in your system.

Updating

If you already installed the CLI with npm and want to upgrade to the latest version, run:

npm install twilio-cli@latest -g

Run twilio login to get the Twilio CLI connected to your account. Visit https://www.twilio.com/console, and you’ll find your unique Account SID and Auth Token to provide to the CLI.

認証トークンは、目玉アイコンをクリックすると表示されます:

認証トークンを表示する

Now to get a phone number...

Get a phone number

If you don't currently own a Twilio phone number with SMS functionality, you'll need to purchase one. With the CLI, run:

twilio phone-numbers:buy:local --country-code US --sms-enabled

Replace US with your ISO-3166-1 country code if you would like a phone number in another country. If you aren't finding any SMS enabled numbers, try looking for a mobile number instead of a local number: twilio phone-numbers:buy:mobile --country-code DE --sms-enabled

Select a phone number to add it to your account.

Next, we need to install .NET Core and the Twilio C# Helper Library.

バッチリです! セットアップ方法を教えてください。

Install .NET Core

You can check if you already have .NET Core installed on your machine by opening up a command prompt or terminal and running the following command:

dotnet --version

You should see something like 2.1.3. If you receive an error message, you can download .NET Core from Microsoft and install it.

Create a new project and add the Twilio NuGet package

Run these commands to create a new .NET project and install the Twilio NuGet package:

mkdir TwilioSend
cd TwilioSend
dotnet new console
dotnet add package Twilio
All set! Let's send a text message.

C#を使用してSMSメッセージを送信する

Now that we have .NET Core and the Twilio .NET NuGet package installed, we can send an outbound text message from the Twilio phone number we just purchased with a single API request. Open the file in your new project called Program.cs and type or paste in this code sample, replacing the template code that's already there.

        
        
        
        
        This code creates a new instance of the Message resource.

        C#を使用してSMSを送信する

        This code creates a new instance of the Message resource.

        メッセージの送信前に、このファイルを少々編集する必要があります:

        プレースホルダーのクレデンシャル値を置き換える

        Swap the placeholder values for accountSid and authToken with your personal Twilio credentials. Go to https://www.twilio.com/console and log in. On this page, you’ll find your unique Account SID and Auth Token, which you’ll need any time you send messages through the Twilio Client like this. You can reveal your auth token by clicking on the 'view' link:

        Reveal your Auth Token in the Twilio Console

        Program.csを編集し、accountSidおよびauthTokenに対応する値をご使用の一意な値に置き換えます。

        Please note: it's okay to hardcode your credentials when getting started, but you should use configuration to keep them secret before deploying to production. We've written blog posts on how to secure user secrets in a .NET Core Web App and a .NET Core Console App that should provide you with some good guidance.

        Replace the "from" phone number

        数分前に購入した、SMS機能対応の電話番号を覚えていますか? どうぞ、既存のfrom番号をご自身の番号に置き換えてください。 また、E.164形式が使用されていることも確認します。

        [+][country code][phone number including area code]

        Replace the "to" phone number

        Replace the to phone number with your mobile phone number. This can be any phone number that can receive text messages, but it’s a good idea to test with your own phone, so you can see the magic happen! As above, you should use E.164 formatting for this value.

        トライアルアカウントをご使用の場合、Twilioで検証済みの電話番号に送信先が制限されます。電話番号はTwilioコンソール内の検証済み発信者番号にて検証できます。 

        Save your changes and run this code:

        dotnet run
        

        一丁上がり! ほどなくして、Twilio電話番号から携帯電話にSMSが受信されるはずです。

        Are your customers in the U.S. or Canada? You can also send them MMS messages by adding just one line of code. Check out this sending MMS tutorial to see how it's done.

        メッセージを送信しました! どのように受信しますか?

        Receive and reply to inbound SMS messages with ASP.NET Core

        When your Twilio number receives an incoming message, Twilio will send an HTTP request to a server you control. This callback mechanism is known as a webhook. When Twilio sends your application a request, it expects a response in the TwiML XML format telling it how to respond to the message. Let's see how we would build this in C# using ASP.NET Core.

        Create a new ASP.NET Core project

        Run these commands to create a new ASP.NET Core project and install the Twilio NuGet package:

        mkdir TwilioReceive
        cd TwilioReceive
        dotnet new mvc
        dotnet add package Twilio.AspNet.Core
        

        新規コントローラーを作成する

        Controllersという名前のディレクトリーで、SmsController.csという新規コントローラーを作成し、下記のコードを使用して受信メッセージを処理できるサーバーを作成します。

              
              
              
              
              電話番号にメッセージが着信すると、TwilioはあなたのサーバーにHTTPリクエストを送信します。 このコードは(TwiMLを使って)テキストメッセージにどうサーバーが返信すべきかを表しています。

              C#でSMSやMMS(日本未対応)を受信、返信する

              電話番号にメッセージが着信すると、TwilioはあなたのサーバーにHTTPリクエストを送信します。 このコードは(TwiMLを使って)テキストメッセージにどうサーバーが返信すべきかを表しています。

              Save the file, then run your application with:

              dotnet run
              

              In your favorite browser, open the URL https://localhost:5001/sms.

              If all went well, you should see XML in your browser with the message we'd like to reply with to all our inbound texts. And, yes, that's all the code you need - there's just one more step before everything is wired up.

              I'm getting excited now - let's connect our code to Twilio!

              WebhookのURLを設定する

              Now, you need to configure your Twilio phone number to call your webhook URL whenever a new message comes in. Just run this CLI command, replacing the phone number with your own Twilio phone number:

              twilio phone-numbers:update "+15017122661" --sms-url="https://localhost:5001/sms"
              

              The CLI will start an ngrok tunnel (so Twilio can reach your development machine) and wait patiently for incoming text messages!

              SMSを送信しアプリケーションをテストする

              Make sure your project is running and your ngrok tunnel is running.

              With both of those servers running, we’re ready for the fun part - testing our new ASP.NET Core SMS application!

              Send an SMS from your mobile phone to your Twilio phone number that's configured with this webhook. Your ASP.NET Core app will process the text message, and you’ll get your response back as an SMS.

              It worked! All done - what's next?

              関連トピック

              C#および.NETを使用したSMSおよびMMSの送受信の基本について理解できたところで、下記の資料について調べてみるのも良いかもしれません。

              楽しいプログラミングを!

              ページを評価:

              ヘルプが必要ですか?

              誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。

                    
                    
                    

                    フィードバックくださりありがとうございます!

                    We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

                    Sending your feedback...
                    🎉 Thank you for your feedback!
                    Something went wrong. Please try again.

                    Thanks for your feedback!

                    Refer us and get $10 in 3 simple steps!

                    ステップ1

                    Get link

                    Get a free personal referral link here

                    ステップ2:

                    Give $10

                    Your user signs up and upgrade using link

                    ステップ3

                    Get $10

                    1,250 free SMSes
                    OR 1,000 free voice mins
                    OR 12,000 chats
                    OR more