Using Twilio/XML without Rails - ruby

I am attempting to write a Twilio script to do voice broadcasting without rails - I would like to be able to run the script straight from my terminal.
I have a very simple script, straight from the twilio-rb gem docs:
# This should be in an initializer or similar
Twilio::Config.setup \
:account_sid => account,
:auth_token => token
Twilio::Call.create :to => '+1234567890', :from => '+0987654321',
:url => xml_file
xml_file is a xml file on my local machine, but it throws this error:
Error #21205: Url is not a valid url
How can I write the above script to operate off of a local xml file? The end goal is strictly to make a phone call, play an audio message, gather a button press and do an action based on the number received. The Twiml XML file should do that for me, if I can get it to work.
EDIT:
When using the dropbox share link, I get this error within the Twilio interface:
'Twilio is unable to process the Content-Type of the provided URL. Please see the Twilio Markup XML Documentation for more information on valid Content-Types.
You must return a Content-Type for all requests. Requests without a Content-Type will appear in the Debugger as a 502 Bad Gateway error.
Having a phone number, outgoing call request or action attribute refer to a non XML or audio resource.
Having a Play verb attempt to play non-audio content, such as XML or text.
Verify that that your web server is returning a Content-Type and it is the expected value
Make sure the URL noted refers to a valid resource'
To make sure, I copied an example I know will work into my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="man">Hey man! Listen to this!</Say>
<Play>http://foo.com/cowbell.mp3</Play>
<Say voice="man">What did you think of that?!</Say>
<Record action="http://foo.com/handleRecording.php" method="GET" maxLength="20" finishOnKey="*"/>
<Gather action="/process_gather.php" method="GET">
<Say>Now hit some buttons!</Say>
</Gather>
<Say voice="man">Awesome! Thanks!</Say>
<Hangup/>
</Response>

Two ideas to try:
(1) If the file is read locally and sent to the Twilio server, try:
:url => 'file:///path/file.xml'
where
host in //host/ is omitted, yielding three slashes in a row.
path is the full filesystem path to your XML file.
file.xml is the name of your XML file.
(2) If the file must be publicly readable by the Twilio server, try placing it in the cloud somewhere (such as Dropbox) and using the public URL to it there.

Related

Ruby post a file stream via HTTP as multipart/form-data?

Now,I have a need that post a file stream,not a local file.the process is:
client(file) ---> my server ----> third party Cloud Storage,the transfer is file stream.
I have found this article:
Ruby: How to post a file via HTTP as multipart/form-data?
require 'rest_client'
RestClient.post('http://localhost:3000/foo',
:name_of_file_param => File.new('/path/to/file'))
you can see that the name_of_file_param is a local file,not stream.
so I want to know ,if this is file stream form the client ,what should I do
You should be able to use any IO object, including a stream, as the parameter:
RestClient.post('http://localhost:3000/foo', :name_of_file_param => my_stream)

Response message not being accepted by WeChat

