예제 #1
0
        public async Task <IActionResult> SendPushBulletFromAppVeyor(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)]
            HttpRequest req)
        {
            try
            {
                var pushBulletApiKey        = Environment.GetEnvironmentVariable("PUSHBULLET_API_KEY", EnvironmentVariableTarget.Process);
                var pushBulletEncryptionKey = Environment.GetEnvironmentVariable("PUSHBULLET_ENCRYPTION_KEY", EnvironmentVariableTarget.Process);

                if (pushBulletApiKey.IsNullOrEmpty())
                {
                    return(new BadRequestErrorMessageResult("PushBelletApiKey cannot be found"));
                }

                if (pushBulletEncryptionKey.IsNullOrEmpty())
                {
                    return(new BadRequestErrorMessageResult("PushBulletEncryptionKey cannot be found"));
                }

                var requestBody = await req.GetRawBodyStringAsync();

                var appVeyor = JsonConvert.DeserializeObject <AppVeyor>(requestBody);

                Log.Logger.Information("AppVeyor Request Built: {@AppVeyor}", appVeyor);
                //_logger.LogInformation("AppVeyor Request Built: {@AppVeyor}", appVeyor);

                var client = new PushBulletClient(pushBulletApiKey, pushBulletEncryptionKey, TimeZoneInfo.Local);

                var channel = req.Headers["Channel"].ToString();
                var title   = $"{appVeyor.EventData.ProjectName} Release";
                var body    = $"There's a new version of {appVeyor.EventData.ProjectName}! Update: {appVeyor.EventData.CommitMessage}";
                var url     = $"https://www.nuget.org/packages/{appVeyor.EventData.ProjectName}/{appVeyor.EventData.BuildVersion}";

                if (channel.IsNullOrEmpty())
                {
                    return(new BadRequestErrorMessageResult($"Unknown channel from project {appVeyor.EventData.ProjectName}"));
                }

                var pushLinkRequest = new PushLinkRequest
                {
                    ChannelTag = channel,
                    Title      = title,
                    Url        = url,
                    Body       = body
                };

                var pushLinkResponse = await client.PushLink(pushLinkRequest);

                _logger.LogInformation("PushBullet Sent Link Message. {@PushLinkRequest} {@PushLinkResponse}", pushLinkRequest, pushLinkResponse);

                return(new OkObjectResult(pushLinkResponse));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error sending PushBullet message");
                return(new BadRequestErrorMessageResult(ex.Message));
            }
        }
예제 #2
0
        private async Task <List <PushResponse> > SendToDevices(PushBulletClient client, PushBullet pushBulletModel, string userId)
        {
            var devices = await client.CurrentUsersDevices();

            var deviceToSend = devices.Devices.Where(x => pushBulletModel.DeviceNickNames.Contains(x.Nickname)).ToList();

            if (!deviceToSend.Any())
            {
                throw new NullReferenceException($"No device nicknames matched {string.Join(", ", pushBulletModel.DeviceNickNames)}");
            }

            var responses = new List <PushResponse>();

            deviceToSend.ForEach(async device =>
            {
                if (string.IsNullOrEmpty(pushBulletModel.Url))
                {
                    var pushNoteRequest = new PushNoteRequest
                    {
                        DeviceIden = device.Iden,
                        Title      = pushBulletModel.Title,
                        Body       = pushBulletModel.Body
                    };

                    var pushNoteResponse = await client.PushNote(pushNoteRequest);

                    _logger.LogInformation("PushBullet Sent Message by {UserId}.", userId);

                    responses.Add(pushNoteResponse);
                }
                else
                {
                    var pushLinkRequest = new PushLinkRequest
                    {
                        DeviceIden = device.Iden,
                        Title      = pushBulletModel.Title,
                        Url        = pushBulletModel.Url,
                        Body       = pushBulletModel.Body
                    };

                    var pushLinkResponse = await client.PushLink(pushLinkRequest);

                    _logger.LogInformation("PushBullet Sent Link Message by {UserId}.", userId);

                    responses.Add(pushLinkResponse);
                }
            });

            return(responses);
        }
예제 #3
0
        private async Task <List <PushResponse> > SendToChannel(PushBulletClient client, PushBullet pushBulletModel, string userId)
        {
            var responses = new List <PushResponse>();

            if (string.IsNullOrEmpty(pushBulletModel.Url))
            {
                var pushNoteRequest = new PushNoteRequest
                {
                    ChannelTag = pushBulletModel.Channel,
                    Title      = pushBulletModel.Title,
                    Body       = pushBulletModel.Body
                };

                var pushNoteResponse = await client.PushNote(pushNoteRequest);

                _logger.LogInformation("PushBullet Sent Message by {UserId}.", userId);

                responses.Add(pushNoteResponse);
            }
            else
            {
                var pushLinkRequest = new PushLinkRequest
                {
                    ChannelTag = pushBulletModel.Channel,
                    Title      = pushBulletModel.Title,
                    Url        = pushBulletModel.Url,
                    Body       = pushBulletModel.Body
                };

                var pushLinkResponse = await client.PushLink(pushLinkRequest);

                _logger.LogInformation("PushBullet Sent Link Message by {UserId}.", userId);

                responses.Add(pushLinkResponse);
            }

            return(responses);
        }