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?

PythonでSMSの会話を作成する

Today we're going to answer a fun question: how do you turn a handful of isolated text messages to and from the same party into a true conversation?

The problem here? SMS is a stateless protocol. You're going to need some way to remember state between each exchanged message.

Luckily for us, building traditional web applications has this same hurdle, as HTTP is also a stateless protocol. This problem has been solved for web applications through the use of HTTP cookies and, rather than reinvent that old wheel, the Twilio Messaging API uses the same solution.

This guide will show you how Programmable SMS makes this easy. We'll use Python, the Flask framework, and the Twilio Python SDK.

PythonによるSMSのWebhook開発がはじめての方には、まず PythonでSMSやMMS(日本未対応)を受信、返信する を先にお読みいただくのもよいでしょう。

準備はよろしいですか? さぁ、はじめましょう!

WebhookでHTTP Cookieを使用する

What is an HTTP Cookie 

Webアプリケーションの場合と同様、Cookieはアプリケーションがユーザー名やアカウントといった情報を追跡するためにTwilioのサーバー上に保存できる軽量なファイルです。

For Twilio Programmable SMS, cookies are scoped to the "conversation" between two parties — you can have a unique cookie for each To/From phone number pair. For example, you can store a unique cookie for any messages sent between 415-555-2222 and 415-555-1111, which will be different than the cookie used between 415-555-3333 and 415-555-1111.

Why Should You Use a Cookie?

In a web app, you write a cookie to keep "statefulness" between separate requests from the same browser.

Similarly, SMS messages are independent communications between two parties, so Twilio allows you to tie them together as a logical session via cookies. This means you can use server-side sessions to keep track of application state between requests. (How cool is that?)

活動がないまま4時間が経過すると、その会話用のCookieはTwilioによって、あたかもユーザーが「ブラウザーを閉じたかのように」期限切れとなります。

会話データを保存する

Cookieを使用すると、複数のメッセージにまたがって状態を共有できるため、別々のメッセージを会話として扱うことができ、将来の参照のために会話に関するデータをCookieに格納することができます。

You can store the data directly in a cookie, or you can use a session state management framework. We'll use Flask's built-in session library for this example.

セッションを用いてSMSの会話を捕捉する

Now let's try using session counters to see if a particular user has messaged us before. If they're a new sender, we'll thank them for the new message. If they've sent us messages before, we'll specify how many messages we've gotten from them.

        
        
        
        

        とっても簡単でしょう?

        If you're looking for a more complex example of using cookies to track a conversation, take a look at our Employee Directory tutorial where we build a full fledged employee directory with Twilio's powerful APIs.

        Andrew Baker Jarod Reyes Paul Kamp David Prothero Kelley Robinson
        Rate this page:

        ヘルプが必要ですか?

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