SlackAPI icon indicating copy to clipboard operation
SlackAPI copied to clipboard

Events not firing when using SlackAPI in class library, calling it from console app

Open vikekh opened this issue 8 years ago • 2 comments

I think the title describes my problem well and I do not think this is an issue with the SlackAPI lib, but rather my knowledge. I studied real-time and concurrent programming too many years ago and have very slim knowledge of the .NET implementation.

As my title says, I have a project called SlackClient which worked well when I ran it is a console app, setting up the ManualResetEventSlim object and SlackAPI events in the Main method. I then decided to move everything concerning console output into its own project called App and then creating an instance of SlackClient and set up all events from its constructor.

How ever, now no events are fired and my bot doesn't even connect when I look in my Slack team. I can see that the bot token is picked up and fed to SlackSocketClient.

Any idea how I should solve this? Examplish code:

class Program
{
    static void Main(string[] args)
    {
        var slackClient = new SlackClient();
        Console.ReadLine();
    }
}

...

public class SlackClient : BaseClient<Config>, IClient
{
    private SlackSocketClient Client { get; set; }

    private ManualResetEventSlim ManualResetEventSlim { get; set; }

    public SlackClient() : base()
    {
        ManualResetEventSlim = new ManualResetEventSlim(false);
        Client = new SlackSocketClient(Config.Token);

        Client.Connect((loginResponse) =>
        {
            // This is called once the client has emitted the RTM start command
            ManualResetEventSlim.Set();
        }, () =>
        {
            // This is called once the RTM client has connected to the end point
        });

        Client.OnMessageReceived += (newMessage) =>
        {
            // Handle each message as you receive them
        };

        ManualResetEventSlim.Wait();
    }

    ...
}

Also, do you have any idea how I would feed something back to console app for output?

Thank you. /Viktor

vikekh avatar Feb 05 '17 13:02 vikekh

Could you check if there is any catched exception in VS output window ?

gpailler avatar Feb 28 '17 02:02 gpailler

Client.OnMessageReceived += (newMessage) =>
{
   // Handle each message as you receive them
};

Is only for a message to the client, this is not all messages. The other handlers for messages are not all implemented and would have to manually implemented.

Depending on what your looking to catch. For instance the below:

client.OnPresenceChangeReceived += async (user) =>
{
   // added to monitor presence changes
};

This confused me at first also. Naming issue.

smorey2 avatar Jun 21 '17 15:06 smorey2