Execute JavaScript from Livecode

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm
Location: San Diego, CA USA

Execute JavaScript from Livecode

Post by lohill » Wed Mar 17, 2021 10:58 pm

I have an API from textbelt.com that allows you to send a text message from your computer to a mobile phone. Here is the Javascript: (they also have other languages but not LiveCode)

Code: Select all

fetch('https://textbelt.com/text', {
  method: 'post',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    phone: '5555555555',
    message: 'Hello world',
    key: 'textbelt',
  }),
}).then(response => {
  return response.json();
}).then(data => {
  console.log(data);
});
They claim that if you change the phone number (555...) to your cell number and replace their key "textbelt" with a key that you can buy from them that you can send "Hello World" or whatever to your cell. They also say that the code they supply (textbelt) will let the API work one time on any given day. My problem is that I can't work out how to call the API from Livecode.

I have tried a suggestion for Klaus in a 2015 thread that looks like this.

Code: Select all

on mouseUp pMouseButton
   put field "Handler" into tCommands
   replace "5555555555" with "6192013100" in tCommands
   set the javascriptHandlers of widget "Browser" to tCommands  --(Klaus said this but nothing more)
   put the result into tResult
   
Nothing seems to happen (no message) and tResult is empty. Any suggestions?

I have also tried revBrowserOpen along with "revBrowserAddJavaScriptHandler lBrowserID, tCommands" and got no results either. Can anyone fix my mouseUp?

Thanks in advance,
Larry

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Thu Mar 18, 2021 2:18 am

It's been a while since I've done anything with JS, but if I remember correctly I had to modify the JS to put the return data into a variable on the JS side, then I could retrieve that variable from LCS side. If I get some time later I'll take a look at that JS + LCS stuff I was working with.

Also I don't see where you are calling and of those JS handlers.
Look at the example in the LC Dictionary

Code: Select all

 // Calling the handler from JavaScript within the browser
liveCode.myJSHandler("myMessage", 12345);
I guess in your LC script you might call something like:

Code: Select all

liveCode.fetch("https://textbelt.com/" & tMyTextMsg, tMyJSONdata)
tMyTextMsg & tMyJSONdata would be variables you would fill with the appropriate data ahead of time.

also it doesn't look like you set the JS handlers correctly

Code: Select all

set the javascriptHandlers of widget "Browser" to "fetch"
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Thu Mar 18, 2021 2:39 am

actually you probably have to modify the JS Handler names too, to make LC handlers available to the JS side.
(I think this is all a bit different than the old revBrowser way)
From the Dictionary:
The javascriptHandlers is a list of LiveCode handlers that are made available to JavaScript calls within the browser. The handlers will appear as methods attached to a global liveCode object. You can call these methods as you would any other JavaScript function and pass whatever parameters you require.
And calling JS from the LC side should be in the form: do tMyJSContainer in widget "Browser"
Example from the LC Dictionary:

Code: Select all

do "document.getElementById('myButton').hidden = 'hidden'" in widget "myBrowser"
It is all a bit confusing.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm
Location: San Diego, CA USA

Re: Execute JavaScript from Livecode

Post by lohill » Thu Mar 18, 2021 2:57 pm

Paul
Thanks for trying but it is still way more than a "bit confusing" for me. When you say "set the javascripthandlers of widget "Browser" to "fetch" did you chose the word "fetch" because that is the first word of the code in the api that textbelt gave me. I think of that as the "container" that I want LiveCode to execute. I just have to make sure the container contains the proper phone number, message and api key before execution. I think of the container as tCommands in my original code. I don't expect to get anything back except maybe a message of whether or not my message got sent.

If you have any more ideas I sure need them.
Larry

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm
Location: San Diego, CA USA

Re: Execute JavaScript from Livecode

Post by lohill » Thu Mar 18, 2021 7:22 pm

Code: Select all

on mouseUp pMouseButton
   put field "Handler" into tCommands -- this field contains the api
   replace "5555555555" with "6192013186" in tCommands
   replace "Hello World" with "Spot Available" in tCommands
   set the javascriptHandlers of widget "Browser" to tCommands
   do liveCode.fetch in widget "Browser"
   put the result into tResult
end mouseUp
LiveCode does not object to this code but no text is sent and tResult is empty.

Larry

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Fri Mar 19, 2021 12:16 am

lohill wrote:
Thu Mar 18, 2021 2:57 pm
Paul
Thanks for trying but it is still way more than a "bit confusing" for me. When you say "set the javascripthandlers of widget "Browser" to "fetch" did you chose the word "fetch" because that is the first word of the code in the api that textbelt gave me. I think of that as the "container" that I want LiveCode to execute. I just have to make sure the container contains the proper phone number, message and api key before execution. I think of the container as tCommands in my original code. I don't expect to get anything back except maybe a message of whether or not my message got sent.

If you have any more ideas I sure need them.
Larry
No, I used "fetch" because that looked like the JavaScript Handler (function) but I don't think you need to set the javascriptHandlers property at all if you're not interested in getting back the result from the JavaScript Handler that you're executing. I think it would not need the livecode. in your line liveCode.fetch (and I'm pretty sure that would only work if you had added it to the JS in your handler fld first anyway).

