Say "hello" when Twilio call connected - python-3.x

I have a websocket in python Flask that listens to a twilio call. When the call is started I want to say "hello" here is the code.
if data['event'] == "start":
speakBytes = speaker.speak("Hello") // using micrsoft cognitive service to convert the text to bytes
convertedBytes = ap.lin2ulaw(speakBytes.audio_data,1)
ws.send(responseString.format(base64.b64encode(convertedBytes), str(data['streamSid'])))
But the above is not working. I checked microsoft cognitive services speech sunthesizer returns the bytes in WAV format so I have used lin2ulaw form python audioop module.
Need help. Thanks in advance.

Twilio developer evangelist here.
It looks like you are correctly creating the audio to send to the Twilio Media Stream, however I don't think you are sending the correct format.
Twilio Media Streams expect a media message to be a JSON object with the following properties:
event: the value "media"
streamSid: the SID of the stream
media: an object with a "payload" property that then contains the base64 encoded mulaw/8000 audio
Something like this might work:
message = {
"streamSid": data['streamSid'],
"event": "media",
"media": {
"payload": base64.b64encode(convertedBytes)
}
}
# Serializing json
json_object = json.dumps(message)
ws.send(json_object)

If you're using Twilio to connect the number then you'll need to reply with TwiML to the call:
from twilio.twiml.voice_response import VoiceResponse
response = VoiceResponse()
response.say('Hello')
return str(response)
See the doc of <Say></Say.
If you want to use the .wav you created then you would need to save it somewhere accessible (e.g. an Amazon S3 bucket) and then you can use TwiML <Play></Play>.

Thanks for the answers everyone. The solution turned out to be a small change.
I had to change ap.lin2ulaw(speakBytes.audio_data,1) to ap.lin2ulaw(speakBytes.audio_data,4) and it worked fine. It seems to be the compatibility of microsoft text to speech and twilio formats.

Related

DialogFlow - send custom payload for telegram such as video

Folks.
I need to send video file from dialog flow as URI to telegram user.
Tried:
{
"telegram": {
"video": "https://"
}
}
Tried to guess JSON format for fulfillment message via "payload" but nothing works.
And I could not find documentation about the required format.
How to do it ?
So apparently you cannot send a video in Telegram. According to doc the only supported rich responses are Image, Card, Quick Replay, and Custom Payload (which includes text and hyperlink). If you want to send the link here's the format of custom response:
{
"telegram": {
"text": "You can read about *entities* [here](/docs/concept-entities).",
"parse_mode": "Markdown"
}
}

How to use Twilio to send customized PDF’s from Google Drive/Sheets

I am having problems trying to send customized PDFs from Google Drive to users on WhatsApp using Twilio’s API.
I am using node.js and the documents are created in Google Sheets and stored in Google Drive.
client.messages
.create({
mediaUrl:['https://drive.google.com/uc?id=INSERT_FILE_ID&export=download'],
from: 'whatsapp:+14155238886',
body: `It's taco time!`,
to: 'whatsapp:+15017122661'
})
.then(message => console.log(message.sid));
I thought it would be possible to send the PDF by passing the PDF’s Drive link through MediaURL as that is what is recommended on Twilio’s site for sending media (Images, .mp3’s and PDF’s) but when I try that it doesn’t work.
NB: it works when I try image URL’s etc.
I have tried sending the webContentLink of the PDF that also doesn’t work.
example: how the pdf should look when sent
I do not want to send the user a normal link for them to leave WhatsApp.
Any help with this or any other method to achieve this would be greatly appreciated.
Twilio will check the content-type of the file which you have been passing in the mediaUrl parameter. Twilio will reject if the content-type headers of your media attachment do not match the file extension (for example, content-type is image/jpg but the file extension is .png). It specified in the following links specifies:
Twilio Troubleshooting
Twilio supported-mime-types

Dialogflow - Can't save response with only media content

I want to set up a response which plays an mp3 file in response to invocation.
The only way I managed to get this to work is of I add a simple response and suggestion_chips.
When I try to save only the media content I get an error "Errors in 'Default Welcome Intent' intent: Google Assistant simple_response, suggestion_chips should be added to intent".
What is the correct way to set up a response that will play an mp3 file without a text response and suggestion_chips?
This does not work:
This works:
A MediaResponse must also have a simple text response or SSML response associated with it.

How can a bot receive a voice file from Facebook Messenger (MP4) and convert it to a format that is recognized by speech engines like Bing or Google?

I'm trying to make a bot for Facebook Messenger using Microsoft's Bot Framework that will do this:
Get a user's voice message sent via Facebook Messenger
Convert speech to text
Do something with it
There's no problem with getting the voice message from Messenger (the URL can be extracted from the message the bot receives), and there's also no problem with converting an audio file to speech (using Bing Speech API or Google's similar API).
However, these APIs require PCM (WAV) files, while Facebook Messenger gives you an MP4 file.
Is there a popular/standard way of converting one format into another that is used in writing the bots?
So far my best idea is to run vlc.exe as a console job on my server and convert the file, but that doesn't sound like the best solution.
Developed a solution that works as follows:
Receive voice message from facebook
Download the MP4 file to local disk using the link inside Activity.Attachments
Use MediaToolKit (wrapper for FFMPEG) to convert MP4/AAC to WAV on local server
Send the WAV to Bing Speech API
So the answer to my question is: use MediaToolKit+ffmpeg to convert the file format.
Sample implementation and code here: https://github.com/J3QQ4/Facebook-Messenger-Voice-Message-Converter
public string ConvertMP4ToWAV()
{
var inputFile = new MediaFile { Filename = SourceFileNameAndPath };
var outputFile = new MediaFile { Filename = ConvertedFileNameAndPath };
using (var engine = new Engine(GetFFMPEGBinaryPath()))
{
engine.Convert(inputFile, outputFile);
}
return ConvertedFileNameAndPath;
}

Recieve zip file from Box View API

i'm using the Box View API to convert a PDF file to HTML, i'm using the /documents/{id}/content.{extension} section.
The response for this GET call is a .zip file, however i don't know how to retrive it and make downloadable.
Also note that i'm using node.js.
Thanks for your help
You can set your own webhook URL that will be called by Box when your document status changes (one POST on your webhook for "document.viewable", and one for "document.done" plus one "document.error" if an transformation error occured).
Just listen to the "document.done" status and download the assets then. Format that is posted to the webhook you have set looks like :
[{
"type": "document.done",
"data": {
"id": "4cca28f1159c4f368193d5014fabc16e"
},
"triggered_at": "2014-01-30T20:33:04.798Z"
}]
Beware of the docs and check the format programatically. Their API docs are often no quite correct and they post multiple webhooks at the time i'm writing (which is a bug i've reported).
For more info and Box View API docs

Resources