SlackAPI icon indicating copy to clipboard operation
SlackAPI copied to clipboard

client.Connect() has no effect

Open JosXa opened this issue 6 years ago • 13 comments

I'm trying to run the Creating a SocketClient instance example, but the success callback of the client.Connect method is never reached, and also doesn't time out after letting it run for half an hour. My token seems to be correct as I can post messages with the instantiated client. Is there maybe some slack setting I need to modify in order for the RTM start command to be emitted?

Do you have any hints on how I could resolve this issue?

Specs: Windows 10 .NET Framework 4.5.1 SlackAPI 1.0.5.93

JosXa avatar Sep 13 '18 14:09 JosXa

any luck with this? Facing same issue.

nehal-iitb avatar Sep 19 '18 10:09 nehal-iitb

@nehal-iitb No luck unfortunately. @Inumedia I'd love to provide more information, but am not sure what would be needed for troubleshooting in this case.

JosXa avatar Sep 24 '18 12:09 JosXa

Hello @JosXa

The Connect( method has 2 callbacks. If you set breakpoints or logging, none of the callback is called? In VisualStudio output window, do you have any line about a catched exception?

gpailler avatar Nov 18 '18 07:11 gpailler

Also having the same issue. If you remove the clientReady.Wait() it seems to work. Then re-enable it afterwards. It's a scrappy workaround but it's consistent in it's process...Maybe something like: public string Connect(string token, bool InitialConnection) { try { ManualResetEventSlim clientReady = new ManualResetEventSlim(false); client = new SlackSocketClient(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 += (message) => { // Handle each message as you receive them }; if (!InitialConnection) clientReady.Wait(); return "CONNECTED"; } catch (Exception ex) { return ex.ToString(); } }

bmp02050 avatar Jan 29 '19 20:01 bmp02050

I take that back. I just attempted this...still has a conniption and stalls...

bmp02050 avatar Jan 29 '19 20:01 bmp02050

Give this a whirl: Install Newtonsoft.Json. I was seeing warnings about different versions running and installed it through NuGet. Worked immediately. Might need an updated version in the source?

bmp02050 avatar Jan 30 '19 15:01 bmp02050

Hi @gpailler,

If you set breakpoints or logging, none of the callback is called?

Correct.

In VisualStudio output window, do you have any line about a catched exception?

Nothing visible there.

I updated Newtonsoft.Json to 12.0.1 and SlackAPI to 1.0.7 in my test project to no effect.

Can you confirm that there is no additional configuration to be done in the Slack Application settings on their website for socket clients to work? Also, I'm using an xoxp-... token, is that correct? Could anybody post a working example and some steps to set it up? Is anyone using this project in production? It looks really promising, but I just don't know how to make it work for me :(

Hey @bmp02050, the clientReady flag in the example is set once the client reports to be connected. Obviously you can just remove the .Wait() check, but I presume that you won't be able to receive any incoming updates from slack if you do that. You can verify that by adding an OnMessageReceived handler before the .Wait() call.

JosXa avatar Feb 06 '19 12:02 JosXa

@JosXa I built a wrapper to handle the things I do with the slack bot at the moment. Currently I just send messages and files to a channel and don't need to receive data from Slack. The projects I use this for are just notifications about Order entry, Updates, etc from our ERP system. The source shows: public string Connect(string token) { try { ManualResetEventSlim clientReady = new ManualResetEventSlim(false); client = new SlackSocketClient(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 += (message) => { // Handle each message as you receive them }; clientReady.Wait(); return "CONNECTED"; } catch (Exception ex) { return ex.ToString(); } } for the connect method, which I call and pass the xox token. I built my own methods for sending messages and files to simplify everything. I've tried removing the .Wait() and have it just return, but that fails to send anything because it appears the client doesn't connect.

Having written my own wrapper for this project, I can use: bot.Connect(<Token>); bot.SendMessage(<Channel>, "Starting Imports"); which works just fine. It's hit or miss when setting up a new project for some reason though but so far, every time it gets stuck on the .Wait(), I reinstall Newtonsoft.Json via NuGet and away it goes.

bmp02050 avatar Feb 06 '19 13:02 bmp02050

@JosXa There is no extra configuration needed on Slack side as far as I know. Do you have any proxy configured on your computer or any firewall? If it still doesn't work, the only option is to download the source code, edit SlackAPI.Tests/Configuration/config.default.json (and rename it to config.json) and launch the tests to find the root cause of the issue

gpailler avatar Feb 07 '19 01:02 gpailler

Has anyone figured this out? var client = new SlackClient("[my-xoxp-token]"); client.Connect((connected) => { }, () => { Console.WriteLine("Connected"); client.PostMessage( resp => { Console.WriteLine("Posted"); }, "[mychannelid]", "Hello Slack from scriptcs" ); });

Can't get this to work whether through receiving a payload from slack or testing in Postman. It just doesn't connect, but doesn't give any error information, either. Also tried it as posted at the link in the readme https://github.com/Inumedia/SlackAPI/wiki/Examples.

Newtonsoft.Json (11.0.2) SlackAPI (1.0.8)

Thank you

moshimc avatar May 11 '19 13:05 moshimc

@Maintainers: It appears like this bug seems to be a real show-stopper for first-time users. We'd all like to give you more information and detailed bug reports if you tell us what to look for.

needs response tag can be removed I think.

JosXa avatar May 11 '19 14:05 JosXa

@gpailler Is there a known issue for running slack bot behind a proxy, if yes is there a workaround for it? Thanks in advance!

Prateeksrt avatar Apr 15 '20 10:04 Prateeksrt

If you're running SlackAPI behind a proxy, you should probably use the constructors overloads taking a IWebProxy instance.

gpailler avatar Apr 15 '20 10:04 gpailler