So we got our application accepted in the wechat debug console, and are looking to respond with rich media type messages.
the request expected from the server is as follows:
<xml>
<ToUserName>UserName</ToUserName>
<FromuserName>TestUser</FromuserName>
<CreateTime>7200</CreateTime>
<MsgId>12302</MsgId>
<Content>Test Message</Content>
</xml>
To which we reply with the following:
<xml>
<ToUserName>TestUser</ToUserName>
<FromUserName>UserName</FromUserName>
<CreateTime>7200</CreateTime>
<MsgType>news</MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title>Test</Title>
<Description>Test</Description>
<PicUrl>http://PICURL</PicUrl>
<Url>http://ARTICLE_URL</Url>
</item>
</Articles>
</xml>
However the application doesn't seem to be getting the requests as it was setup so the questions is:
Will requests go to the URL setup?
If so is the xml provided correct for the response and also for the messages that gets posted to the url provided?
Are there specific headers presents in the request?
Yes the Requests will go to the URL that you have setup and send a straight XML post to your script.
Check your XML there seems to be quite a few differences from the actual system input and output also check the example of working input and output XML and try this. Obviously replacing the ToUserName and FromUserName:
INPUT RESPONSE
<xml>
<ToUserName><![CDATA[gh_4456]]></ToUserName>
<FromUserName><![CDATA[123abc]]></FromUserName>
<CreateTime>1397201326</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[test]]></Content>
<MsgId>6000934001298302633</MsgId>
</xml>
OUTPUT RESPONSE
<xml>
<ToUserName><![CDATA[123abc]]></ToUserName>
<FromUserName><![CDATA[gh_4456]]></FromUserName>
<CreateTime>1397201781</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[Your test title]]></Title>
<Description><![CDATA[test description]]></Description>
<PicUrl><![CDATA[http://test.com/img.jpg]]></PicUrl>
<Url><![CDATA[http://test.com/]]></Url>
</item>
</Articles>
</xml>
No headers you need to worry about.
FOR OFFICIAL OA: I think your problem might be that you have not enabled developer mode yet. Even though you have setup the URL and TOKEN. Please confirm developer mode is ENABLED. Go to admin.wechat.com -> login -> function -> advanced -> developer mode should be ENABLED.
FOR SANDBOX ACCOUNT: developer mode is always enabled.
Also check your CreateTime this should be a unix timestamp.
If none of that resolves it go and look at your access logs. Find the URL wechat is posting to. Once you have the URL got to http://www.hurl.it/ change the destination type to POST and paste the URL there. Add a Header called "Content-Type" with the value "text/xml" click on add body and post your input response in there. This will give you the response that WeChat sees. I tested yours and found the ToUserName was blank, also ensure you don't have unneeded spaces or newlines there.

Ruby openuri test if uri is valid

I'm trying to test if a uri is valid (e.g. actually has content, not testing if it is well formed here) using ruby code, and I can open a uri using open(uri). But in my case, the uri is a link to a file to be downloaded and I don't want to have to download the whole file just to verify that there is content there.
Is there another solution for this?
Try this
require 'net/http'
u = URI.parse('http://www.example.com/')
status = Net::HTTP.start(u.host, u.port).head(u.request_uri).code
# status is HTTP status code
You'll need to use rescue to catch exception in case domain resolution fails.

Using WSDL With Ruby

I'm getting this error:
WSDL::XMLSchema::Parser::UnknownElementError
unknown element: {}HTML
at 'new'
when I consume webservices using Ruby. Here is the code snippet:
require 'soap/wsdlDriver'
wsdl = url
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
driver.options["protocol.http.basic_auth"] << [url, user_name, password]
the url points to a well-formed xml.
Any solutions?
Can you share the wsdl file? Maybe that would help us answering it better.
In any case, I'd suggest generating the Driver classes first using wsdl2ruby. And then loding them in your Ruby file (through require). Examples (from the man pages):
# For server side:
$ wsdl2ruby.rb --wsdl myapp.wsdl --type server
# For client side:
$ wsdl2ruby.rb --wsdl myapp.wsdl --type client
If you load the URL in a web browser, does it get redirected to a different location?
In my experience, one reason the error "unknown element: {}HTML" comes up is the WSDL parser is trying to parse the HTML portion of the HTTP redirect and failing to do so. Therefore, you should deal with the redirect yourself (either in code or manually) and give the WSDL driver the final URL.

Issues POSTing XML to OAuth and Signature Invalid with Ruby OAuth Gem

[Cross-posted from the OAuth Ruby Google Group. If you couldn't help me there, don't worry bout it]
I'm working on integrating a project with TripIt's OAuth API
and am running into a weird issue.
I authenticate fine, I store and retrieve the token/secret for a given
user with no problem, I can even make GET requests to a number of
services using the gem. But when I try using the one service I need
POST for, I'm getting a 401 "invalid signature" response.
Perhaps I'm not understanding how to pass in data to the AccessToken's
post method, so here's a sample of my code:
xml = <<-XML
<Request>
<Trip>
<start_date>2008-12-09</start_date>
<end_date>2008-12-27</end_date>
<primary_location>New York, NY</primary_location>
</Trip>
</Request>
XML`
response = access_token.post('/v1/create', {:xml => xml},
{'Content-Type' => 'application/x-www-form-urlencoded'})
I've tried this with and without escaping the xml string before hand.
The guys at TripIt seemed to think that perhaps the xml param wasn't
getting included in the signature_base_string, but when I output that
(from lib/signature/base.rb) I see:
POST&https%3A%2F%2Fapi.tripit.com%2Fv1%2Fcreate&oauth_consumer_key
%3D%26oauth_nonce
%3Djs73Y9caeuffpmPVc6lqxhlFN3Qpj7OhLcfBTYv8Ww%26oauth_signature_method
%3DHMAC-SHA1%26oauth_timestamp%3D1252011612%26oauth_token
%3D%26oauth_version%3D1.0%26xml%3D%25253CRequest%25253E
%25250A%252520%252520%25253CTrip%25253E%25250A
%252520%252520%252520%252520%25253Cstart_date%25253E2008-12-09%25253C
%252Fstart_date%25253E%25250A
%252520%252520%252520%252520%25253Cend_date%25253E2008-12-27%25253C
%252Fend_date%25253E%25250A
%252520%252520%252520%252520%25253Cprimary_location%25253ENew
%252520York%252C%252520NY%25253C%252Fprimary_location%25253E%25250A
%252520%252520%25253C%252FTrip%25253E%25250A%25253C%252FRequest%25253E
%25250A
This seems to be correct to me.
I output signature (from the same file) and the output doesn't match
the oauth_signature param of the Auth header in lib/client/
net_http.rb. It's been URL-encoded in the auth header. Is this
correct?
Anyone know if the gem is broken/if there's a fix somewhere? I'm finding it hard to trace through some of the code.
Your oauth_token is empty. Not familiar with the protocol, is that ok?
Once you include the correct token, please also remember to use token_secret in the signing.

Resources