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));
        }
Example #2
0
    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 channelSid = "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string identity   = "identity";
        const string roleSid    = "RLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

        // Add a Member
        var    client = new IpMessagingClient(accountSid, authToken);
        Member member = client.CreateMember(serviceSid, channelSid, identity, roleSid);

        Console.WriteLine(member);
    }