コード例 #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(WebHookType = "genericJson")] HttpRequestMessage req, ILogger log)
        {
            _log = log;
            log.LogInformation("DarioBot was triggered!");

            MonitorReportList.Clear();
            try
            {
                string botToken = DarioBotConfiguration.Get(DarioBotConfiguration.BotTokenKey);
                _logToId        = int.Parse(DarioBotConfiguration.Get(DarioBotConfiguration.LogToIdKey));
                _telegramClient = new TelegramBot(botToken);

                string jsonContent = await req.Content.ReadAsStringAsync();

                log.LogInformation(jsonContent);
                _telegramUpdate = JsonConvert.DeserializeObject <TelegramUpdate>(jsonContent);
                TelegramFrom telegramFrom = GetFrom(_telegramUpdate);
                string       message      = GetMessage(_telegramUpdate);

                LogMessage($"received message from {telegramFrom?.FirstName} {telegramFrom?.LastName}. {message}");

                string toId          = DarioBotConfiguration.Get(DarioBotConfiguration.ForwardToIdKey);
                string resourcesPath = DarioBotConfiguration.Get(DarioBotConfiguration.RemoteResourcesPathKey);
                string azureStorageConnectionString =
                    DarioBotConfiguration.Get(DarioBotConfiguration.StorageConnectionStringKey);
                string azureStorage = DarioBotConfiguration.Get(DarioBotConfiguration.AzureStorageNameKey);
                log.LogInformation($"forwardId: {toId}");

                DarioBot darioBot = new DarioBot(
                    botToken,
                    toId,
                    new AzureFileRepository(azureStorageConnectionString, resourcesPath, azureStorage));

                IDarioBotReply reply = darioBot.ReplyBack(_telegramUpdate);
                LogMessage(
                    $"DarioBot reply to {telegramFrom?.FirstName} {telegramFrom?.LastName}. {message} --> {reply.Type}");

                await reply.SendBackReplay();

                LogMessage($"DarioBot replied with success");
            }
            catch (JsonSerializationException e)
            {
                LogError(e.Message);
                LogError(e.StackTrace);
                return(req.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (Exception e)
            {
                LogError(e.Message);
                LogError(e.StackTrace);
                return(req.CreateResponse(HttpStatusCode.InternalServerError));
            }
            finally
            {
                SendReport();
            }

            return(req.CreateResponse(HttpStatusCode.OK));
        }
コード例 #2
0
        public void DarioBot_AnswersBadFormat_WhenTextIsNull()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage
                {
                    From = new TelegramFrom()
                    {
                        Id = UnknownId
                    }
                }
            });

            Assert.Equal(DarioBotReplyEnum.UnhandledInput, reply.Type);
        }
コード例 #3
0
        public void DarioBot_AnswerBack_AnAudio_IfIt_Has_TheFile()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage
                {
                    From = new TelegramFrom
                    {
                        Id = MyId
                    },
                    Text = "Gesù"
                }
            });

            Assert.Equal(DarioBotReplyEnum.Audio, reply.Type);
        }
コード例 #4
0
        public void DarioBot_AnswersKo_WhenAudio_IsUploaded_ByAnUnknownId()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage
                {
                    From = new TelegramFrom()
                    {
                        Id = UnknownId
                    },
                    Voice = _telegramVoiceExample
                }
            });

            Assert.Equal(DarioBotReplyEnum.Ko, reply.Type);
        }
コード例 #5
0
        public void DarioBot_AnswersKo_IfCallBackCommand_IsUnknown(string command, DarioBotReplyEnum expectedReply)
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                CallbackQuery = new TelegramCallbackQuery()
                {
                    From = new TelegramFrom()
                    {
                        Id = MyId
                    },
                    Data = command
                }
            });

            Assert.Equal(expectedReply, reply.Type);
        }
コード例 #6
0
        public void DarioBot_AnswersWelcome_ToStartCommand()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage()
                {
                    From = new TelegramFrom()
                    {
                        Id = AnyoneId
                    },
                    Text = $"{TelegramBotConstants.StartCommand}"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Welcome, reply.Type);
        }
コード例 #7
0
        public void DarioBot_AnswersKo_AnUnknowId_Asks_ToSet_AName_FromACallback()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                CallbackQuery = new TelegramCallbackQuery()
                {
                    From = new TelegramFrom()
                    {
                        Id = UnknownId
                    },
                    Data = $"{TelegramBotConstants.SetNameCommand} {MyName}"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Ko, reply.Type);
        }
コード例 #8
0
        public void DarioBot_AnswersOk_IfForwardId_Asks_Ammazzotutti_FromACallback()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                CallbackQuery = new TelegramCallbackQuery()
                {
                    From = new TelegramFrom()
                    {
                        Id = MyId
                    },
                    Data = $"{TelegramBotConstants.AmmazzoTuttiCommand} {MyId}"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Ammazzotutti, reply.Type);
        }
コード例 #9
0
        public void DarioBot_AnswersOk_IfForwardId_Asks_ToSet_ANameWithSpaces_FromACallback()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                CallbackQuery = new TelegramCallbackQuery()
                {
                    From = new TelegramFrom()
                    {
                        Id = MyId
                    },
                    Data = $"{TelegramBotConstants.SetNameCommand} {MyName} Piccinelli"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Ok, reply.Type);
            reply.ApplyTo(this);
        }
コード例 #10
0
        public void DarioBot_AnswerBack_ForwardedReply_IfIt_DoesNotHas_TheFile()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage
                {
                    From = new TelegramFrom
                    {
                        Id = MyId
                    },
                    Text = "Baaaaa"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Forwarded, reply.Type);
            reply.ApplyTo(this);
        }
コード例 #11
0
        public void DarioBot_AnswersOk_IfForwardId_Asks_ToSet_AName_FromAMessage()
        {
            IDarioBotReply reply = _darioBot.ReplyBack(new TelegramUpdate
            {
                Message = new TelegramMessage()
                {
                    From = new TelegramFrom()
                    {
                        Id = MyId
                    },
                    Text = $"{TelegramBotConstants.SetNameCommand} {MyName}"
                }
            });


            Assert.Equal(DarioBotReplyEnum.Ok, reply.Type);
            reply.ApplyTo(this);
        }