HTTP GET request through Form Script (Java Script)

737 views
Skip to first unread message

Periklis Argiriadis

unread,
Jun 19, 2020, 4:10:19 AM6/19/20
to Softone Developers Network
Hi,

in a form script using Java Script, i am trying to send a HTTP GET request to Telegram API. Reading and searching, i've reached at BlackBook page 325 where there is an example using google map api.
Based on that, i tried the following code.
Obviously it doesn't work (that's why i post here) and i am trying to figure out why. I don't have a better debugging method than place some X.WARNING between the lines.

function sendMessageTelegram(msg) {
this.msg = msg || "";
this.onSuccess = function(){};
this.onNoReply = function(){};
this.onError= function(){}
this.request= null;
var me = this,
url;
this.execute =
function() {
var apiToken = "xxxx";
var chatId = "zzzz";

url = 'http://api.telegram.org/bot'+ apiToken + 'sendMessage?chat_id='+ chatId + '&text='+ msg;

this.request = new ActiveXObject("MSXML2.XMLHTTP");
this.request.open('GET', url, true);
this.request.onreadystatechange=
  function() {
            
             X.WARNING("State: " + me.request.readyState);
                       
                            if (me.request.readyState==4) {

                               
X.WARNING("Status: " + me.request.status);

      if (me.request.status==200)
        me.onSuccess(me.request.responseText);
      else
me.onNoReply();
    }
  }
  try {
    this.request.send();
    
            X.WARNING("Response: " + this.request.responseText);

  }
  catch(e) {
    this.onError(e);
  }
}
}

function ON_AFTERPOST() {
var dm = new sendMessageTelegram('Hello!');
dm.onSuccess =
function(reply) {
  eval('var data = ' + reply);
  if (data.status === 'OK') {
    X.WARNING('OK');
  }
  else {
    X.WARNING('ERROR: ' + data.status + '\n\nError Message: '+ data.error_message);
  }
};
dm.execute();
}

As a result when i post the form, i get the following messages:

STATUS: 1
STATUS: 4
STATE: 0

I have checked the url that is generated and if i execute it through a browser, it works as expected.

Do you have any idea on how i can examine further why it failed ?

Liviu Buligan

unread,
Jun 19, 2020, 6:57:25 AM6/19/20
to Softone Developers Network
probably you mean you get STATUS 1, 4 and ReadyState 0

this is hard to understand as ReadyState=0 is an undefined error

Try to move from GET to POST and see if the error remains.

The rest of the code looks fine.

peripc.gr

unread,
Jun 19, 2020, 7:08:24 AM6/19/20
to liviu....@gmail.com, so...@googlegroups.com
With "State" i mean indeed the ReadyState. My mistake, excuse me.
ReadyState: 1
ReadyState: 4
Status: 0 (this seems weird. Normally it should be three digit number)

I will try with POST command too and i'll give feedback here



Στάλθηκε από το MIX 2 μου
--
Softone Developers Network group.
To post to this group, send email to so...@googlegroups.com
---
Λάβατε αυτό το μήνυμα επειδή έχετε εγγραφεί σε ένα θέμα στην ομάδα "Softone Developers Network" στις Ομάδες Google.
Για να απεγγραφείτε απ' αυτό το θέμα, επισκεφτείτε τη διεύθυνση https://groups.google.com/d/topic/soft1/p9dVVHFQD2g/unsubscribe.
Για να απεγγραφείτε απ' αυτή την ομάδα και όλα τα θέματά της, στείλτε ένα μήνυμα ηλεκτρονικού ταχυδρομείου στη διεύθυνση soft1+un...@googlegroups.com.
Για να κάνετε προβολή αυτής της συζήτησης στον ιστό, επισκεφτείτε τη διεύθυνση https://groups.google.com/d/msgid/soft1/250651a2-774c-4760-833f-6117767f6733o%40googlegroups.com.

Periklis Argiriadis

unread,
Jun 19, 2020, 10:33:03 AM6/19/20
to Softone Developers Network
I am going crazy in here.
I tried two other api urls, one from a test website https://jsonplaceholder.typicode.com and another one from google maps api (as in black book's example).


ReadyState: 1
ReadyState: 2
ReadyState: 3
ReadyState: 4
Status: 200

In third one i get only

ReadyState: 1
ReadyState: 4
Status: 0
(it omits ReadyStates 2 and 3 and i get an undefined status)

i cannot find any difference in calling this api compared to others.
Anyone can try directly the https://api.telegram.org/botXxxxxxXXxx/sendMessage?chat_id=ZZZZzzzZZzz&text=Hello and verify that the request is completed (not authorised, unknown chatId, etc etc. but the request itself is completed)
Για να απεγγραφείτε απ' αυτή την ομάδα και όλα τα θέματά της, στείλτε ένα μήνυμα ηλεκτρονικού ταχυδρομείου στη διεύθυνση soft1+unsubscribe@googlegroups.com.

Liviu Buligan

unread,
Jun 19, 2020, 12:19:47 PM6/19/20
to Softone Developers Network
sss
Https
1
2
3
4

