Skip to content

Instantly share code, notes, and snippets.

@dmongeau
Last active December 22, 2015 14:49
Show Gist options
  • Save dmongeau/6488232 to your computer and use it in GitHub Desktop.
Save dmongeau/6488232 to your computer and use it in GitHub Desktop.
Répondeur avec Twilio
<?='<?xml version="1.0" encoding="UTF-8"?>'?>
<Response>
<Say language="fr-CA">Ceci est la boîte vocale de Folklore. Laissez-nous un message</Say>
<Record timeout="10" playBeep="true" maxLength="300" method="POST" action="record.php" />
</Response>
define(
[
'jquery','underscore','backbone'
],
function(
$,_,Backbone
) {
var MessageModel = Backbone.Model.extend({
});
return MessageModel
});
<?php
class Message extends Eloquent {
protected $table = 'messages';
protected $guarded = array();
protected $fillable = array('filename', 'duration', 'from', 'city');
protected $hidden = array('from','city','id','created_at','updated_at');
public static function grabAudioFile($url) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
$content = curl_exec($c);
curl_close($c);
//Get file property
$basePath = public_path().'/files/audio/';
$folder = date('Y-m-d').'/';
$fileName = uniqid().'.mp3';
//Create directory if not exists
if(!file_exists($basePath.$folder)) {
mkdir($basePath.$folder,0775,true);
}
file_put_contents($basePath.$folder.$fileName,$content);
return $folder.$fileName;
}
}
define(
[
'jquery','underscore','backbone',
'models/message'
],
function(
$,_,Backbone,
MessageModel
) {
var MessagesCollection = Backbone.Collection.extend({
model : MessageModel
});
return MessagesCollection
});
define([
'jquery','underscore','backbone',
'collections/messages'
],
function(
$, _, Backbone,
MessagesCollection
) {
var PlayerView = Backbone.View.extend({
options: {
},
events: {
'click' : 'clickPlay',
'touchstart' : 'touchstart',
'touchend' : 'touchend'
},
playing: false,
currentIndex: 0,
currentSound: null,
initialize: function() {
this.collection = new MessagesCollection();
this.collection.reset(this.options.messages || []);
},
render: function() {
},
playNext: function() {
this.$el.addClass('playing');
var sound = this.collection.at(this.currentIndex);
this.currentIndex = this.currentIndex == this.collection.length-1 ? 0:(this.currentIndex+1);
this.currentSound = soundManager.createSound({
url: '/files/audio/'+sound.get('filename'),
autoPlay: true,
onplay: _.bind(function() {
this.playing = true;
},this),
onfinish: _.bind(function() {
this.stop();
if(!Modernizr.touch && this.currentIndex != 0) {
this.playNext();
}
},this)
});
_gaq.push(['_trackPageview','/ete/'+sound.get('filename')]);
},
stop: function() {
this.playing = false;
this.$el.removeClass('playing');
if(this.currentSound) {
this.currentSound.stop();
this.currentSound.destruct();
this.currentSound = null;
}
},
clickPlay: function(e) {
e.preventDefault();
if(Modernizr.touch) {
return;
}
if(this.$el.is('.playing')) {
this.stop();
return;
}
if(this.currentSound) {
this.stop();
}
if(App.soundReady) {
this.playNext();
} else {
this.listenOnce(App,'sound:ready',this.playNext);
}
},
touchstart: function() {
this.$el.addClass('playing');
if(this.$el.is('.playing')) {
this.stop();
return;
}
if(this.currentSound) {
this.stop();
}
if(App.soundReady) {
this.playNext();
} else {
this.listenOnce(App,'sound:ready',this.playNext);
}
},
touchend: function() {
this.$el.removeClass('playing');
}
});
return PlayerView;
});
<?php
$input = Input::all();
if(isset($input['RecordingDuration']) && (int)$input['RecordingDuration'] > 1) {
//Grab audio file
$filename = Message::grabAudioFile($input['RecordingUrl'].'.mp3');
//Save message
$item = new Message();
$item->fill(array(
'filename' => $filename,
'duration' => (int)$input['RecordingDuration'],
'from' => $input['From'],
'city' => $input['FromCity']
));
$item->save();
}
?>
<?='<?xml version="1.0" encoding="UTF-8"?>'?>
<Response>
<Hangup/>
</Response>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment