Employee Directory with Node.js and Express
SMSを使用して照会できる従業員目録の実装方法を学びます。 貴社への情報のリクエストはTwilio電話番号にテキスト・メッセージを送信するだけで行えます。
ここでは、高いレベルでどのように機能するのかについて示します。
- エンドユーザーはTwilio電話番号に従業員の名前の入ったSMSを送信します。
- エンドユーザーはリクエストされた従業員の情報を受信します。
従業員モデルを作成する
まず必要になるのは従業員のコレクションです。 この目的にはMongooseを使用します。
従業員のエンティティには、連絡先情報のフィールドが 2 ~ 3 個あります。この情報には、従業員の名前、電話番号、および画像を含むパブリック URL などが含まれます。
Now that we have a model that represents an employee, let's see how to search for employees by name.
従業員を名前で検索する
employee-finder
モジュールを使用すると、名前ごと、あるいは一意なデータベースの識別子ごとに従業員をデータベースで検索することができます。 名前で検索を行うと、名前が検索クエリーに一致する可能性のある従業員の一覧を返します。 従業員IDが分かっている場合は、それを探し出して即座に返すことができます。
Now, let's use this search functionality when responding to an SMS from a user.
着信 SMS を受信する
When your number receives an SMS message, Twilio will send an HTTP POST request to our application. This will be handled by the /directory/search/
route.
We check for the cookie and numeric input (line 15/more on that later) or perform a query for the desired employee. The results are packaged up as a TwiML response through the twiml-generator
module and sent back to Twilio and, in turn, the original sender of the SMS.
Now that we have our search route, let's see how we can request a specific employee by name.
Respond with a Single Match for an Employee Name
Let's say it finds a single employee matching the text message. In this case, we simply write out a response that contains the employee's contact information, including a photo, making our response a MMS message.
ただし、従業員の単一照合が唯一のシナリオではありません。
As you can see, The Twilio Node.js Helper Library simplifies the way you can generate and send SMS messages. Together with a simple database query, the application is able to return valuable information over SMS. Let's see how we handle multiple or no results next.
Handle Multiple or No Results
従業員が見つからない場合は、「見つかりませんでした」というメッセージを返すだけです。
複数一致についてはどうでしょうか。この場合は、一致する従業員名の一覧を、ユーザーが選択を行えるよう通し番号を伴って返すことにします。 たとえば、"Man"という検索を行った場合は、以下のような結果が返る可能性があります:
We found: 1-Spider-Man, 2-Iron Man
- Reply with # of desired person
Let's see how these options are stored next.
Cache the List of Possible Matches
ユーザーに返されるメッセージテキストについて、一致候補の番号付きメニューを構築します。
アプリでは、ユーザーからの SMS メッセージ間で、1、2、3 の選択番号が従業員の実際の一意の ID にマッピングされていることを把握している必要があります。これらは cookie に入れられ、Twilio がアプリケーションに HTTP リクエストを送信するたびに一緒に送り返されます。
When the user that queried the employee directory receives the message with a list of employees, they will text back a number that corresponds to the result on the list that they are interested in querying further. Twilio will send a request to the webhook which handles incoming SMS messages. At this point, our app will try to parse the user's message to determine what to do next.
Return Employee's Contact Information by Number Choice
SMS メッセージを受信したら、以下について確認します。
- テキストの本文は実のところ数字です。
- ID への番号のマッピングに Cookie が存在する。
これらの確認のいずれかに失敗した場合は、単純に、通常の名前検索を進めます。
それ以外の場合、選択されたオプションがクッキー内に存在するか確認します。それが存在する場合は、選択肢と一致する単一の従業員を返します。
後は、成功を祝うだけです。
Expressを使って、従業員目録を実装しました。 これで、Twilio電話番号にSMSを送ることで、従業員情報を取得できるようになりました。
次はどこでしょうか?
Twilio を使う Node.js 開発者であれば、他のチュートリアルもお楽しみください。
Twilio クライアントを使って、ブラウザーから電話へ、およびブラウザーからブラウザーへの通話を簡単に行う方法を学びます。
ExpressとTwilioを使用して、到着予定通知の実装方法を学びます。
これは役に立ちましたか?
このチュートリアルをお読みいただき、ありがとうございます。ご意見やご感想などございましたら、ぜひお聞かせください。 ご意見やご感想などございましたら、ぜひお聞かせください。 @twilioにツイートして、ご意見をお寄せください!
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。