Skip to content

Commit

Permalink
#8 fixed bug with hanging time and address input
Browse files Browse the repository at this point in the history
  • Loading branch information
sipakov committed Apr 15, 2023
1 parent 1c2da57 commit 0646be0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AlgoTecture.TelegramBot/Controllers/BoatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task PressToEnterTheStartEndTime(BotState botState, RentTimeState r
{
PushL("Enter the rental start time (in HH:mm format, for example, 14:15)");
await Send();
time = await AwaitText();
time = await AwaitText(() => Send("Text input timeout. Use /start to try again"));
}

botState.StartRent = rentTimeState == RentTimeState.StartRent ? DateTimeParser.GetDateTimeUtc(dateTime, time) : botState.StartRent;
Expand Down
99 changes: 59 additions & 40 deletions AlgoTecture.TelegramBot/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,14 @@ private async Task PressAddressToRentButton(string geoAdminFeatureId)

var targetAddress = _telegramToAddressResolver.TryGetAddressListByChatId(chatId.Value).FirstOrDefault(x => x.FeatureId == geoAdminFeatureId);

//var user = await _unitOfWork.Users.GetByTelegramChatId(chatId.Value);
var targetSpace = await _spaceGetter.GetByCoordinates(targetAddress.latitude, targetAddress.longitude);

_telegramToAddressResolver.RemoveAddressListByChatId(chatId.Value);

if (targetSpace == null)
{
await Start();
await Send("No spaces found at this address");
PushL("No spaces found at this address. Use /start to try again");
await SendOrUpdate();
}
else
{
Expand All @@ -163,56 +162,76 @@ private async Task PressAddressToRentButton(string geoAdminFeatureId)
PushL($"Found! {targetSpace.UtilizationType.Name}: {targetSpaceProperty?.Name}. {targetSpaceProperty?.Description}");
}
}
[Action]
private async Task EnterAddress()
{
var chatId = Context.GetSafeChatId();
if (!chatId.HasValue) return;

PushL("Enter the address or part of the address");
await SendOrUpdate();

var address = await AwaitText(() => Send("Text input timeout. Use /start to try again"));

var telegramToAddressList = new List<TelegramToAddressModel>();

var labels = (await _geoAdminSearcher.GetAddress(address)).ToList();

foreach (var label in labels)
{
var telegramToAddressModel = new TelegramToAddressModel
{
FeatureId = label.featureId,
latitude = label.lat,
longitude = label.lon,
Address = label.label
};
telegramToAddressList.Add(telegramToAddressModel);
RowButton(label.label, Q(PressAddressToRentButton, label.featureId));
}

if (!labels.Any())
{
RowButton("Try again");
await Send("Nothing found");
}
else
{
_telegramToAddressResolver.TryAddCurrentAddressList(chatId.Value, telegramToAddressList);

if (_telegramToAddressResolver.TryGetAddressListByChatId(chatId.Value).Count > 1)
{
await Send("Choose the right address");
}
else
{
await Send("Address");
}
}
}

[On(Handle.Exception)]
public async Task Exception(Exception ex)
{
_logger.LogError(ex, "Handle.Exception on telegram-bot");

PushL("Ooops. An error has occurred");
await Start();
await SendOrUpdate();
}

[On(Handle.Unknown)]
public async Task Unknown()
{
var chatId = Context.GetSafeChatId();
if (!chatId.HasValue) return;

PushL(
"I'm sorry, but I'm not yet able to understand natural language requests at the moment. Enter an address to search for the space");
await Send();

var address = await AwaitText();

var telegramToAddressList = new List<TelegramToAddressModel>();


var labels = (await _geoAdminSearcher.GetAddress(address)).ToList();
RowButton("Enter address", Q(EnterAddress));
RowButton("Go back", Q(Start));

foreach (var label in labels)
{
var telegramToAddressModel = new TelegramToAddressModel
{
FeatureId = label.featureId,
latitude = label.lat,
longitude = label.lon,
Address = label.label
};
telegramToAddressList.Add(telegramToAddressModel);
RowButton(label.label, Q(PressAddressToRentButton, label.featureId));
}

if (!labels.Any())
{
RowButton("Try again");
await Send("Nothing found");
}
else
{
_telegramToAddressResolver.TryAddCurrentAddressList(chatId.Value, telegramToAddressList);
await Send("Choose the right address");
}
PushL("I'm sorry, but I'm not yet able to understand natural language requests at the moment. Enter an address to search for the space");
await SendOrUpdate();
}

[On(Handle.ChainTimeout)]
void ChainTimeout(Exception ex)
{
_logger.LogError(ex, "Handle.Exception on telegram-bot");
}
}

0 comments on commit 0646be0

Please sign in to comment.