📜 ⬆️ ⬇️

Twitter bot on C #



Hello, dear users of Habrahabr.
In this article I want to tell you about a very simple and useful bot for Twitter, which will help monitor the status of the Windows server (occupied space on disks, percentage of CPU, RAM).

Foreword


I work as a system administrator at a cannery. Since the company is not profile, you have to be an administrator and a programmer in one person.
The reason for creating this bot is a fast-ending place on the server's hard disk due to daily backups of 1C databases.
I have been thinking about possible options for such a control for a long time and came to the conclusion that Twitter is the most convenient solution for this. So, from the word to the case.

Training


Since we will write the bot in C #, we need:
  1. Microsoft Visual Studio 2008-2010 any edition (or SharpDevelop 3.2 - 4.x)
  2. Twitterizer library for working with Twitter in the .NET Framework
  3. A little patience

Development


We download the library from the developer’s site and unpack its contents into the directory with our project. We need only two files - Twitterizer2.dll and Newtonsoft.Json.dll . Add a Twitterizer library to our project. Right mouse button on the project -> Add Reference Select the “Browse” tab and specify the path to Twitterizer2.dll .
')


Add to the main form code

using Twitterizer; 

Now you can go directly to work with Twitter.
Register our application. Thevar1able has written about this well in his article on the Python bot.
We will need 4 keys: Consumer key , Consumer secret , Access token and Access token secret .

All actions are reduced to sending the information we need in the form of a tweet.

  private bool SendTwit(string Twit) { OAuthTokens tokens = new OAuthTokens(); //    tokens.ConsumerKey = "xAWDWV5YE1F5gz3pHw"; // Consumer Key // Consumer Secret tokens.ConsumerSecret = "iSoQ5lYl24BolEdjTte0wnPN4KX9WuGGzJgJivg"; // AccessToken tokens.AccessToken = "370065603-6mRJ8vNHb6tFCKKQhQNxkVR0uy7kK7dFbt2UN"; // Access Token Secret tokens.AccessTokenSecret = "zYT39d8K8SQEm7OxUt1BUCpIZi0VixKElb9go0w"; //    TwitterResponse<TwitterStatus> tweetResponse = TwitterStatus.Update(tokens, Twit ); if (tweetResponse.Result == RequestResult.Success) //  { return true; //  } else { return false; //   } } 


Information about the processor and RAM can be obtained using the PerformanceCounter class, which is in the System.Diagnostics namespace.

 private PerformanceCounter cpuCounter; //       private PerformanceCounter ramCounter; //   cpuCounter = new PerformanceCounter(); // //     ramCounter = new PerformanceCounter("Memory","Available MBytes"); cpuCounter.CategoryName = "Processor"; // cpuCounter.CounterName = "% Processor Time"; //  cpuCounter.InstanceName = "_Total"; // float RAMFree ramCounter.NextValue(); //     float CPULoad ramCounter.NextValue(); //    


As a result, we got something like this:


Original

In the settings window, you can select the status update interval (1, 10, 15, 30, 60 minutes). The ticked data will be sent to Twitter, the list below displays the date and time of sending the tweet, the status of sending and the number of characters in the message.

Conclusion


In the future we plan to implement the following features:
  1. Sending more details via Direct Message
  2. Bot control with Direct Message and Mentinos
  3. Saving settings
  4. Logging

Here are the features that will be implemented in the next version of the bot.
Thank you all for your attention, I hope everyone was interested. I wish you creative success!

Links


Sources of the program
Twitterizer Developer Site
Developer’s Twitterizer documentation

Source: https://habr.com/ru/post/128422/


All Articles