First, I'm a total noob with AWS Lambda so I wanted to see if anyone can point me in the right direction to setting up an AWS Lambda function that will send a Twilio MMS message that would be triggered by an upload of an image to an S3 bucket? I guess C# isn't a hard set requirement but it would be nice since that is the language that I'm currently working with.
I'm currently doing this via my app but want to offload this to an AWS Lambda function if at all possible.
TIA
I'm neither a C# expert nor very familiar with AWS Lamda, but this post here should get you started.
And this documentation page explains how to send messages with C#. So the only thing you need to do is add the Twilio Account SID and Auth key to the Lamda environment variables, and there you go.
// Install the C# / .NET helper library from twilio.com/docs/csharp/install
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
class Program
{
static void Main(string[] args)
{
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "Join Earth's mightiest heroes. Like Kevin Bacon.",
from: new Twilio.Types.PhoneNumber("+15017122661"),
to: new Twilio.Types.PhoneNumber("+15558675310")
);
Console.WriteLine(message.Sid);
}
}
Related
I am attempting to write a C# wrapper for the Twilio Programmable Chat tool. The library provided is for JS clients. I thought that using a tool like ClearScript (V8) would allow me to wrap the js as needed.
The example code on the site is
const Chat = require('twilio-chat');
// Make a secure request to your backend to retrieve an access token.
// Use an authentication mechanism to prevent token exposure to 3rd parties.
const accessToken = '<your accessToken>';
Chat.Client.create(accessToken)
.then(client => {
// Use Programmable Chat client
});
so after I initialize
using (var engine = new V8ScriptEngine())
{
engine.Execute(#"
const Chat = require('twilio-chat.js');
const token = 'my token';
Chat.Client.create(token).then(client=>{
});
");
}
The program errors on the 'require' line with the error require is not defined.
I have read that require is simply returning the module exports so I replaced the require('...
with
engine.Execute(#"
const Chat = ('twilio-chat.js').module.exports;
...
but that errors with Cannot read property 'exports' of undefined'
I got the js file from https://media.twiliocdn.com/sdk/js/chat/releases/4.0.0/twilio-chat.js
How can I get around this or maybe there is a better way. I appreciate any and all insights.
Thanks
I don't know anything about Twilio, but here's how to enable ClearScript's CommonJS module support. This sample loads the script from the web, but you can limit it to the local file system or provide a custom loader:
engine.AddHostType(typeof(Console));
engine.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableWebLoading;
engine.DocumentSettings.SearchPath = "https://media.twiliocdn.com/sdk/js/chat/releases/4.0.0/";
engine.Execute(new DocumentInfo() { Category = ModuleCategory.CommonJS }, #"
const Chat = require('twilio-chat');
const token = 'my token';
Chat.Client.create(token).then(
client => Console.WriteLine(client.toString()),
error => Console.WriteLine(error.toString())
);
");
This successfully loads the Twilio script, which appears to be dependent on other scripts and resources that aren't part of the bare standard JavaScript environment that ClearScript/V8 provides. To get it working, you'll have to augment the search path and possibly expose additional resources manually. As shown, this code prints out ReferenceError: setTimeout is not defined.
I am trying to set up a method so each time my application is accessed it retrieves a new token using JWT.
I've ran through the docusign github examples but this is still newer to me so I am stuck unfortunately.
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
using DocuSign.eSign.Client.Auth;
public string getAccessToken(){
var apiClient = new ApiClient();
OAuth.OAuthToken authToken = apiClient.RequestJWTUserToken({clientID},
"{Impersonated User GUID}",
"https://account-d.docusign.com",
Encoding.UTF8.GetBytes(System.Configuration.ConfigurationManager.AppSettings["jwtPrivKey"]),
1);
return authToken.access_token;
}
Exception Details: DocuSign.eSign.Client.ApiException: Error while requesting server, received a non successful HTTP code Error with response Body:
I have similar code to what you have. I ran into an issue when I was developing where my auth path variable could not include "https://". I have to use plain "account-d.docusign.com" instead.
I need to automatically make calls for customers and start a interaction with them through voice. Basically, when the customer pickup the phone, my "robot" will ask: "Hey, it seems you didn't finish your order. Would you like to finish by phone?" Customer will say YES, NO, or another phrase, and I will follow the flow.
My questions:
1) What is the best approach to solve this problem using Twilio?
2) It seems Twilio has this functionality (ASR) to understand only for inbound calls when I use functions. How can I do that with outbound calls?
3) Is Twilio ready to understand another languages except English? I need to use Portuguese, Brazil.
Thank you for your help.
Twilio developer evangelist here.
To automatically make calls you will need to use the Twilio Programmable Voice API. I note you're using C# according to the tags, so you can start with the Twilio C# library. Using the library you can make calls with the API like this:
using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
class Example
{
static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/console
const string accountSid = "your_account_sid";
const string authToken = "your_auth_token";
TwilioClient.Init(accountSid, authToken);
var to = new PhoneNumber("+14155551212");
var from = new PhoneNumber("+15017122661");
var call = CallResource.Create(to,
from,
url: new Uri("http://demo.twilio.com/docs/voice.xml"));
Console.WriteLine(call.Sid);
}
}
For a bit more detail on what all this means, check out the guide on making outbound phone calls with C#.
You will see in that example that we pass a URL to the method that makes the call. This URL can point anywhere, including at a Twilio Function (which is just a Node.js run in the Twilio infrastructure) or your own server. When the call connects to the user Twilio will make an HTTP request to that URL to find out what to do next. To tell Twilio what to do you will need to return TwiML.
To respond with the message that you want and then gather speech input from your user you will need to use the <Say> and <Gather> elements of TwiML. An example response might look like:
<Response>
<Gather input="speech" hints="yes, no" action="/gatherResponse">
<Say voice="alice">Hey, it seems you didn't finish your order. Would you like to finish by phone?</Say>
</Gather>
</Response>
In this case we start with <Gather> so that we can capture anything the user says while the call is speaking to them. We set the input attribute to "speech" so that we can use speech to text to recognise what they say. Also included is the hints attribute which can give hints to the service for the text you expect to hear. Finally, there is an action attribute which is a URL that will be called with the result of this.
You can change the language that the <Gather> expects to hear using the language attribute. There are a number of languages available including Brazilian Portuguese, which you would set with "pt-BR".
Nested inside the <Gather> is a <Say> which you use to read out your message. You can use the voice attribute to change available voices.
The next thing you need to do is respond to the result of the <Gather>. At this stage it depends on what web application framework you are using. The important thing is that when it has a result, Twilio will make an HTTP request to the URL set as the action attribute. In that request will be two important parameters, SpeechResult and Confidence. The SpeechResult has the text that Twilio believes was said and the Confidence is a score between 0.0 and 1.0 for how sure Twilio is about it. Hopefully your result will have "Yes" or "No" (or the Brazilian Portuguese equivalent). At this point you need to return more TwiML to tell Twilio what to do next with the call depending on whether the answer was positive, negative or missing/incorrect. For more ideas on how to handle calls from here, check out the Twilio voice tutorials in C#.
Let me know if that helps at all.
I am working on one small utility using Twilio API which intend to download the recording of the call as soon as call ends ( can be done using webhooks ? ). I am not sure if twilio supports this kind of webhooks or not.
The second approach that i have in mind is to create nightly job that can fetch all the call details for that day and download the recordings.
Can anyone suggest me which is the best approach to follow. I checked the webhooks but i am not sure if they provide the call ended event.
I would appreciate if anyone can provide me code sample on how to get the recordings for particular date and download them using C# from twilio.
Twilio developer evangelist here.
You can get recording webhooks, but how you do so depends on how you record the call.
Using <Record>
Set a URL for the recordingStatusCallback attribute on <Record> and when the recording is ready you will receive a webhook with the link.
Using <Dial>
If you record a call from the <Dial> verb using the record attribute set to any of record-from-answer, record-from-ringing, record-from-answer-dual, record-from-ringing-dual then you can also set a recordingStatusCallback.
Using <Conference>
If you record a conference then you can also set a recordingStatusCallback.
Recording an outbound dial
If you record the call by setting Record=true on an outbound call made with the REST API then you can also set a webhook URL by setting a RecordingStatusCallback parameter in the request.
Retrieving recordings from the REST API
You can also use your second option and call the REST API to retrieve recordings. To do so, you would use the Recordings List resource. You can restrict this to the recordings before or after a date using the list filters.
Here is a quick example of how you would use the Twilio C# library to fetch recent recordings:
using System;
using Twilio;
class Example
{
static void Main(string[] args)
{
// Find your Account Sid and Auth Token at twilio.com/user/account
string AccountSid = "AC81ebfe1c0b5c6769aa5d746121284056";
string AuthToken = "your_auth_token";
var twilio = new TwilioRestClient(AccountSid, AuthToken);
var recordings = twilio.ListRecordings(null, null, null, null);
foreach (var recording in recordings.Recordings)
{
Console.WriteLine(recording.Duration);
// Download recording.Uri here
}
}
}
Let me know if this helps at all.
I am trying to use the Microsoft Bot Framework DirectLine API to read and add messages to existing conversations between other users and my bot. From what I've read I believe this should be possible when using the master-secret but it's just not working for me. I'm using a WebAPI to try and access two of my existing conversations (on Facebook & Skype) as follows:
[HttpPost]
[Route("remind")]
public string Remind()
{
var secret = System.Configuration.ConfigurationManager.AppSettings["secret"];
var uri = new Uri("https://directline.botframework.com/");
var creds = new DirectLineClientCredentials(secret);
DirectLineClient client = new DirectLineClient(uri, creds);
Conversations convs = new Conversations(client);
var conversationIDs = new string[] { "0000000000000000-0000000000000000", "00:0123456789abcdefghijklmnopqrstuvwxyz0123456789-A-_0123456798ABCDEF" }; // Existing Facebook & Skype conversations
// Send a message to each conversation:
foreach (var conversationID in conversationIDs)
{
Message message = new Message(conversationId: conversationID, fromProperty: "My Bot", text: "Hey dude, remember that thing!");
Console.WriteLine(message.Text);
convs.PostMessage(conversationID, message); // FAILS - This executes but doesn't do anything.
}
// Try reading the messages from a conversation (just to test if it's working):
string waterMark = null;
var set = convs.GetMessages(conversationIDs[0], waterMark); // FAILS - This fails with a 404 not found.
waterMark = set.Watermark;
return "Done :-)";
}
It fails silently calling PostMessage() and fails with a 404 for the GetMessages(). I seem to be doing the right thing, the bot is live etc and works very well in Facebook & Skype separately from the DirectLine API. It only works if I create a new conversation using the DirectLine API, I can then access its messages and post new messages to it.
This question sort of helps but doesn't quite tell me what to do to fix it:
Difficulty accessing messages in an existing conversation in Microsoft Bot Framework
Any help would be much appreciated.
Thanks
For security reasons, you can't use DirectLine to spy on messages from another conversation. For the scenario you describe (escalating to a human) there a number of different ways to approach this. One is to have your bot broker conversations between the accounts (i.e. Facebook End User <-> Your Bot <-> Facebook Support Person). Each is talking to the bot, and the bot passes the message through to the other user. (Could also be Facebook User <-> Your Bot <-> Skype User) Your bot would have to store last n messages to provide context. Alternatively, I've seen folks build their own customer support chat interface using direct line that sits on the far side. Hope this helps