public IActionResult Create(Friend friend)
        {
            ModelState.Clear();
            TryValidateModel(friend);
            if (ModelState.IsValid)
            {
                using (var db = new FriendsContext())
                {
                    // Create a channel
                    var channel = _client.CreateChannel(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), "public", friend.PhoneNumber, friend.PhoneNumber, string.Empty);

                    // join this channel
                    if (channel.RestException != null)
                    {
                        //an exception occurred making the REST call
                        return(Content(channel.RestException.Message));
                    }
                    else
                    {
                        // Create a user
                        var user = _client.CreateUser(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), friend.PhoneNumber.RemoveSpecialCharacters());
                        if (user.RestException != null)
                        {
                            return(Content(user.RestException.Message));
                        }
                        else
                        {
                            // Create membership
                            var member = _client.CreateMember(Environment.GetEnvironmentVariable("TWILIO_IPM_SERVICE_SID"), channel.Sid, user.Identity, string.Empty);
                            if (member.RestException != null)
                            {
                                return(Content(member.RestException.Message));
                            }
                            else
                            {
                                // Add complete user to the DB
                                friend.ChannelSid = channel.Sid;
                                friend.UserSid    = user.Sid;
                                db.Friends.Add(friend);
                                db.SaveChanges();
                            }
                        }
                    }
                    return(RedirectToAction("Index"));
                }
            }
            return(View(friend));
        }
        public async Task <dynamic> GetOrCreateCaseChannel(int caseId)
        {
            var client = new IpMessagingClient(settings.Account.Sid, settings.AuthToken);

            var friendlyName = $"Messaging channel to discuss case ID = {caseId}";
            var uniqueName   = $"case{caseId}";
            var channel      = client.GetChannel(settings.IpMessaging.Service.Sid, uniqueName);

            if (channel.RestException?.Status == "404")
            {
                // if channel doesn't exist, create it
                channel = client.CreateChannel(settings.IpMessaging.Service.Sid, "public", friendlyName, uniqueName, (string)null);
            }

            var model = CreateModel(channel);

            return(model);
        }
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/user/account
        const string accountSid   = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken    = "your_auth_token";
        const string serviceSid   = "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string channelType  = "public";
        const string friendlyName = "MyChannel";
        const string uniqueName   = "my-channel";
        const string attributes   = "";

        // Create a channel
        var     client  = new IpMessagingClient(accountSid, authToken);
        Channel channel = client.CreateChannel(serviceSid,
                                               channelType,
                                               friendlyName,
                                               uniqueName,
                                               attributes);

        Console.WriteLine(channel);
    }