Send text from calgo to Twitter

Created at 30 Nov 2015, 19:35
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!
breakermind's avatar

breakermind

Joined 17.07.2013

Send text from calgo to Twitter
30 Nov 2015, 19:35


Hi, how send tweet from calgo cbot ?

Thanks.


@breakermind
Replies

Spotware
30 Nov 2015, 20:08

Dear Trader,

There is no method offered in cAlgo to post messages to social networking sites. However, you can take advantage of the C# language used in cAlgo and you can write some methods of your own to achieve it. A simple search using any search engine will show you several examples on how to post something to a social networking site.   


@Spotware

breakermind
30 Nov 2015, 20:38

RE:

Spotware said:

Dear Trader,

There is no method offered in cAlgo to post messages to social networking sites. However, you can take advantage of the C# language used in cAlgo and you can write some methods of your own to achieve it. A simple search using any search engine will show you several examples on how to post something to a social networking site.   

I hope that someone already wrote a simple example :). I do not want again to look for the simplest solutions.

Thanks for Your time.


@breakermind

ClickAlgo
30 Nov 2015, 20:47

Its pretty simple to send a tweet with C# using twitter API, you need to open a twitter account that you want to post from and get your authentication codes, it has been a while since i worked on the twitter API, but it is something like this:

// TwitterCredentials.SetCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
TwitterCredentials.SetCredentials("2252688033-b20s6VyUbgMhhq60mgt8H5i1FlT2IkG", "Gb6q86w7WZR0BCFbPbeRMLLbxcMKowODjN8Ho4SdW", "1EDHuiqKRaor771Fu7SCy", "pguFU6zPYALGBtReR5vElfBLM78PLoSKbI23oGudh");

var tweet = Tweet.CreateTweet("this is your tweet");
var success = tweet.Publish();

you can also tweet with media images, so you can auto tweet every time you win a trade with a screen shot if your clever.

have fun.


@ClickAlgo

ClickAlgo
30 Nov 2015, 20:49 ( Updated at: 21 Dec 2023, 09:20 )

Also you will what these references or get them from NuGet.


@ClickAlgo

breakermind
30 Nov 2015, 21:26

RE:

I found this (some errors recive) but send my text


        //function to post message to twitter (parameter string message)
        private void SendToTwitter(string message)
        {
            // Add this references first
            //using System.Net;
            //using System.IO;
            //using System.Security.Cryptography;
            //using System.Text;

            //The facebook json url to update the status
            string URL = "https://api.twitter.com/1.1/statuses/update.json";

            //set the access tokens (REQUIRED)
            string oauth_consumer_key = "";
            string oauth_consumer_secret = "";
            string oauth_token = "";
            string oauth_token_secret = "";

            // set the oauth version and signature method
            string oauth_version = "1.0";
            string oauth_signature_method = "HMAC-SHA1";

            // create unique request details
            string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            System.TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
            string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            // create oauth signature
            string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

            string baseString = string.Format(baseFormat, oauth_consumer_key, oauth_nonce, oauth_signature_method, oauth_timestamp, oauth_token, oauth_version, Uri.EscapeDataString(message));

            string oauth_signature = null;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret))))
            {
                oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes("POST&" + Uri.EscapeDataString(URL) + "&" + Uri.EscapeDataString(baseString))));
            }

            // create the request header
            string authorizationFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " + "oauth_version=\"{6}\"";

            string authorizationHeader = string.Format(authorizationFormat, Uri.EscapeDataString(oauth_consumer_key), Uri.EscapeDataString(oauth_nonce), Uri.EscapeDataString(oauth_signature), Uri.EscapeDataString(oauth_signature_method), Uri.EscapeDataString(oauth_timestamp), Uri.EscapeDataString(oauth_token), Uri.EscapeDataString(oauth_version));

            HttpWebRequest objHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
            objHttpWebRequest.Headers.Add("Authorization", authorizationHeader);
            objHttpWebRequest.Method = "POST";
            objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            using (Stream objStream = objHttpWebRequest.GetRequestStream())
            {
                byte[] content = ASCIIEncoding.ASCII.GetBytes("status=" + Uri.EscapeDataString(message));
                objStream.Write(content, 0, content.Length);
            }

            var responseResult = "";
            try
            {
                //success posting
                WebResponse objWebResponse = objHttpWebRequest.GetResponse();
                StreamReader objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
                responseResult = objStreamReader.ReadToEnd().ToString();
                Print(responseResult);
            } catch (Exception ex)
            {
                //throw exception error
                responseResult = "Twitter Post Error: " + ex.Message.ToString() + ", authHeader: " + authorizationHeader;
                Print(responseResult);
            }
        }

Why?


@breakermind

breakermind
30 Nov 2015, 21:43

RE: RE:

Send msg and display this (but very slow):

30/11/2015 19:41:01.433 | Twitter Post Error: Input string was not in a correct format., authHeader:


