Level up your Twilio API skills in TwilioQuest, an educational game for Mac, Windows, and Linux. Download Now

メニュー

Expand
Rate this page:

Thanks for rating this page!

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

Send SMS and MMS Messages In Tcl/Tk

Today we'll use Tcl and the Tk graphical toolkit to rapidly prototype a Twilio-powered SMS and MMS sending application. We're going to be using the packages ttk (which will require Tcl 8.5+), tls, base64, and http.

Sign Up for (or Sign Into) a Twilio Account

You'll need to either have an existing Twilio account or create a new account to try out the Tcl code.

Don't have an account yet? Sign up for a free Twilio trial!

SMSを送信可能な電話番号を購入または検索する

You'll need a SMS (or optionally, MMS) capable number to use this demo application. Purchase a new SMS-enabled number or use one that you already own.

From the Twilio Console, click the '#' hash tag to look at Phone Numbers. Any numbers you have purchased will have the capabilities listed in the middle. 'SMS' and 'MMS' enabled numbers will appear with an icon:

SMS機能に対応したTwilio電話番号を確認する

If you need to purchase a number, navigate to the 'Buy a Number' link and click the SMS checkbox (and optionally the MMS checkbox). Hit 'Search' and you should see all of the numbers available.

SMS対応Twilio電話番号を購入する

Prerequisites for This Tcl MMS and SMS Guide

Many POSIX-Compliant operating systems ship with a distribution of Tcl and a number of popular packages. tls, however, may not be installed on your machine - you can access the tls package's site here. Alternatively, ActiveState maintains a number of excellent distributions of Tcl which make it easy to install packages.

TCL packages needed:

  • TLS
  • http
  • base64
  • Tk

We were able to run the demo with the default Tcl8.5 distribution that comes with recent versions of Mac OS X. Here's an example session in bash on Mac OSX 10.11.6:

git clone https://github.com/TwilioDevEd/twilio-tcl-demo.git
cd twilio-tcl-demo
export TWILIO_ACCOUNT_SID=ACXXXXXXXXXXX
export TWILIO_AUTH_TOKEN=yourauthtokengoeshere
export TWILIO_PHONE_NUMBER=+18005551212
chmod 755 twilio.tcl
/usr/bin/tclsh8.5 /twilio.tcl # Or wherever; default 8.5 worked on our Mac

Send an SMS or MMS Message with TCL/Tk

Our sample application shows how to bring up a cross-platform GUI with Tcl and Tk, then quickly send out a text message.

The GUI code is pretty boilerplate, other than the fact we're taking full advantage of ttk widgets.

        
        
        
        
        Grab the 'to' number, message body, and optional image URL for Twilio to send an SMS or MMS

        Set up a Tcl/Tk GUI

        Grab the 'to' number, message body, and optional image URL for Twilio to send an SMS or MMS

        After submitting the form, we first run a function to get the current state of all the GUI elements. Your production application should perform more exhaustive checks than we're demonstrating, but for now we'll just let Twilio reject our out-of-scope requests (like letters instead of phone numbers!).

        Following that, we get to the heart of the SMS and MMS sending code. The send_sms{} procedure should be easily portable to your own application. It takes phone numbers, a message body, Twilio account credentials, and an optional media url as input and POSTS to Twilio's REST API.

              
              
              
              
              Use the tls and http packages to POST to Twilio

              SMSとMMSの送信

              Use the tls and http packages to POST to Twilio

              Whether Twilio sends out an MMS or an SMS depends on the content of the Media URL and the number's home country. Twilio currently supports MMS messages in the United States and Canada. Other countries will have the image appended to the body as an automatically shortened URL.

              Tickle Your Fancy with Twilio and... Tcl

              With the power of Twilio's REST APIs you can quickly integrate communications into your application, no matter the language. Whether that's embedding Twilio connectivity in your appliance, prototyping a desktop support application, SMSing the whole design team when the routes are too scenic or texting you when there's another Tcl flamewar on your favorite newsgroup... we've got your back.

              With Twilio's API and your application, the possibilities are endless.

              We can't wait to see what you build - let us know on Twitter when you've got something to show!

              Paul Kamp
              Rate this page:

              ヘルプが必要ですか?

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