Chat with C# and ASP.NET MVC
The Twilio Conversations API is now Generally Available! We recommend using Conversations to build your chat application to get access to additional channels and even more functionality.
Ready to implement a chat application using Twilio Chat?
This application allow users to exchange messages through different channels, using the Twilio Chat API. With this example, we'll show you how to use this API to manage channels and their usages.
Properatiは不動産の買い手と売り手をリアルタイムにつなぐ手助けをするモバイルメッセージングAppを構築しました。 さらに詳しく。
For your convenience, we consolidated the source code for this tutorial in a single GitHub repository. Feel free to clone it and tweak as required.
トークンの生成
In order to create a Twilio Chat client, you will need an access token. This token provides access for a client (such as a Javascript front end web application) to talk to the Twilio Chat API.
新しい Token
を作成し、これに ChatGrant
を提供することにより、このトークンを生成します。 Token
がある場合は、そのメソッド ToJwt()
を使用して、その文字列表現を返すことができます。
We can generate a token, now we need a way for the chat app to get it.
トークン生成コントローラー
On our controller we expose an endpoint that provides a valid token. Using the parameter:
identity
:ユーザー自身を識別します。
tokenGenerator.Generate
メソッドを使用して新しいトークンを入手し、クライアントに対して使用される JSON フォーマットでトークンを返します。
Now that we have a route that generates JWT tokens on demand, let's use this route to initialize our Twilio Chat Client.
Chatクライアントを初期化する
クライアントで、エンドポイントに対する POST リクエストを使用して新しいトークンを取得します。
And with the token we can instantiate a new Twilio.AccessManager
that is used to initialize our Twilio.Chat.Client
.
Now that we've initialized our Chat Client, lets see how we can get a list of channels.
Channel一覧を取得する
After initializing the client, we can now call it's method getChannels
to retrieve all visible channels. The method returns a promise as a result that we use to show the list of channels retrieved on the UI.
Next, we need a default channel.
一般チャンネルに参加する
This application will try to join a channel called "General Channel" when it starts. If the channel doesn't exist, we'll create one with that name. The scope of this example application will show you how to work only with public channels, but the Chat client allows you to create private channels and handle invitations.
アプリケーションを起動するたびに一般チャンネルを新たに作成したいわけではないので、一般チャンネルに一意の名前を設定します。
ここで、Channelイベントをいくつかリッスンしてみましょう。
チャンネルイベントをリスニングする
With access to the channel objects we can use them to listen to a series of events. In our case, we're setting listeners to the following events:
messageAdded
:接続しているチャンネルに他のメンバーがメッセージを送信する場合。typingStarted
:接続しているチャンネルで他のメンバーがメッセージを入力している場合。typingEnded
:接続しているチャンネルで他のメンバーがメッセージの入力を中止した場合。memberJoined
:接続しているチャンネルに他のメンバーが参加する場合。memberLeft
:接続しているチャンネルから他のメンバーが退出する場合。
Here, we just register a different function to handle each particular event.
クライアントも同様にイベントを生成します。 このイベントを同様にリッスンできるようにする方法を見ていきましょう。
クライアントイベントをリスニングする
Just like with channels, we can register handlers for events on the Client:
channelAdded
:チャンネルがクライアントに表示されるようになった場合。channelRemoved
:チャンネルがクライアントに表示されなくなった場合。tokenExpired
:提供されたトークンの有効期限が切れた場合。
ここで実際のチャットアプリケーションが用意できましたが、複数Channelを使用してもっと面白いものを作ってみましょう。
チャンネルを作成する
To create a new channel, the user clicks on the "+ Channel" link. That we'll show an input text field where it's possible to type the name of the new channel. The only restriction here, is that the user can't create a channel called "General Channel". Other than that, creating a channel is as simple as calling createChannel
with an object that has the friendlyName
key. You can create a channel with more options though, see a list of the options here.
続いて、Channel間を切り替える方法を見ていきましょう。
他のChannelに参加する
サイドバーからチャンネルの名前をタップすると、そのチャンネルが selectedChannel
として設定されます。selectChannel
メソッドは選択したチャンネルへの参加および selectedChannel
の設定を行います。
いつか、ユーザーはChannelを削除したいと思うでしょう。 これを行う方法を見ていきましょう。
チャンネルを削除する
チャネルの削除は、追加よりも簡単です。 アプリケーションは、"delete current channel"リンクによって、ユーザーにチャネルを削除できるようにします。Twilioから実際にチャネルを削除するのに必要なことは、削除しようとしているチャネルの delete
メソッドを呼ぶことだけです。 'Channel'オブジェクトの他のメソッドと同様、successハンドラーを設定できるプロミスを返します。
一丁あがり! ASP.NET MVC を使用して C# の簡単なチャットアプリケーションを実装できました。
次はどこでしょうか?
Twilio を使う C# 開発者であれば、他のチュートリアルも調べてみてください。
もう 1 つのサーバー停止状態も見逃さないでください。サーバー停止状態が発生した場合に SMS 経由ですべての管理者に警告するサーバー通知システムを構築する方法を学びます。
ビジネスに重要なワークフローを自動化することで、レスポンスレートを上げます。このチュートリアルでは、貸し別荘会社のスケール可能な状態の自動 SMS ワークフローを構築する方法を学びます。
音声通話およびSMSを使い匿名でユーザー同士を接続しプライバシーを保護します。使い捨ての電話番号をオンデマンドで用意し、2人のユーザーが電話番号など個人情報を交換しなくても通話できる方法を学びます。
これは役に立ちましたか?
このチュートリアルをお読みいただき、ありがとうございます。ご意見やご感想などございましたら、ぜひお聞かせください。@twilio へツイートいただき、ご意見をお知らせください。
ヘルプが必要ですか?
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。