Handle Media files attached to MMS and store it in database (laravel)

Published

I have a controller that handle the files attached to MMS, When I send SMS the controller handle it very good, But when I send MMS with a file it does not work and give me error 500 ( internal server error, I test my code and i think that the problem lie in this part

for ($i=0; $i < $NumMedia; $i++) {
            $mediaUrl = $request->input("MediaUrl$i");
            $MIMEType = $request->input("MediaContentType$i");
            $fileExtension = $converter->toExtension($MIMEType);
            $mediaSid = basename($mediaUrl);

            $media = $mediaService->getMediaContent($mediaUrl);

            $filename = "$mediaSid.$fileExtension";

            $mediaData = compact('mediaSid', 'MessageSid', 'mediaUrl', 'media', 'filename', 'MIMEType');
            $mmsMedia = new MMSMedia($mediaData);
            $mmsMedia->save();
        }

the whole controller is:-

<?php

namespace AppHttpControllers;

use AppHttpControllersController;
use AppMMSMedia;
use AppServicesMediaMessageServiceIMediaMessageService;
use IlluminateFoundationAuthAuthenticatesUsers;
use IlluminateHttpRequest;
use IlluminateHttpResponse;
use MagyarjetiMimeTypesMimeTypeConverter;
use TwilioRestClient;
use TwilioTwiMLMessagingResponse;

class MessagingController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Messaging Controller
    |--------------------------------------------------------------------------
    |
    | This controller receives messages from Twilio and makes the media available
    | via the /images url.
    */

    protected $twilio;
    protected $accountSid;
    protected $twilioNumber;
    protected $twilioNumberSid;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->accountSid = env('TWILIO_SID');
        $this->twilioNumber = env('TWILIO_NUMBER');
        $this->twilioNumberSid = env('TWILIO_NUMBER_SID');
        $authToken = env('TWILIO_AUTH_TOKEN');

        $this->twilio = new Client($this->accountSid, $authToken);
    }

    public function handleIncomingSMS(Request $request, IMediaMessageService $mediaService)
    {
        $converter = new MimeTypeConverter;
        $NumMedia = (int)$request->input('NumMedia');
        $FromNumber = $request->input('From');
        $MessageSid = $request->input('MessageSid');

        for ($i=0; $i < $NumMedia; $i++) {
            $mediaUrl = $request->input("MediaUrl$i");
            $MIMEType = $request->input("MediaContentType$i");
            $fileExtension = $converter->toExtension($MIMEType);
            $mediaSid = basename($mediaUrl);

            $media = $mediaService->getMediaContent($mediaUrl);

            $filename = "$mediaSid.$fileExtension";

            $mediaData = compact('mediaSid', 'MessageSid', 'mediaUrl', 'media', 'filename', 'MIMEType');
            $mmsMedia = new MMSMedia($mediaData);
            $mmsMedia->save();
        }

        $response = new MessagingResponse();
        $messageBody = $NumMedia == 0 ? 'Send us an image!' : "Thanks for the $NumMedia images.";
        $message = $response->message('',[
            'from' => $request->input('To'),
            'to' => $FromNumber
        ]);
        $message->body($messageBody);

        //return (string)$response;
        return response($response->asXML())->header('Content-Type', 'application/xml');
    }

    public function deleteMediaFromTwilio($mediaItem)
    {
        return $this->twilio->api->accounts($this->accountSid)
            ->messages($mediaItem['MessageSid'])
            ->media($mediaItem['mediaSid'])
            ->delete();
    }

    public function allMedia()
    {
        $mediaItems = MMSMedia::all();
        return $mediaItems;
    }

    public function getMediaFile($filename, Response $response)
    {
        $media = MMSMedia::where('filename', $filename)->firstOrFail();
        $fileContents = $media['media'];
        $MessageSid = $media['MessageSid'];
        $mediaSid = $media['mediaSid'];
        $MIMEType = $media['MIMEType'];

        $media->delete();
        $this->deleteMediaFromTwilio(compact('mediaSid', 'MessageSid'));

        return response($fileContents, 200)
            ->header('Content-Type', $MIMEType);
    }

    public function config()
    {
        return ['twilioNumber' => $this->twilioNumber];
    }
}

Any ideas about it?

Source: Laravel

Published
Categorised as laravel, twilio Tagged ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq