Last active
August 29, 2015 14:08
-
-
Save ShawkyZ/73a43cf3915941a07e3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Json; | |
using System.Net; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Xml; | |
using System.Xml.Linq; | |
using System.Text.RegularExpressions; | |
namespace ConsoleApplication3 | |
{ | |
public class User | |
{ | |
public string id { get; set; } | |
public string username { get; set; } | |
public string url { get; set; } | |
public string avatarUrlSmall { get; set; } | |
public string avatarUrlMedium { get; set; } | |
public string role { get; set; } | |
} | |
public class Mention | |
{ | |
public string screenName { get; set; } | |
public string userId { get; set; } | |
} | |
public class issue | |
{ | |
public string number { get; set; } | |
} | |
public class URL | |
{ | |
public string url { get; set; } | |
} | |
public class Message | |
{ | |
public string id { get; set; } | |
public string text { get; set; } | |
public string html { get; set; } | |
public string sent { get; set; } | |
public string editedAt { get; set; } | |
public User fromUser { get; set; } | |
public bool unread { get; set; } | |
public int readBy { get; set; } | |
public List<URL> urls { get; set; } | |
public List<Mention> mentions { get; set; } | |
public List<issue> issues { get; set; } | |
public string meta { get;set; } | |
public string v { get; set; } | |
} | |
class Program | |
{ | |
static List<User> oldusers = new List<User>(); | |
static void Main(string[] args) | |
{ | |
System.Threading.Thread th=new System.Threading.Thread (CheckCurrentUsers); | |
th.Start(); | |
System.Threading.Thread th2 = new System.Threading.Thread(GetMessages); | |
th2.Start(); | |
} | |
static void GetMessages() | |
{ | |
List<Message> postedids = new List<Message>(); | |
List<Message> mymessages; | |
while (true) | |
{ | |
System.Threading.Thread.Sleep(5000); | |
using (var wb = new WebClient()) | |
{ | |
//TODO:Send HTTP Get Request To Get The Last Messages. | |
wb.Headers.Add("Authorization", "Bearer 58e579a62047739c3839063d8b8e4c7d64588977"); //This is the Access Token I Got. | |
wb.Headers.Add("Host", "api.gitter.im"); | |
var response = wb.DownloadString("https://api.gitter.im/v1/rooms/54577284db8155e6700d0a73/chatMessages?limit=1"); //I set the limit to 1 to get the last message ony , you can remove it and get the last 50 messages. | |
//TODO:Deserialize the Json Response To List Of Messages. | |
DataContractJsonSerializer contract = new DataContractJsonSerializer(typeof(List<Message>)); | |
MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(response)); | |
mymessages = (List<Message>)contract.ReadObject(stream); | |
} | |
for (int i = 0; i < mymessages.Count; i++) | |
{ | |
//Check if there are any mentions in this message with the screenName "testbot23" which is the bot screen name. | |
if(mymessages[i].mentions.Count>0) | |
if (mymessages[i].mentions[0].screenName == "testbot23") | |
{ | |
//Check if I posted this message before to not post it again. | |
bool posted = postedids.Any(x => x.id == mymessages[i].id); | |
if (!posted) | |
{ | |
WebClient wb = new WebClient(); | |
//Check if the user wanted the bot to tell him a quote, if he didn't the bot will just say "Yo". | |
if (mymessages[i].text.ToLower().Contains("compile")) | |
{ | |
//Get Random quote from all-famous-quotes.com/quotes.generator.html | |
string[] langandcode = mymessages[i].text.ToLower().Split(' '); | |
var data = new NameValueCollection(); | |
data["utf8"] = "%CE%BB"; | |
try | |
{ | |
string code = mymessages[i].text.Replace(langandcode[0]+" " + langandcode[1]+" " + langandcode[2]+" " + langandcode[3], "").Remove(0,1); | |
data["code"] = code; | |
} | |
catch { break; } | |
data["execute"] = "on"; | |
switch (langandcode[3]) | |
{ | |
case "c": | |
data["lang"] = "c/gcc-4.9.1"; | |
break; | |
case "c++": | |
data["lang"] = "c++/c++11-gcc-4.9.1"; | |
break; | |
case "coffescript": | |
data["lang"] = "coffeescript/node-0.10.29-coffee-1.7.1"; | |
break; | |
case "fortran": | |
data["lang"] = "fortran/f95-4.4.3"; | |
break; | |
case "haskell": | |
data["lang"] = "haskell/hugs98-sep-2006"; | |
break; | |
case "io": | |
data["lang"] = "io/io-20131204"; | |
break; | |
case "javascript": | |
data["lang"] = "javascript/node-0.10.29"; | |
break; | |
case "lua": | |
data["lang"] = "lua/lua-5.2.3"; | |
break; | |
case "ocaml": | |
data["lang"] = "ocaml/ocaml-4.01.0"; | |
break; | |
case "php": | |
data["lang"] = "php/php-5.5.14"; | |
break; | |
case "pascal": | |
data["lang"] = "pascal/fpc-2.6.4"; | |
break; | |
case "perl": | |
data["lang"] = "perl/perl-5.20.0"; | |
break; | |
case "python": | |
data["lang"] = "python/cpython-3.4.1"; | |
break; | |
case "ruby": | |
data["lang"] = "ruby/mri-2.1"; | |
break; | |
case "slash": | |
data["lang"] = "slash/slash-head"; | |
break; | |
case "assembly": | |
data["lang"] = "assembly/nasm-2.07"; | |
break; | |
} | |
data["input"] = ""; | |
wb.Headers.Add("Host", "eval.in"); | |
wb.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"); | |
wb.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |
wb.Headers.Add("Accept-Language", "en-US,en;q=0.5"); | |
wb.Headers.Add("Referer", "https://eval.in/"); | |
byte[] response; | |
try | |
{ | |
response = wb.UploadValues("https://eval.in", "POST", data); | |
} | |
catch { break; } | |
string htmlraw = System.Text.Encoding.UTF8.GetString(response); | |
string res1 = htmlraw.Substring(htmlraw.IndexOf("<h2>Program Output</h2>")); | |
string res2 = res1.Remove(res1.IndexOf("<h2>Fork</h2>")); | |
string finalres = Regex.Replace(res2, "<.*?>", string.Empty); | |
postMessage(finalres + "@" + mymessages[i].fromUser.username); | |
} | |
else if (mymessages[i].text.ToLower().Contains("define")) | |
{ | |
string after=mymessages[i].text.Substring(mymessages[i].text.IndexOf("define")).Replace("define", "").Replace(" ",""); | |
string response=""; | |
try | |
{ | |
response = wb.DownloadString("http://www.merriam-webster.com/dictionary/" + after); | |
} | |
catch { | |
postMessage("Sorry,There is no definition for this world. " + "@" + mymessages[i].fromUser.username); | |
break; | |
} | |
int cutIndex = response.IndexOf("<div class=\"ld_on_collegiate\">"); | |
if (cutIndex<0) | |
{ | |
postMessage("Sorry,There is no definition for this world. " + "@"+mymessages[i].fromUser.username); | |
break; | |
} | |
string raw1 = response.Substring(cutIndex); | |
string definition = raw1.Remove(raw1.IndexOf("<p class=\"bottom_entry\">")).Replace("<div class=\"ld_on_collegiate\">", "").Replace("<p>","").Replace("</p>","").Replace("\r","").Replace("\n",""); | |
postMessage("The definition for " + after + " is " + definition + "\nSource:http://www.merriam-webster.com/dictionary/"+after+" @" + mymessages[i].fromUser.username); | |
} | |
else if (mymessages[i].text.ToLower().Contains("quote")) | |
{ | |
//Get Random quote from all-famous-quotes.com/quotes.generator.html | |
string response = wb.DownloadString("http://www.all-famous-quotes.com/quotes_generator.html"); | |
string raw1 = response.Substring(response.IndexOf("<blockquote class=\"new\">")); | |
string quote=raw1.Remove(raw1.IndexOf("- <a href=\"http://www.all-famous-quotes.com")).Replace("<blockquote class=\"new\">", ""); | |
postMessage(quote + "@"+mymessages[i].fromUser.username); | |
} | |
else if(mymessages[i].text.ToLower().Contains("joke")) | |
{ | |
//Get Random Joke | |
string rawhtml = wb.DownloadString("http://www.ajokeaday.com/ChisteAlAzar.asp"); | |
string raw1 = rawhtml.Substring(rawhtml.IndexOf(("<div class=\"chiste\">"))); | |
string raw2 = raw1.Remove(raw1.IndexOf("<div class=\"opciones\">")); | |
string joke = Regex.Replace(raw2, "<.*?>", string.Empty); | |
postMessage(joke + " @" + mymessages[i].fromUser.username); | |
} | |
else | |
{ | |
postMessage("Yo, @" + mymessages[i].fromUser.username); | |
} | |
} | |
//Mark this message as posted. | |
postedids.Add(mymessages[i]); | |
} | |
} | |
} | |
} | |
static void CheckCurrentUsers() | |
{ | |
while (true) | |
{ | |
System.Threading.Thread.Sleep(5000); | |
List<User> myusers; | |
using (var wb = new WebClient()) | |
{ | |
//TODO:Send HTTP Get Request To Get The Current Users. | |
wb.Headers.Add("Authorization", "Bearer 87eaf739c5c4c90707df73a938d435c5f9e5cca3"); //This is the Access Token I Got. | |
wb.Headers.Add("Host", "api.gitter.im"); | |
var response = wb.DownloadString("https://api.gitter.im/v1/rooms/54577284db8155e6700d0a73/users"); | |
//TODO:Deserialize the Json Response To List Of Users. | |
DataContractJsonSerializer contract = new DataContractJsonSerializer(typeof(List<User>)); | |
MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(response)); | |
myusers = (List<User>)contract.ReadObject(stream); | |
} | |
bool newUser = false; | |
//Check There are any new users with this O(n^2) horrible algorithm. | |
for (int i = 0; i < myusers.Count; i++) | |
{ | |
newUser = false; | |
for (int j = 0; j < oldusers.Count; j++) | |
{ | |
if (myusers[i].id != oldusers[j].id) | |
{ | |
newUser = true; | |
} | |
else | |
{ | |
newUser = false; | |
break; | |
} | |
} | |
if (newUser) | |
{ | |
//If There is a new user post a welcome Message | |
postMessage("Welcome @" + myusers[i].username + "\n `You can ask me to say a quote, define a word or compile some code. Just mention me`"); | |
} | |
} | |
oldusers = myusers; | |
} | |
} | |
static void postMessage(string msg) | |
{ | |
WebClient wb = new WebClient(); | |
var data = new NameValueCollection(); | |
wb.Headers.Add("Authorization", "Bearer 58e579a62047739c3839063d8b8e4c7d64588977"); | |
wb.Headers.Add("Accept", "application/json"); | |
data["text"] = msg; | |
wb.UploadValues("https://api.gitter.im/v1/rooms/54577284db8155e6700d0a73/chatMessages", "POST", data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment