Exemple #1
0
        private void AddReaction(Action <ReactionAddedResponse> callback, string name = null, string channel = null, string timestamp = null)
        {
            List <Tuple <string, string> > parameters = new List <Tuple <string, string> >();

            if (!string.IsNullOrEmpty(name))
            {
                parameters.Add(new Tuple <string, string>("name", name));
            }

            if (!string.IsNullOrEmpty(channel))
            {
                parameters.Add(new Tuple <string, string>("channel", channel));
            }

            if (!string.IsNullOrEmpty(timestamp))
            {
                parameters.Add(new Tuple <string, string>("timestamp", timestamp));
            }

            client.APIRequestWithToken(callback, parameters.ToArray());
        }
Exemple #2
0
        private void PullSlackMessagesAndOutputToFile()
        {
            var messages = new List <JObject>();

            if (System.IO.File.Exists("SlackChat.json"))
            {
                messages = JsonConvert.DeserializeObject <List <JObject> >(System.IO.File.ReadAllText("SlackChat.json"));
                if (messages == null)
                {
                    messages = new List <JObject>();
                }
            }

            // _slackClient = new SlackSocketClient("TODO");
            _slackClient.Connect((connected) => {
                // This is called once the client has emitted the RTM start command
                Log(null, "Connected to Slack, requesting emojis");
                _slackClient.APIRequestWithToken <EmojiListResponse>((resp) =>
                {
                    _slackEmojis = resp.emoji;

                    lock (_messagesLock)
                    {
                        Log(null, "Generating Slack HTML");
                        System.IO.File.WriteAllText("SlackChat.htm", GenerateHtmlMessages(messages));
                    }
                });
            }, () => {
                // This is called once the RTM client has connected to the end point
            });
            _slackClient.OnMessageReceived += (messageObj) =>
            {
                Log(null, "Received message from Slack");

                var messageRaw = JsonConvert.DeserializeObject <JObject>(JsonConvert.SerializeObject(messageObj));

                var message          = JsonConvert.DeserializeObject <NewMessage>(JsonConvert.SerializeObject(messageRaw));
                var fileShareMessage = JsonConvert.DeserializeObject <FileShareMessage>(JsonConvert.SerializeObject(messageRaw));

                if (message.channel == "CPY2WM43U")
                {
                    Log(null, "Message was in correct channel");

                    Action complete = () =>
                    {
                        lock (_messagesLock)
                        {
                            messages.Add(messageRaw);

                            Log(null, "Generating Slack HTML");

                            System.IO.File.WriteAllText("SlackChat.json", JsonConvert.SerializeObject(messages));
                            System.IO.File.WriteAllText("SlackChat.htm", GenerateHtmlMessages(messages));
                        }
                    };

                    if (message.subtype == "file_share")
                    {
                        Log(null, "Slack message contained file");

                        if (fileShareMessage.file.mimetype.Contains("image"))
                        {
                            Log(null, "Sharing Slack image file");

                            _slackClient.APIRequestWithToken <FilesSharedPublicURLResponse>((resp) =>
                            {
                                Task.Run(async() =>
                                {
                                    // Wait for Slack to make the file public.
                                    await Task.Delay(1500);
                                    fileShareMessage.file = resp.file;
                                    complete();
                                });
                            }, new Tuple <string, string>("file", fileShareMessage.file.id));
                        }
                        else
                        {
                            complete();
                        }
                    }
                    else
                    {
                        complete();
                    }
                }
            };
        }