HTTP requests with cBot Cloud instance

Created at 10 Aug 2024, 19:43
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ZI

zinger6673

Joined 10.08.2024

HTTP requests with cBot Cloud instance
10 Aug 2024, 19:43


Hello, When running a cBot with a cloud instance is it possible to make a HTTP request? 

When i run this code in locally it works but I need to give it full access so it can access the required DLL.

But when i try to run it in the cloud i cant because it uses fullaccess.
Is there any way i can do this or is there a workaround?

Thanks

using System.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;
using System.Net;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.FullAccess, AddIndicators = true)]
    public class TESTlistenonly : Robot
    { 
        public HttpClient client;
        public string uri;
        
        protected override void OnStart() {

            uri = "webhook";
            
            HttpClientHandler h = new HttpClientHandler {
                UseCookies = false
            };
            client = new HttpClient(h);
            SendMessage("test");
        }
        
        void SendMessage(string message) {
            
            try
            {
                var payload = new
                {
                    username = "Alert",
                    avatar_url = "",
                    content = "<@109213512366071808> " + message
                };

                string jsonPayload = JsonSerializer.Serialize(payload);

                var request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = "application/json";

                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(jsonPayload);
                    streamWriter.Flush();
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        string responseContent = streamReader.ReadToEnd();
                        Print("Response: " + responseContent);
                    }
                }
            }
            catch (Exception ex)
            {
                Print("Error: " + ex.Message);
            }
        }
        
        

        protected override void OnTick()
        {
            // Handle price updates here
        }

        protected override void OnStop()
        {
            // Handle cBot stop here
        }
    }
}

@zinger6673
Replies

firemyst
11 Aug 2024, 11:02 ( Updated at: 11 Aug 2024, 15:29 )

Your bot probably won't work as you want because if you read this page:

https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#compile-time-references

http requests aren't sent when bots run in the cloud.

 


@firemyst

PanagiotisCharalampous
12 Aug 2024, 05:03

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis


@PanagiotisCharalampous

zinger6673
13 Aug 2024, 20:40 ( Updated at: 14 Aug 2024, 09:58 )

RE: HTTP requests with cBot Cloud instance

PanagiotisCharalampous said: 

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis

Thanks for the reply, If internet access is not permitted then why does it say here https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#api-features that we can connect to a WebSocket server as long its port 25345. Is this article outdated or is it possible?
 

using System;
using cAlgo.API;
namespace cAlgo.Robots {
    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]
    public class TESTlistenonly : Robot {
        public WebSocketClient client;
        public string uri;
        
        protected override void OnStart() {
            Print("attempting connection");
            client = new WebSocketClient();
            var uri = new Uri("wss://server:25345/");
            client.Connect(uri);
            Print("WebSocket connection opened");
        }
    }
}

I have my own server running on port 25345 and it works on local execution but on the cloud it says this


@zinger6673

PanagiotisCharalampous
14 Aug 2024, 10:08

RE: RE: HTTP requests with cBot Cloud instance

zinger6673 said: 

PanagiotisCharalampous said: 

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis

Thanks for the reply, If internet access is not permitted then why does it say here https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#api-features that we can connect to a WebSocket server as long its port 25345. Is this article outdated or is it possible?
 

using System;using cAlgo.API;namespace cAlgo.Robots {    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]    public class TESTlistenonly : Robot {        public WebSocketClient client;        public string uri;                protected override void OnStart() {            Print("attempting connection");            client = new WebSocketClient();            var uri = new Uri("wss://server:25345/");            client.Connect(uri);            Print("WebSocket connection opened");        }    }}

I have my own server running on port 25345 and it works on local execution but on the cloud it says this

There was no mention of web sockets in the initial post. Yes, it's possible to use web sockets through port 25345. Does your server listen to this port?


@PanagiotisCharalampous

zinger6673
14 Aug 2024, 12:43 ( Updated at: 15 Aug 2024, 05:20 )

RE: RE: RE: HTTP requests with cBot Cloud instance

PanagiotisCharalampous said: 

zinger6673 said: 

PanagiotisCharalampous said: 

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis

Thanks for the reply, If internet access is not permitted then why does it say here https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#api-features that we can connect to a WebSocket server as long its port 25345. Is this article outdated or is it possible?
 

