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)); }
public SetNameDarioBotResponse(TelegramBot telegramApi, IDarioBotRepository repository, TelegramFrom @from, string name) { _telegramApi = telegramApi; _repository = repository; _from = @from; Name = name; }
private IDarioBotReply GetTheCommand(TelegramFrom from, string command, string[] args) { if (command == TelegramBotConstants.SetNameCommand) { if (args.Length < 1) { return(new BadCommandFormatResponse(@from, _telegramApi, $"Expected at least 1 command and 1 argument. Received: {ArgsToString(args)}")); } if (from.Id != _forwardId) { return(new PrivateCommandDarioBotResponse(from, _telegramApi)); } return(new SetNameDarioBotResponse(_telegramApi, _repository, from, args.First())); } else if (command == TelegramBotConstants.StartCommand) { return(new WelcomeDarioBotResponse(_telegramApi, from)); } else if (command == TelegramBotConstants.AmmazzoTuttiCommand) { if (args.Length != 1) { return(new BadCommandFormatResponse(@from, _telegramApi, $"Expected 1 command and 1 argument. Received: {ArgsToString(args)}")); } return(new AmmazzotuttiDarioBotResponse(_repository, _telegramApi, from, _forwardId, args.First())); } return(new UnknownCommand(from, _telegramApi, $"Not a known command: {command}, args: {args}")); }
private IDarioBotReply ReplyFor(TelegramFrom @from, TelegramVoice messageVoice) { if (@from.Id != _forwardId) { return(new PrivateCommandDarioBotResponse(@from, _telegramApi)); } return(new AudioUploadDarioBotReply(_telegramApi, _repository, @from, messageVoice)); }
public AmmazzotuttiDarioBotResponse(IDarioBotRepository repository, TelegramBot telegramApi, TelegramFrom @from, int forwardId, string idToKill) { _repository = repository; _telegramApi = telegramApi; _from = @from; _forwardId = forwardId; _idToKill = int.Parse(idToKill); }
public AudioUploadDarioBotReply( TelegramBot telegramApi, IDarioBotRepository repository, TelegramFrom @from, TelegramVoice messageVoice) { _telegramApi = telegramApi; _repository = repository; _from = @from; _messageVoice = messageVoice; }
private IDarioBotReply HandleCommand(string commandData, TelegramFrom @from) { string[] commandTokens = GetCommandTokens(commandData); string theCommand = commandTokens.First(); if (!IsACommand(theCommand)) { return(new BadCommandFormatResponse(@from, _telegramApi, $"Not a command. A command should start with: {commandData}")); } string[] args = commandTokens.Skip(1).ToArray(); return(GetTheCommand(@from, theCommand, args)); }
public UnknownCommand(TelegramFrom @from, TelegramBot telegramApi, string errorMessage) { _from = @from; _telegramApi = telegramApi; _errorMessage = errorMessage; }
public PrivateCommandDarioBotResponse(TelegramFrom @from, TelegramBot telegramApi) { _from = @from; _telegramApi = telegramApi; }
public BadCommandFormatResponse(TelegramFrom @from, TelegramBot telegramApi, string errorMessage) { _from = @from; _telegramApi = telegramApi; ErrorMessage = errorMessage; }
public WelcomeDarioBotResponse(TelegramBot telegramApi, TelegramFrom @from) { _telegramApi = telegramApi; _from = @from; }
public UnhandledInput(TelegramBot telegramApi, IDarioBotRepository repository, TelegramFrom messageFrom) { _telegramApi = telegramApi; _repository = repository; _messageFrom = messageFrom; }