Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Created April 24, 2014 21:41
Show Gist options
  • Save kashwaa/11270516 to your computer and use it in GitHub Desktop.
Save kashwaa/11270516 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facebook;
namespace fbBot
{
public class FB
{
public static FacebookClient client = null;
public static dynamic me;
public static FacebookClient GetClient()
{
if (Program.ongoingAuth)
{
return null;
}
if (client!=null)
{
return client;
}
try
{
client = new FacebookClient(Data.Get("token"));
me=client.Get("me/");
}
catch (Exception)
{
Program.ongoingAuth = true;
GenerateToken();
client = null;
}
return client;
}
public static void GenerateToken()
{
Browser b = new Browser(UrlType.Login);
b.Show();
}
public static string ExtendToken(string oldToken)
{
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = Data.Get("id"),
client_secret = Data.Get("secret"),
grant_type = "fb_exchange_token",
fb_exchange_token = oldToken
});
return result.access_token;
}
public static void MarkNotificationAsRead(string notificationId)
{
var client = GetClient();
if (client!=null&&!Program.ongoingAuth)
{
client.Post(notificationId, new {unread="false" });
}
}
public static string ExtractCommentIdFromLink(string link)
{
return link.Split('?').Last().Split('&').Where(c => c.Contains("comment_id")).First().Split('=').Last();
}
public static string ExtractPostIdFromLink(string link)
{
var parts=link.Split('?').First().Split('/').ToList();
return parts[parts.IndexOf("permalink") + 1];
}
public static dynamic GetComment(string commentId)
{
var client = GetClient();
return client.Get(commentId);
}
public static void ProcessNotification(dynamic notification)
{
if (notification.application.name == "Groups"&&notification.title.ToLower().Contains("mentioned"))
{
var comment = GetComment(ExtractCommentIdFromLink(notification.link));
foreach (var tag in comment.message_tags)
{
if (tag.id == me.id)
{
//if (comment.message.Contains("ping"))
//{
string input = comment.message;
input = "//" + input;
string output = CSCompiler.compile(input);
client.Post(ExtractPostIdFromLink(notification.link) + "/comments", new { message = output });
break;
//}
}
}
}
MarkNotificationAsRead(notification.id);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment