Esempio n. 1
0
        private static async Task Run(string token)
        {
            var count = 0;
            var api   = new SlackApiClient(token);

            using (var rtmClient = new SlackRtmClient(token))
            {
                await rtmClient.Connect().ConfigureAwait(false);

                Console.WriteLine("Connected");

                rtmClient.Messages
                .Where(m => m.Text.Contains("ping"))
                .Subscribe(async m =>
                {
                    var user = (await api.Users.Info(m.User).ConfigureAwait(false));
                    Console.WriteLine($"Received ping from @{user.Name}");

                    await api.Chat.PostMessage(new Message
                    {
                        Channel     = m.Channel,
                        Text        = "pong",
                        Attachments = { new Attachment {
                                            Text = $"Count: {++count}"
                                        } }
                    }).ConfigureAwait(false);
                });

                await rtmClient.Events;
            }
        }
Esempio n. 2
0
        static async Task Main(string[] args)
        {
            var oauthAccessMethod = new OauthAccessMethod("Slack app client ID", "Slack app client secret", "temp auth code")
            {
                RedirectUri = "Slack app authorized redirect URL"
            };

            var webClient = await SlackWebClientFactory.CreateWebClient("userId", oauthAccessMethod);

            var connectionResponse = await webClient.CallApiMethod <ConnectResponse>(new RtmConnectMethod());

            var slackClient = new SlackRtmClient();

            if (connectionResponse.Ok)
            {
                await slackClient.Connect(connectionResponse.Url);

                slackClient.BindEvent <ReactionAddedEvent>(ReactionAddedCallback);
                slackClient.BindEvent <ReactionRemovedEvent>(ReactionRemovedCallback);

                Console.WriteLine("User connected");
            }

            Console.Read();
        }
Esempio n. 3
0
        public void Connect_OpensRtmWebSocket(bool manualPresenceSubscription, bool batchPresenceAware)
        {
            var cancellationToken = new CancellationToken(false);
            var rtmUrl            = "rtm url";

            _slackApiClient.Rtm.Connect(manualPresenceSubscription, batchPresenceAware, cancellationToken)
            .Returns(new ConnectResponse {
                Url = rtmUrl
            });
            _webSocketFactory.Create(rtmUrl).Returns(_webSocket);

            var result = _sut.Connect(batchPresenceAware, manualPresenceSubscription, cancellationToken);

            result.IsCompleted.ShouldBe(false);
            _webSocket.Received().Open();

            _opened.OnNext(Unit.Default);

            result.ShouldComplete();
        }
Esempio n. 4
0
        public async Task ProcessActivityBySocketAsync(
            BotCallbackHandler callback = null
            )
        {
            await InitUserIfNeeded();

            var socket = new SlackRtmClient(options.BotUserToken);
            await socket.Connect().ConfigureAwait(false);

            var sub = socket.Messages.Subscribe(async(message) =>
            {
                await ProcessActivityAsync(message, callback);
            });

            await socket.Events;

            sub.Dispose();
        }
Esempio n. 5
0
        private static async Task StartListeningSlackEvents()
        {
            if (string.IsNullOrEmpty(_slackSettings.Token))
            {
                Console.WriteLine("Slack token is missing from the appsettings!");
            }
            else
            {
                using (var rtmClient = new SlackRtmClient(_slackSettings.Token))
                {
                    await rtmClient.Connect().ConfigureAwait(false);

                    Console.WriteLine("Slack connected");
                    // subscribe user change events
                    var subscription = rtmClient.Events.Where(x => x.Type == "user_change").Subscribe(async args => await HandleSlackEvent(args));
                    await WaitForKeyPress().ConfigureAwait(false);
                }
            }
        }