using System;using cAlgo.API;namespace cAlgo.Robots {    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]    public class TESTlistenonly : Robot {        public WebSocketClient client;        public string uri;                protected override void OnStart() {            Print("attempting connection");            client = new WebSocketClient();            var uri = new Uri("wss://server:25345/");            client.Connect(uri);            Print("WebSocket connection opened");        }    }}

I have my own server running on port 25345 and it works on local execution but on the cloud it says this

There was no mention of web sockets in the initial post. Yes, it's possible to use web sockets through port 25345. Does your server listen to this port?

Hello Panagiotis, thank you for the reply. Sorry I meant to say in my reply that I switched to trying WebSockets after i found out in the article no HTTP requests won't be sent. And yes my server does listen to post 25345, it works on local execution and i check it on a ping test. It's also using SSH. If you would like to try its up right now on wss://araxy.co.uk:25345/

 

Cloud execution

Local execution

Browser websocket test


@zinger6673

PanagiotisCharalampous
16 Aug 2024, 12:43

RE: RE: RE: RE: HTTP requests with cBot Cloud instance

zinger6673 said: 

PanagiotisCharalampous said: 

zinger6673 said: 

PanagiotisCharalampous said: 

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis

Thanks for the reply, If internet access is not permitted then why does it say here https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#api-features that we can connect to a WebSocket server as long its port 25345. Is this article outdated or is it possible?
 

using System;using cAlgo.API;namespace cAlgo.Robots {    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]    public class TESTlistenonly : Robot {        public WebSocketClient client;        public string uri;                protected override void OnStart() {            Print("attempting connection");            client = new WebSocketClient();            var uri = new Uri("wss://server:25345/");            client.Connect(uri);            Print("WebSocket connection opened");        }    }}

I have my own server running on port 25345 and it works on local execution but on the cloud it says this

There was no mention of web sockets in the initial post. Yes, it's possible to use web sockets through port 25345. Does your server listen to this port?

Hello Panagiotis, thank you for the reply. Sorry I meant to say in my reply that I switched to trying WebSockets after i found out in the article no HTTP requests won't be sent. And yes my server does listen to post 25345, it works on local execution and i check it on a ping test. It's also using SSH. If you would like to try its up right now on wss://araxy.co.uk:25345/

 

Cloud execution

Local execution

Browser websocket test

Try wss://server:25345 instead of wss://server:25345/


@PanagiotisCharalampous

zinger6673
16 Aug 2024, 16:31 ( Updated at: 16 Aug 2024, 18:31 )

RE: RE: RE: RE: RE: HTTP requests with cBot Cloud instance

PanagiotisCharalampous said: 

zinger6673 said: 

PanagiotisCharalampous said: 

zinger6673 said: 

PanagiotisCharalampous said: 

Hi there,

No, internet access is not permitted for cBots executed on the cloud.

Best regards,

Panagiotis

Thanks for the reply, If internet access is not permitted then why does it say here https://help.ctrader.com/ctrader-algo/synchronisation/requirements-for-cbots/#api-features that we can connect to a WebSocket server as long its port 25345. Is this article outdated or is it possible?
 

using System;using cAlgo.API;namespace cAlgo.Robots {    [Robot(AccessRights = AccessRights.None, AddIndicators = true)]    public class TESTlistenonly : Robot {        public WebSocketClient client;        public string uri;                protected override void OnStart() {            Print("attempting connection");            client = new WebSocketClient();            var uri = new Uri("wss://server:25345/");            client.Connect(uri);            Print("WebSocket connection opened");        }    }}

I have my own server running on port 25345 and it works on local execution but on the cloud it says this

There was no mention of web sockets in the initial post. Yes, it's possible to use web sockets through port 25345. Does your server listen to this port?

Hello Panagiotis, thank you for the reply. Sorry I meant to say in my reply that I switched to trying WebSockets after i found out in the article no HTTP requests won't be sent. And yes my server does listen to post 25345, it works on local execution and i check it on a ping test. It's also using SSH. If you would like to try its up right now on wss://araxy.co.uk:25345/

 

Cloud execution

Local execution

Browser websocket test

Try wss://server:25345 instead of wss://server:25345/

Thank you for the reply, I'm still getting the same error message, double checked the server and ran it locally again and it works fine. I think it has to be something to do with the cloud instance


@zinger6673