Example #1
0
        /// <summary>
        /// Accepts an incoming webhook request, creates a turn context,
        /// and runs the middleware pipeline for an incoming TRUSTED activity.
        /// </summary>
        /// <param name="httpRequest">Represents the incoming side of an HTTP request.</param>
        /// <param name="httpResponse">Represents the outgoing side of an HTTP request.</param>
        /// <param name="bot">The code to run at the end of the adapter's middleware pipeline.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A task that represents the work queued to execute.</returns>
        public override async Task ProcessAsync(HttpRequest httpRequest, HttpResponse httpResponse, IBot bot,
                                                CancellationToken cancellationToken = new CancellationToken())
        {
            string stringifiedBody;

            using (var sr = new StreamReader(httpRequest.Body))
            {
                stringifiedBody = await sr.ReadToEndAsync().ConfigureAwait(false);
            }

            if (!_authorizationHelper.VerifySignature(httpRequest.Headers[InfobipConstants.HeaderSignatureKey], stringifiedBody, _infobipViberOptions.InfobipAppSecret))
            {
                if (_infobipViberOptions.BypassAuthentication)
                {
                    _logger.LogWarning("WARNING: Bypassing authentication. Do not run this in production.");
                }
                else
                {
                    httpResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
                    _logger.LogWarning("WARNING: Webhook received message with invalid signature. Request stopped and response will have unauthorized status code!");
                    return;
                }
            }

            var incomingMessage = stringifiedBody.FromInfobipIncomingMessageJson <InfobipViberIncomingResult>();
            var activities      = _toViberActivityConverter.Convert(incomingMessage);

            foreach (var activity in activities)
            {
                using (var context = new TurnContext(this, activity))
                {
                    await RunPipelineAsync(context, bot.OnTurnAsync, cancellationToken).ConfigureAwait(false);
                }
            }
        }
Example #2
0
        public void ConvertViberTextMessageWithCallbackDataToActivity()
        {
            var incomingMessage = new InfobipIncomingMessage <InfobipViberIncomingResult>
            {
                Results = new List <InfobipViberIncomingResult>
                {
                    new InfobipViberIncomingResult
                    {
                        MessageId       = "Unique message Id",
                        From            = "subscriber-number",
                        To              = "viber-sender",
                        ReceivedAt      = DateTimeOffset.UtcNow,
                        IntegrationType = "VIBER",
                        Message         = new InfobipViberIncomingMessage
                        {
                            Text = "Just simple text"
                        },
                        Price = new InfobipIncomingPrice
                        {
                            PricePerMessage = 0,
                            Currency        = "GBP"
                        },
                        CallbackData = "{\"initialMenu\": true, \"userId\": 1, \"username\":\"newUser\"}"
                    }
                },
                MessageCount        = 1,
                PendingMessageCount = 0
            };

            var activity = _toActivityConverter.Convert(incomingMessage).First();

            Assert.NotNull(activity);
            Assert.Equal(InfobipViberConstants.ChannelName, activity.ChannelId);
            var entity = activity.Entities.Single(x => x.Type == InfobipEntityType.CallbackData);
            var result = entity.Properties.ToDictionary();

            Assert.Equal("true", result["initialMenu"]);
            Assert.Equal("1", result["userId"]);
            Assert.Equal("newUser", result["username"]);
        }