Inumedia / SlackAPI

.NET Implementation of the Slack team communication platform API.
MIT License
451 stars 243 forks source link

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

Open vikekh opened 7 years ago

vikekh commented 7 years ago

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

gpailler commented 7 years ago

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

smorey2 commented 7 years ago
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.