Creating a twiML file while working with twilio and C#.net - c#

I am working with twilio.com api in C#.net. C# code calls the phone number I have given:
string accountSid = "AC5xxxxxxxxxfdb0cf485d52ce";
string authToken = "57fxxxxxxxxx1xxx71a";
var client = new TwilioRestClient(accountSid, authToken);
var request = new CallListRequest();
var callList = client.ListCalls(request);
var options = new CallOptions();
options.Url = "http://demo.twilio.com/docs/voice.xml";//
options.To = "+919876123456";
options.From = "+15163425887";
var call = client.InitiateOutboundCall(options);
MessageBox.Show(call.Sid);
I call my phone through above given code, picking up the call connects me to the xml file (a twiML file) mentioned in options.Url and I listen the message and .mp3 file mentioned in voice.xml. Now I want to replace this xml file with my custom xml file placed in a server. For testing purpose I created the exact copy of voice.xml and placed it into my server. So I change the url property to:
options.Url="http://productionserver.com/voice.xml";
After making this change when I pick the phone, it says "Application error has occurred" and call ends.
Has anyone worked with twilio and experienced such problem ? Is there any step that I am missing other than just creating an xml file ?

Twilio evangelist here.
First I'd suggest checking to see if anything got logged in your App Monitor. I suspect you will see Twilio has logged a 500 error there.
If you are trying to serve the raw XML file from IIS, then your probably running into the issue that IIS won't serve an XML file from a POST request, which is the default HTTP Method Twilio uses to request the Url.
There are a couple of work-arounds for this:
You can set the Method property on the CallOptions to GET which tells Twilio to make its request using the HTTP GET method OR
Since this is static XML you are serving, you cold use http://twimlbin.com to host the TwiML
Hope that helps.

Related

Accessing downloaded file instead of page HTML

I have some code that connects to an HTTP API, and is supposed to get an XML response. When the API link is placed in a browser, the browser downloads the XML as a file. However, when the code connects to the same API, HTML is returned. I've told the API owner but they don't think anything is wrong. Is there a way to capture the downloaded file instead of the HTML?
I've tried setting the headers to make my code look like a browser. Also tried using WebRequest instead of WebClient. But nothing works.
Here is the code, the URL works in the browser (file downloaded) but doesn't work for WebClient:
WebClient webClient = new WebClient();
string result = webClient.DownloadString(url);
The code should somehow get the XML file instead of the page HTML (actually the HTML doesn't appear in the browser, only the file).
The uri that you access may be a HTML page that have its own mechanism (like generating the actual download address which may be dynamically generated by the server and redirecting to it, in order to prevent external linking access) to access the real file.
It is supposed to use a browser background browser core like CefSharp to run the HTML and its javascript to let it navigate, and probably you may want to hook the download event to handle the downloading.
I think you need to add accept header to the WebClient object.
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.Accept] = "application/xml;q=1";
string result = webClient.DownloadString(url);
}
Thank you all for your input. In the end, it was caused by our vendor switching to TLS 1.2. I just had to force my code to use 1.2 and then it worked.

Check if website is loading will all its resources in C# .NET Framework

My goal is to make a request to web page which consists video player. The link looks like this:
mywebsite/620x439/9122600a
but if you want to have better resolution you have to pass an argument with it, so the link looks like this:
mywebsite/620x439/9122600a?wersja=720p
The problem is, that my program won't know if video from this link have better resolution, it have to figure it out itself. I had an idea to make a request to this URI and check the server response. I do it this way:
Uri targetUri = new Uri(videoPageURLHD);
var request = (HttpWebRequest)WebRequest.Create(targetUri);
var response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//do something
}
else
{
//do something else
}
The problem is, that no matter if video file with resolution exists or not, server always responds with HTTP 200, despite in debug console in web browser I can see that it failed to load resource ->
The question is: is it possible to get this info about failing to load resource using request/Webclient/whatever? As an output I need simple true/false answer to question if this video have better resolution or not.
is it possible to get this info about failing to load resource using
request/Webclient
No. HttpRequest and WebClient only loads the html, nothing else. They don't render the page, validate the html syntax, let alone loading JavaScript and Video.
You are getting a 200 is because the html download was successful. You could however try to figure out the Url for the video and try to download it using WebClient if its not blocked by the server.

Twilio Cant make call but no errors

I am trying make phonecall via Twilio using c# i am using the below code to make call.code executes successfully but i wont receive call.In my twilio page it shows staus as completed and durations.
const string accountSid = "ACe200aeb6115e1f3038a012e1103bd874";
const string authToken = "6XXXXXX";
TwilioClient.Init(accountSid, authToken);
var to = new PhoneNumber("+12025688652");
var from = new PhoneNumber("+919500553163");
var call = CallResource.Create(to,
from,
url: new Uri("https://demo.twilio.com/welcome/voice/"));
Console.WriteLine(call.Sid);
Twilio developer evangelist here.
I believe that you are actually making that call successfully. You are not experiencing the call yourself because of the parameters you have used.
You are making the call from your verified number. This is your real phone number, but when you set it to be the from address that just means Twilio will use it as the caller ID for the call.
For the to address you are using your Twilio number that you have bought within your account. When you make calls to that number, Twilio makes a webhook (HTTP) request to the URL that is set as the voice response in the number's settings. Currently, that is still set to the default, so Twilio retrieves this URL: https://demo.twilio.com/welcome/voice/. If you click through, you will see that this just reads out a message.
The URL you use when you create the call is also the default URL.
So what is happening with the call is that Twilio is using your caller ID to dial your Twilio number and when the call is answered, both sides of the call are reading out that message to each other.
If you want to at least make your own phone ring, then I'd start by reversing the to and from numbers in your API call. If you do that, then you should receive a call on your phone and when you answer it you will hear the demo message. Then, once you've done that you can start writing your own TwiML responses, possibly using Twimlbins to get started or by building your own application.
Let me know if this helps.

Application to download files from URL

Email Link
So I currently work for an engineering company and we receive files via Aconex (Aconex is a web based document management system for all consulting teams on a given project). Currently we have a system where we download the files (there is a link in the email that leads to the Aconex website) from the email and file them in a dated folder under the specific project. I've attached an image of the Aconex email link.
Now for the issue. Sometimes it can be quite overwhelming when you receive 20+ project related emails in a day (on top of everything else) and some of these may slip through the gaps.
Basically I would like to automate this process somehow. I want the user to be able to add the email link to the application, hit 'Process' and the files are then downloaded and filed under the specific project.
I've got some basic programming experience (mainly in c#) and would like to use this as my first 'real world' programming project.
Any help that can be offered is really appreciated.
Thanks people!
So, actually you want to make HTTP request to download it from publicly available web server?
You would need these two in the header:
using System.IO;
using System.Net;
The IO will help you messing with the local file system paths, directories and etc. Check: https://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx
The Net namespace contains many options for creating requests or downloading files.
Then in a method you create a WebRequest instance, with e.g. Create static method that have an URI object as input:
var httpRequest = WebRequest.Create(urlObject);
// here you setup your stuff like authorization or request method etc:
httpRequest.Method = "GET";
httpRequest.Timeout = settings.timeout;
// and finally call the request (this is an async approach)
httpRequest.BeginGetResponse(getResponse, this);
Here you get the response
public void getResponse(IAsyncResult __result)
{
WebResponse response = httpRequest.EndGetResponse(__result);
// here you deal with the response
}
Or you may use simpler way:
var webClient = new WebClient();
Uri urlObject = new Uri("http://yourUrl");
String localPath = "local\\filesystem\\path.file";
webClient.DownloadFileAsync(urlObject, localPath, this);
Check: https://msdn.microsoft.com/en-us/library/w8bysebz(v=vs.110).aspx
Have I got your issue right?

c# file downloading problem

I am using this code to download file from server and parse data from JSON notation:
WebClient wcl = new WebClient();
Uri url = new Uri(tickurl);
string srlz = wcl.DownloadString(url);
var dict = (new JavaScriptSerializer()).Deserialize<Dictionary<string, dynamic>>(srlz);
When I use it with http://deepbit.net/api/ + my token (URI returns JSON data) it works well.
But with https://mtgox.com/code/data/ticker.php it stucks on 3rd line of the function(data downloading).
What am i doing wrong? Both URLs return same JSON formatted data.
[add] it's not issue with https, this code works well enough with other https services. i am wondering if this could be a problem with SSL cert.
SOLVED: turned off SSL certificate validation. thanks
I suspect it's failing due to a security problem. When I fetch with wget, I get:
ERROR: certificate common name www.mtgox.com' doesn't match requested host namemtgox.com'
It's fine when I fetch with wget using the --no-check-certificate flag.
I don't know whether you can persuade WebClient not to check certificates... but a better option would obviously be to get the certificate fixed.
Alternatively, try this URL instead: https://www.mtgox.com/code/data/ticker.php - note the www at the front. That fetches in wget without any issues.
The Url: https://mtgox.com/code/data/ticker.php doesn't even open up in browser. It starts downloading the 'ticker.php' file. Your server is misconfigured. The code is fine. Most probably the server is not properly configured to process .php files as scripts.
it is a certificate error like Jon Skeet said.
Have a look here to find a easy solution
WebClient + HTTPS Issues
you shouldn't use this for all request, only for debugging

Categories