404

at least is better then 0


even if it is something to learn from here:

"The readyState value of 4 means the operation completed successfully or failed. The status property is initialized to 0 and will remain at 0 if an error occurs."



Για να απεγγραφείτε απ' αυτή την ομάδα και όλα τα θέματά της, στείλτε ένα μήνυμα ηλεκτρονικού ταχυδρομείου στη διεύθυνση so...@googlegroups.com.

Periklis Argiriadis

unread,
Jun 19, 2020, 1:22:45 PM6/19/20
to Softone Developers Network
Did you tried and you got these returns;

Liviu Buligan

unread,
Jun 19, 2020, 1:56:46 PM6/19/20
to Softone Developers Network
With http - like you described
With https - like I described
Of course I tried.

Periklis Argiriadis

unread,
Jun 19, 2020, 2:21:40 PM6/19/20
to Softone Developers Network
Something weird is happening here.
I have no difference with http or https.

I thought maybe the problem is at the host machine. All my experiments are in a client machine. I tried running the same from server machine and i get

1
4
12029 (!!!!!!)

Hahaha, i cannot give any logical explanation

Periklis Argiriadis

unread,
Jun 20, 2020, 4:43:04 AM6/20/20
to Softone Developers Network
I figured out finally.
It was due to firewall restriction. Firewall drops off connection to telegram.org. After hours and hours of testing and trial-error procedures i discovered the guilty one!

Now i am searching a way, the sendMessageTelegram function to be called every time and not only the first one.
I call the following code
var dm = new sendMessageTelegram('Hello!');

dm
.execute();
and it send "Hello!" message. After a change on form and re-submit, the function is successfully completed but no message is sent. I get a responseText with the same MessageID every time. If i change the message text ( sendMessageTelegram('Test'); ) or restart SoftONE application, it sends it as a new message (with increment MessageID of course).

I read that it cannot be called the send() twice but a new instance should be created. https://stackoverflow.com/questions/8635796/send-method-in-msxml2-xmlhttp-not-sending-second-time
I don't know any way to release or remove the instance from memory every time a successful send is completed.

Periklis Argiriadis

unread,
Jun 20, 2020, 4:58:22 AM6/20/20
to Softone Developers Network
After studying the https://stackoverflow.com/questions/5235464/how-to-make-microsoft-xmlhttprequest-honor-cache-control-directive
i made a little change to my code and voila!

change MSXML2.XMLHTTP ==> MSXML2.ServerXMLHTTP
add the RequestHeader for cache-control

this.request = new ActiveXObject("MSXML2.ServerXMLHTTP");
this.request.open('GET', url, true);
this.request.setRequestHeader("Cache-Control", "max-age=0");

Thanks @Liviu, i really appreciate your help :)

Liviu Buligan

unread,
Jun 20, 2020, 6:07:32 AM6/20/20
to Softone Developers Network
I'm glad you finally managed. Good job and very useful lesson to learn about avoiding caching!
Is Telegram do push the message to the user? I am looking for a solution to get notification on mobile based on S1 events. EVA was one idea but i don't see any notification from it...

Liviu Buligan

unread,
Jun 20, 2020, 7:41:45 AM6/20/20
to Softone Developers Network
This Telegram API is doomed
I crate my own bot and chat_id,  the best i can do (with your code help) is an 502 Bad Gateway error
and of course from the browser command line is working perfect

WTF?

Periklis Argiriadis

unread,
Jun 20, 2020, 8:00:52 AM6/20/20
to Softone Developers Network
(You catch up me, haha, i started to write but i got a telephone break so meanwhile you did the process)

Exactly. EVA cannot push notifications for a reason. Maybe because it's a bot and not real person? I don't know.

But with Telegram (we use it for internal purposes inside our company) it seems easy (finally) to implement this using your own custom bot.

To give you a hint, if you want to create and use private channel, you can do it in this way:
1. Make your channel public
2. Send a message through web browser and get the channel id (it starts by -100xxxxx)
3. Make your channel private again and use the channel id you have (without using @ at the beginning)

Here is my final working code to test

function sendMessageTelegram(msg) {
this.msg = encodeURIComponent(msg) || "";
this.onSuccess = function(){};
this.onNoReply = function(){};
this.onError= function(){}
this.request= null;
var me = this,
url;
this.execute =
function() {
var apiToken = encodeURIComponent("xxxx:xxxxxxx");
var chatId = encodeURIComponent("-100zzzzzzzzzz");

url = 'https://api.telegram.org/bot'+ apiToken + '/sendMessage?chat_id='+ chatId + '&text='+ msg;
this.request = new ActiveXObject("MSXML2.ServerXMLHTTP");
this.request.open('GET', url, true);
this.request.setRequestHeader("Cache-Control", "max-age=0");
this.request.onreadystatechange=
  function() {
      //X.WARNING("State: " + me.request.readyState);      
    if (me.request.readyState==4) {
//X.WARNING("Status: " + me.request.status);
      if (me.request.status==200)
        me.onSuccess(me.request.responseText);
      else
me.onNoReply();
    }
  }
  try {
    this.request.send();
    X.WARNING("Response: " + this.request.responseText);
  }
  catch(e) {
    this.onError(e);
  }
}
}

