0
votes

I'm using a python3 flask REST-ful application to control my twilio-based phone services. Everything is working very well, but I have a question for which I haven't been able to find an answer.

When I want to redirect a caller to voicemail, I call the following voice_mail function from my REST interface, and I manage the voicemail via a twimlet, as follows ...

def voice_mail():
    vmbase = 'http://twimlets.com/[email protected]&Transcribe=False'
    vmurl = '... URL pointing to an mp3 with my voicemail greeting ...'
    return redirect(
        '{}&Message={}'.format(vmbase, vmurl),
        302
    )

This works fine, but there doesn't seem to be any way to control how long the caller's voicemail message can last. I'd like to put an upper limit on that duration.

Is there any way via this twimlet (or perhaps a different twimlet) to force the voicemail recording to be cut off after a configurable amount of time?

If not, is there a default maximum duration of this twimlet-based voicemail recording?

And if neither of these are available, can someone point me to a python-based way of using other twilio features to ...

  1. Route the caller to voice mail
  2. Play a specified recording
  3. Capture the caller's message.
  4. Cut off the caller's recording after a configurable number of seconds.
  5. Email a notification of the call and the URL of the recorded message to a specified email address.

I know that the existing twimlet performs items 1, 2, 3, and 5, but I don't know how to implement item 4.

Thank you.

1
I just found the following, which indicates that such a time limit is not available within twimlets: stackoverflow.com/questions/32911651/…. So, I guess I will have to implement this myself. But before I "re-invent the wheel", has anyone already implemented this functionality in python? The complicated part is generating the email with the transcribed message.HippoMan

1 Answers

0
votes

Given that the voicemail twimlet doesn't allow a recording time limit to be specified, I was able to solve this without a twimlet in the following manner. And the sending of email turns out to be simple via the flask_mail package.

The following code fragments show how I did it ...

import phonenumbers
from flask import Flask, request, Response, url_for, send_file
from flask_mail import Mail, Message

app = Flask(__name__)

mail_settings = {
    'MAIL_SERVER'   : 'mailserver.example.com',
    'MAIL_PORT'     : 587,
    'MAIL_USE_TLS'  : False,
    'MAIL_USE_SSL'  : False,
    'MAIL_USERNAME' : 'USERNAME',
    'MAIL_PASSWORD' : 'PASSWORD'
}

app.config.update(mail_settings)
email = Mail(app)

# ... etc. ...

def voice_mail():
    vmurl = '... URL pointing to an mp3 with my voicemail greeting ...'
    resp = VoiceResponse()
    resp.play(vmurl, loop=1)
    resp.record(
        timeout=5,
        action=url_for('vmdone'),
        method='GET',
        maxLength=30, # maximum recording length
        playBeep=True
    )
    return Response(str(resp), 200, mimetype='application/xml')

@app.route('/vmdone', methods=['GET', 'POST'])
def vmdone():
    resp    = VoiceResponse()
    rcvurl  = request.args.get('RecordingUrl',      None)
    rcvtime = request.args.get('RecordingDuration', None)
    rcvfrom = request.args.get('From',              None)
    if not rcvurl or not rcvtime or not rcvfrom:
        resp.hangup()
        return Response(str(resp), 200, mimetype='application/xml')
    rcvurl  = '{}.mp3'.format(rcvurl)
    rcvfrom = phonenumbers.format_number(
        phonenumbers.parse(rcvfrom, None),
        phonenumbers.PhoneNumberFormat.NATIONAL
    )
    msg = Message(
        'Voicemail',
        sender='[email protected]',
        recipients=['[email protected]']
    )
    msg.html = '''
<html>
<body>
<p>Voicemail from {0}</p>
<p>Duration: {1} sec</p>
<p>Message: <a href="{2}">{2}</a></p>
</body>
</html>
'''.format(rcvfrom, rcvtime, rcvurl)
    email.send(msg)
    return Response(str(resp), 200, mimetype='application/xml')

Also, it would be nice if someone could add a parameter to the voicemail twimlet for specifying the maximum recording duration. It should be as simple as using that parameter's value for setting maxLength in the arguments to the record() verb.

If someone could point me to the twimlets source code, I'm willing to write that logic, myself.