C++でSMSやMMS(日本未対応)を送信する
Today we'll use C++, some standard libraries, libcurl, and some POSIX APIs to send SMS and MMS messages using the Twilio REST API. You'll need a (mostly) POSIX-compliant build environment as well as libcurl to follow along with this guide.
はじめましょう!
Sign Up for (or Sign In to) a Twilio Account
You'll need to login to your existing Twilio account, or create a new account.
Don't have an account yet? Sign up for a free Twilio trial, post-haste.
SMSを送信可能な電話番号を購入または検索する
You'll need a SMS (or optionally, MMS) capable number to complete this demo. Either use a phone number that you already have, or purchase a new one with SMSes enabled.
購入した電話番号で行えることは、Twilioコンソールのこちらから決めることができます。
If you need to purchase a number, after navigating to the 'Buy a Number' link click the SMS checkbox (and the MMS checkbox, if you wish to send media messages).
Prerequisites for This C++ MMS and SMS Guide
We're targeting POSIX Compliant environments with this guide, and tested our code with clang-800.0.42.1 on Mac OSX 10.11, gcc version 4.9.2 on Raspbian 8.0 (kernel 4.4.38-v7+) and gcc 5.4.0 in Cygwin 2.877 on Windows 7 64-Bit.
To build, you will need to ensure you have:
- stdio.h, stdlib.h, unistd.h - C POSIXライブラリー
- libcurl - Client-side transfer library
- iostream, sstream, string - C++標準ライブラリー
libcurl was already on the Mac. On the Raspberry Pi, you can install a suitable version with:
sudo apt-get install libcurl4-openssl-dev
Windows 7とCygwinでは、Cygwinセットアップでlibcurlをインストールできます。
Building cpp_demo
All three environments worked with this example Makefile. You may need to make minor edits - usually to the library search paths - to build in your environment.
On almost all *NIX-type systems (including Windows/Cygwin), building should be very similar to the following lines (you might even be able to copy, paste and execute):
git clone https://github.com/TwilioDevEd/twilio_cpp_demo.git cd twilio_cpp_demo make
Send an SMS or MMS Message with C++
We've just built cpp_demo, which attempts to send an SMS or MMS based on your inputs from the command line. src/twilio.cc
and include/twilio.hh
demonstrate a class, Twilio, which should be easy to integrate into your own codebase. (Note that before putting it into production, you need to add input validation and safety. The first order of concern: the code in its current form can be passed additional HTTP parameters through the command line inputs.)
C++開発者には、CによるSMSおよびMMSガイドでは下記のコードについてのいくつかの重要な洞察が得られるでしょう - しかし舞台裏で何が起こっているのか詳しく見てみることにしましょう。
- First, we instantiate a
Twilio
object with our account credentials (find them on the Twilio Console). - Second, we call the method
send_message
with the to/from numbers, the message, and optionally a URL to a picture and a boolean of whether we want verbose output.
- Third, once inside the code we first do some exception checking (making sure the message is the proper size), followed by setting up libcurl with our eventual HTTP POST
- Fourth, curl makes the HTTP POST and we clean up with
curl_easy_cleanup
. - 最後に、成功か失敗かを得るためにレスポンスを確認し、呼び出し側の関数に結果を返します。
Running cpp_demo
Running cpp_demo from the command line is easy once you have it built. Merely set your account credentials and call the code as follows:
account_sid=ACXXXXXXXXXXXXXXXXXXXXXX auth_token=your_auth_token bin/cpp_demo -a $account_sid -s $auth_token -t "+18005551212" -f "+18005551213" -m "Hello, World!" # Optionally, use '-p http://some/path/to/img.jpg' to send an MMS.
TwilioとC++でC-Riousを入手する
After this demo, you now have the means to add communications to your new or existing C++ applications. Add monitoring or help to your C++ application, or bring your own use case.
Whether you're a master of the language or closer to a practitioner of "C with classes" please do let us know on Twitter when you've got something built!
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。