MMS Controller
//Reminder this class needs to inherit from TwilioController --> public class MMSController : TwilioController | |
public ActionResult Index(string from, string mediaurl0) | |
{ | |
Image blended = CreateBlendedImage(mediaurl0); | |
//We are using Azure Storage, and loading our credentials from our config file | |
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["<CONNECTIONKEY>"].ToString()); | |
// Create the blob client | |
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); | |
// Retrieve a reference to a container. | |
CloudBlobContainer container = blobClient.GetContainerReference("<CONTAINERNAME>"); | |
//Allow public access to the blob (but not to the container) | |
container.SetPermissions(new BlobContainerPermissions {PublicAccess = BlobContainerPublicAccessType.Blob }); | |
// Create the container if it doesn't already exist. | |
container.CreateIfNotExists(); | |
// Retrieve reference to a blob names using the from phone number. | |
//We add the png file extension so its easier for us when looking at a list of blobs | |
CloudBlockBlob blockBlob = container.GetBlockBlobReference(from + ".png"); | |
// Create or overwrite the phone mumber blob with contents from a local file. | |
//We re using temp file space for file creation | |
var filepath = System.IO.Path.GetTempFileName(); | |
blended.Save(filepath, ImageFormat.Png); | |
//remmeber to set the Content Type, else Azure will return appliction/octet and Twilio won't be able to read the image | |
blockBlob.Properties.ContentType = "image/png"; | |
//Upload the file to Azure storage | |
blockBlob.UploadFromFile(filepath, FileMode.Open); | |
//Create th Twilio TwiML markup response | |
var response = new TwilioResponse(); | |
string[] mediaurls = new string[1]; | |
//For some reason only http worked for me | |
mediaurls[0] = blockBlob.Uri.AbsoluteUri.Replace("https://","http://"); | |
//Add the Blended Image URL from Azure to the response | |
response.Message(mediaurls); | |
//Note how we return a TwiML respone and not a View | |
return TwiML(response); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment