Esempio n. 1
0
        public async Task <IActionResult> Webhook([FromBody] SlackWebhook webhook)
        {
            if (webhook.Type == "url_verification")
            {
                return(Content(webhook.Challenge));
            }

            if (webhook.Type == "event_callback")
            {
                // Ignore retry attempts
                if ((HttpContext.Request.Headers["X-Slack-Retry-Num"].ToString() ?? "").Length > 0)
                {
                    return(Ok());
                }

                if (webhook.Event.Type == "message" && webhook.Event.SubType == null)
                {
                    try
                    {
                        await HandleMessage(webhook);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
            }

            return(Ok());
        }
Esempio n. 2
0
        async Task HandleMessage(SlackWebhook webhook)
        {
            var conversationInfo = await _slackApiClient.Conversations.Info(webhook.Event.Channel);

            var userInfo = await _slackApiClient.Users.Info(webhook.Event.User);

            var text        = webhook.Event.Text;
            var channelName = conversationInfo.Name;
            var userName    = $"@{userInfo.Name}";

            await _slackApiDaemonService.HandleMessage(text, channelName, userName);
        }
 public void SendWebhookMessage(string message, string username)
 {
     try
     {
         if (SlackWebhook.IsNotNullOrEmpty() && message.IsNotNullOrEmpty())
         {
             new Rest(SlackWebhook).PostAsync <string>("", new
             {
                 text     = message,
                 username = username.IsNullOrEmpty() ? $"MLEADER's Trading Bot [CEX.IO]" : username
             }).Wait();
         }
     }
     catch (Exception ex)
     {
         Rest.LogDebug(ex.StackTrace, ex);
     }
 }
Esempio n. 4
0
        public SlackReporter(ReportLevelType reportLevel,
                             String webhookURL,
                             String userName,
                             String channelDebug,
                             String channelInfo,
                             String channelWarn,
                             String channelError,
                             String channelFatal,
                             String iconEmoji          = null,
                             Int32 addUTCHour          = 0,
                             Boolean tryOrderingReport = false)
            : base(ReporterType.Slack, reportLevel)
        {
            if (String.IsNullOrEmpty(webhookURL))
            {
                _slackWebhook = null;
            }
            else
            {
                _channelDebug = channelDebug;
                _channelInfo  = channelInfo;
                _channelWarn  = channelWarn;
                _channelError = channelError;
                _channelFatal = channelFatal;

                _slackWebhook = new SlackWebhook(webhookURL, _channelDebug, userName, iconEmoji, addUTCHour);

                _tryOrderingReport = tryOrderingReport;

                if (_tryOrderingReport)
                {
                    _thread        = new NaiveLoopThread(SendReportInQueue, THREAD_INTERVAL_MS, null, nameof(SlackReporter));
                    _reportActions = new QueueMT <Action>();
                    _thread.Start();
                }
                else
                {
                    _thread        = null;
                    _reportActions = null;
                }
            }
        }
Esempio n. 5
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            [Table("Webhooks")] CloudTable webhooks,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data        = JsonConvert.DeserializeObject(requestBody);

            if (data == null)
            {
                return(new BadRequestObjectResult("Invalid request"));
            }

            var url = data?.url.ToString();
            var uri = new Uri(url);

            var entry = new SlackWebhook()
            {
                PartitionKey = "slack",
                RowKey       = Guid.NewGuid().ToString("D").ToLower(),
                WebhookUrl   = uri.AbsoluteUri
            };

            if (!await PushConfirmationMessage(uri.AbsoluteUri))
            {
                return(new BadRequestObjectResult("Provided webhook url did not respond successfully.  Cannot subscribe."));
            }

            var operation = TableOperation.Insert(entry);
            await webhooks.ExecuteAsync(operation);

            return(entry != null
                ? (ActionResult) new OkObjectResult($"Webhook added successfully")
                : new BadRequestObjectResult("Invalid request"));
        }