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)); }
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 identity = "identity"; const string roleSid = "RLXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Create a user var client = new IpMessagingClient(accountSid, authToken); User user = client.CreateUser(serviceSid, identity, roleSid); Console.WriteLine(user); }