Skip to content

RTM API example for just parroting messages received

Daigo Tsubouchi edited this page May 16, 2020 · 1 revision

RTM API example for just parroting messages received

This is a simple example for receiving a message and sending back the same message, using RTM API.

class RTMParrotExample
    {
        static void Main(string[] args)
        {
            ManualResetEventSlim clientReady = new ManualResetEventSlim(false);
            SlackSocketClient client = new SlackSocketClient(YOUR_AUTH_TOKEN);
            client.Connect((connected) => {
                // This is called once the client has emitted the RTM start command
                clientReady.Set();
            }, () => {
                // This is called once the RTM client has connected to the end point
            });
            client.OnMessageReceived += async (message) =>
            {
                // Handle each message as you receive them
                await Task.Run(() => client.SendMessage(null, message.channel, message.text));
            };
            clientReady.Wait();
            Console.ReadKey();
        }
    }

Note: You must wrap client.SendMessage to async function; otherwise client.OnMessageReceived would be fired only once.