I would think just replacing the parameters used in the JavaScript, which is in your tCommands variable, and then calling it with

Code: Select all

do tCommands in widget "Browser" 
-- should work.

Think of the browser widget as a separate Script interpreter engine that only knows about JavaScript but nothing at all about LiveCode (with the exception of a global 'livecode' JavaScript object that can be used to pass data back to the LiveCode scripting engine). There isn't all that much difference between executing JavaScript in the Browser widget versus executing the same JavaScript in a full-blown web browser app.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Fri Mar 19, 2021 12:43 am

lohill wrote:
Thu Mar 18, 2021 7:22 pm

Code: Select all

on mouseUp pMouseButton
   put field "Handler" into tCommands -- this field contains the api
   replace "5555555555" with "6192013186" in tCommands
   replace "Hello World" with "Spot Available" in tCommands
   set the javascriptHandlers of widget "Browser" to tCommands
   do liveCode.fetch in widget "Browser"
   put the result into tResult
end mouseUp
LiveCode does not object to this code but no text is sent and tResult is empty.

Larry
I just looked at the https://textbelt.com... I'm not sure that you even need JavaScript at all! This is a POST method, it's like a webpage form that POST's it's fields back to the webserver when the person filling it out hits the submit button. Depending on the OS, you could do this in LC with the shell() command and curl, like in the bash example on their site:

Code: Select all

get shell("curl -X POST https://textbelt.com/text --data-urlencode phone='18001234'  --data-urlencode message='Hello world' -d key=textbelt")
put the result
Make sure you use the 10 digits phone number for the US or Canada (added the 1 in front of the number).
Last edited by PaulDaMacMan on Fri Mar 19, 2021 2:04 am, edited 1 time in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Fri Mar 19, 2021 2:00 am

Just tested it with my phone number
I sure hope I'm not on a SPAM Phone list now.
attachment=0]Screenshot_20210318-205726_Messages.jpg[/attachment]
Attachments
Screenshot_20210318-205726_Messages.jpg
Last edited by PaulDaMacMan on Fri Mar 19, 2021 2:05 am, edited 2 times in total.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Execute JavaScript from Livecode

Post by FourthWorld » Fri Mar 19, 2021 2:04 am

LC has built in POST and GET commands for exchanging data with web servers. Details in the Dictionary, and in the Lessons:

https://lessons.livecode.com/m/4071/l/1 ... a-rest-api
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

lohill
Posts: 770
Joined: Tue Dec 08, 2009 6:37 pm
Location: San Diego, CA USA

Re: Execute JavaScript from Livecode

Post by lohill » Fri Mar 19, 2021 2:08 am

You da man PaulDaMacMan'

do tCommands in widget "Browser" worked.

Sure glad I don't have to worry about bash and curl

Thanks,
Larry

PaulDaMacMan
Posts: 616
Joined: Wed Apr 24, 2013 4:53 pm
Contact:

Re: Execute JavaScript from Livecode

Post by PaulDaMacMan » Fri Mar 19, 2021 2:24 am

FourthWorld wrote:
Fri Mar 19, 2021 2:04 am
LC has built in POST and GET commands for exchanging data with web servers. Details in the Dictionary, and in the Lessons:

https://lessons.livecode.com/m/4071/l/1 ... a-rest-api
What Richard suggested is better because it purely uses LiveCode and so isn't dependent on anything external (such as curl), you might have to URLEncode the Data (message= & phone=) before send it to the URL with the post command.
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Execute JavaScript from Livecode

Post by Thierry » Fri Mar 19, 2021 4:21 pm

I have an API from textbelt.com that allows you to send a text message
from your computer to a mobile phone.
they also have other languages but not LiveCode
Here is a working solution in pure LIVECODE, using the POST command.

LC POST to Textbelt.jpg
Post to TextBelt.com

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

JereMiami
Posts: 119
Joined: Mon Nov 03, 2014 12:17 am

Re: Execute JavaScript from Livecode

Post by JereMiami » Sat May 08, 2021 11:27 pm

Using the post command, how would you convert a SHELL command that contains a "-u" as opposed to a "-d" to a POST command? For instance:

Code: Select all

shell("curl https://api.stripe.com/v1/charges -u <api_key>")
I tried:

Code: Select all

   put <api_key> into tKey
   put "key=" & (urlencode(tKey)) into tUser
   post tUser to url ("https://api.stripe.com/v1/charges")
But to no success, although it does say the api key is invalid. So, I guess there was some success.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9801
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Execute JavaScript from Livecode

Post by FourthWorld » Sun May 09, 2021 1:36 am

What do the d and u flags do?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

JereMiami
Posts: 119
Joined: Mon Nov 03, 2014 12:17 am

Re: Execute JavaScript from Livecode

Post by JereMiami » Sun May 09, 2021 2:44 am

"-d" is data and "-u' is user. Stripe gives me an "invaid_request_error" on their control panel, so it is making a connection, but it may have something to do with my request, I am sure.

Post Reply

Return to “Internet”