function ON_AFTERPOST() {
var dm = new sendMessageTelegram("Your message");
dm.onSuccess =
function(reply) {
  eval('var data = ' + reply);
  if (data.ok) {
    X.WARNING('OK');
  }
  else {
    X.WARNING('ERROR: ' + data.status + '\n\nError Message: '+ data.error_message);
  }
};
dm.execute();
}

I missed a "/" in front of sendMessage in the url in my first post. I fix it in the above code.
Please use the above code and tell me the results.

Liviu Buligan

unread,
Jun 20, 2020, 8:41:36 AM6/20/20
to Softone Developers Network
It worked with a slight different path:
i create the bot with botfather (no mother, huh? :)) ... i assume they know about EVA...)
i start a message from the web client (https://web.telegram.org/)
i run from google address bar https://api.telegram.org/bot<apicode>/getupdates - that shows in the results the chat_id
chat":{"id":NNNNNNNNNNNNN,"first_name":"Liviu","last_name":"Buligan","type":"private"}

in my case chat_id was "120525.....", so no negative number and no 100 in front

i have used your code and it works like a dream!

Thanks for the contribution!

BTW - the missing " / " was concluding to Bad Gateway... so this is fixed as well, everybody happy :)

Periklis Argiriadis

unread,
Jun 20, 2020, 9:10:08 AM6/20/20
to Softone Developers Network
Happy to hear that it worked :)
Somewhere that i cannot recall right now, i read that the channel ids are negative values starting with -100, like mine, but maybe it was an old bit of information. Anyway, it works so as you said, everyone is happy :)

The Botfather is watching everything, so he knows about EVA too, ssssssh :)

Pericles Argyriades

unread,
Jun 22, 2020, 2:56:30 AM6/22/20
to Softone Developers Network
I just find out that the script doesn't work when using s360 environment. Neither mobile app nor through web browser. I suppose that it cannot process the command

this.request = new ActiveXObject("MSXML2.ServerXMLHTTP");

It is processed only in windows environment and it doesn't work through web services, like s360 is working.

First of all, it should be found or created a way to call http requests through Form Script when using s360.
Secondly, either a universal way of running a http request should be found, or it should be a command (i'll baptise it X.SYS.MOBILE) so to check if we are in windows environment and use the previous script as it is, or we are in web services environment so another way should be used.

Liviu Buligan

unread,
Jun 22, 2020, 7:35:18 AM6/22/20
to Softone Developers Network
Hi Periklis, 
i just implement the following flow:
website->WS-> CreateOrder->On_post->CreateTelegramMsg so this works

It is very true that the process
Picking -> On_Post->HTTP call to Update_website_order_ status is working ONLY from desktop and NOT fr picking docs created with S360. 

Looking forward having another option!


Liviu

Pericles Argyriades

unread,
Jun 23, 2020, 12:31:22 PM6/23/20
to Softone Developers Network
Hey Liviu,
where do you call On_post in the first scenario? Do you have a website that using WebServices, you create an order and the on_post is called but where do you have the code for on_post function? As form script in S1?

Liviu Buligan

unread,
Jun 23, 2020, 1:57:50 PM6/23/20
to Softone Developers Network
I mainly use AdvancedJs not form. So it is not a script on form.

Pericles Argyriades

unread,
Jun 23, 2020, 2:04:16 PM6/23/20
to Softone Developers Network
Yes, i haven't use it but it maybe uses the same principles right?
For example, you are still using ActiveXObject("MSXML2.ServerXMLHTTP"), so this does no difference, right ?

Liviu Buligan

unread,
Jun 23, 2020, 2:19:36 PM6/23/20
to Softone Developers Network
I stopped using form script for several reasons:
1. Advanced JS is triggered on any form, including built in ones
2. It is much more easier to debug
3. i can implement much more easier functions running on all modules 

nevertheless... in very specific cases you can write code on form level that will run instead the Advance one

The script is 100% compatible with the form one
so yes,m i am using the same activeX object

i am reviewing my code now and i see i am using to call the Telegram call ON_AFTERPOST(). I did it as i need the newid of the document.

Maybe this is the reason???

I do not completely understand where is executed this JS code physically.... normally it should be on the machine that is hosting the S1 agent.

Pericles Argyriades

unread,
Jun 24, 2020, 12:34:21 PM6/24/20
to Softone Developers Network
"Where is executed this JS code physically" ?
That is really a good question.

Obviously an answer would be indeed at the hosting of S1 agent. But if it happens here, it shouldn't make any difference executing the script from desktop versus web environment.
If you try to insert a X.WARNING('test') just in place above or below the ActiveX object call, you will see that it is executed normally, either in desktop or web. So the ON_AFTERPOST() seems to be called in all cases.
Reply all
Reply to author
Forward
0 new messages