@breakermind

ClickAlgo
30 Nov 2015, 22:07

i think the API call is a few lines of code, i do not know what you have there. I looked at my old code and it was simply less than 5 lines of code.

have you looked at the Twitter API?

another example

var service = new TweetSharp.TwitterService("ConsumerKey","ConsumerSecret","TokenKey","TokenSecretKey"); //Replace keys with values from step #5
var twitterStatus = service.SendTweet(new SendTweetOptions() { Status ="Hello World" });
if (twitterStatus != null)
{
    MessageBox.Show("It worked");
}

@ClickAlgo

ClickAlgo
30 Nov 2015, 22:11

I was using TweetInvi wrapper

https://tweetinvi.codeplex.com/

add the Nuget pachage to VS

https://www.nuget.org/packages/TweetinviAPI/

you will get the references I showed you above in your solution then simply write 3 meaningfully clear lines of code to send a tweet.

TwitterCredentials.SetCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
 
var tweet = Tweet.CreateTweet("this is your tweet");
var success = tweet.Publish();

good luck.


@ClickAlgo

mindbreaker
01 Dec 2015, 09:57

RE:

Paul_Hayes said:

I was using TweetInvi wrapper

https://tweetinvi.codeplex.com/

add the Nuget pachage to VS

https://www.nuget.org/packages/TweetinviAPI/

you will get the references I showed you above in your solution then simply write 3 meaningfully clear lines of code to send a tweet.

TwitterCredentials.SetCredentials("Access_Token", "Access_Token_Secret", "Consumer_Key", "Consumer_Secret");
 
var tweet = Tweet.CreateTweet("this is your tweet");
var success = tweet.Publish();

good luck.

Thanks,

but if i copy my cbot to another pc where i have not instaled TweetinviApi  it will work or I need add TweetAPI (I prefer versions whitout external api i dont know who write it and how)?

 


@mindbreaker

ClickAlgo
01 Dec 2015, 17:58

If you want to copy to a VPS for example; just install the cBot like you would normally would and copy the TweetInvi assemblies across, create a folder in your robots directory where all the bots live and put the library files there so you can share the assemblies with other robots.

in cAlgo click on manage references of your robot then choose Libraries, browse to the folder where the Tweetinvi library files are located and add them, its a very simple and easy procedure.

There are some very good libraries out there which wrap up complex logic for programming languages and provide simple logic for what was once complex tasks, Microsoft has been doing this for years, their framework is just many libraries of wrapped up logic. The TweekInvi makes life very simple and increases productivity and simplicity allowing rapid development of software.

 


@ClickAlgo

breakermind
01 Dec 2015, 18:34

RE:

Paul_Hayes said:

If you want to copy to a VPS for example; just install the cBot like you would normally would and copy the TweetInvi assemblies across, create a folder in your robots directory where all the bots live and put the library files there so you can share the assemblies with other robots.

in cAlgo click on manage references of your robot then choose Libraries, browse to the folder where the Tweetinvi library files are located and add them, its a very simple and easy procedure.

There are some very good libraries out there which wrap up complex logic for programming languages and provide simple logic for what was once complex tasks, Microsoft has been doing this for years, their framework is just many libraries of wrapped up logic. The TweekInvi makes life very simple and increases productivity and simplicity allowing rapid development of software.

 

and then I saw error  can not find Twitter Credentials :D:D

 

My example works ok only i need change message text for each next tweet (or add time to msg).

Thanks for help.

 


@breakermind

Nobody
02 Sep 2016, 15:28 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Hello,

i tried to add the Tweetinvi package to calgo using nuget (by command line) but once package installed i do not see it in .net reference or library...

How do you use it ? 

thanks in advance,

Alex

 

Paul_Hayes said:

Also you will what these references or get them from NuGet.

 


@Nobody

brunoamancio.ti
03 Sep 2016, 15:32 ( Updated at: 21 Dec 2023, 09:20 )

RE: RE:

Get it on Nuget ;) To do that, you can open the bot on visual studio and just add the nuget package ;)

 

aglctid123 said:

Hello,

i tried to add the Tweetinvi package to calgo using nuget (by command line) but once package installed i do not see it in .net reference or library...

How do you use it ? 

thanks in advance,

Alex

 

Paul_Hayes said:

Also you will what these references or get them from NuGet.

 

 


@brunoamancio.ti

shrutigp2909
01 Jul 2020, 12:52 ( Updated at: 21 Dec 2023, 09:22 )

RE:

ClickAlgo said:

Also you will what these references or get them from NuGet.

 

By any chance, can you please provide me dll for these libraries for me to add as "reference" in cTrader ? I don't use VS ( dont' even know much about it) but can add a library in cTrader via "Manage References" and then adding it in cBot like "using Tweetinvi"

 

Will you be kind to help me out ?

 

 


@shrutigp2909