Created
February 3, 2016 14:40
-
-
Save g0dzill3r/9825c3fb26c2fd5dd50c 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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
TT.Win.SDK.Global.Init("[API Key]", "[API Secret]"]); | |
TT.Win.SDK.Api.Events.MessageReceivedEvent += Events_MessageReceivedEvent; | |
TT.Win.SDK.Api.Events.StartListening(); | |
Console.WriteLine("You can post message anytime by entering recipient, message and press <ENTER> (Format: recipientId;message)"); | |
StartSendMessenger(); | |
} | |
private static void StartSendMessenger() | |
{ | |
while (true) //Keep running the program until user presses ENTER or an invalid input | |
{ | |
var input = Console.ReadLine(); | |
var arrInput = input.Split(';'); | |
if (arrInput.Length < 2) //Exit the program if input format is incorrect | |
return; | |
var messageSent = TT.Win.SDK.Api.Message.SendMessage(arrInput[1], arrInput[0]); //Post message | |
if (messageSent.message_id != null) | |
Console.WriteLine("Message sent successfully - " + messageSent.message_id); | |
} //while loop | |
} | |
private async static void Events_MessageReceivedEvent(object sender, TT.Win.SDK.Events.MessageEventArgs e) | |
{ | |
if (e.MessageData.sender_user == null) | |
e.MessageData.sender_user = await TT.Win.SDK.Api.User.GetCachedUserAsync(e.MessageData.sender); | |
Console.WriteLine(string.Format("Message received from {0} : {1}", e.MessageData.sender_user.display_name, e.MessageData.body)); | |
if (e.MessageData.AttachmentsToDownload != null) | |
DownloadFile(e.MessageData); | |
} | |
private static async void DownloadFile(TT.Win.SDK.Model.MessageEventData message) | |
{ | |
foreach (var attachment in message.AttachmentsToDownload) | |
{ | |
var fileName = string.Format("{0}_{1}{2}", message.message_id, attachment.AttachmentSequence, attachment.FileExtension); | |
Console.WriteLine("Downlading attached file " + fileName + " ..."); | |
var fileContents = await attachment.GetFileContents(); | |
var filePath = Path.Combine(@"C:\TTAttachments", fileName); | |
System.IO.File.WriteAllBytes(filePath, fileContents); | |
Console.WriteLine("Downloaded the file at " + folderPath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment