Skip to content

Commit

Permalink
Merge be540d2 into 9bb6532
Browse files Browse the repository at this point in the history
  • Loading branch information
danbilokha committed May 2, 2020
2 parents 9bb6532 + be540d2 commit f12bd19
Show file tree
Hide file tree
Showing 18 changed files with 373 additions and 134 deletions.
6 changes: 5 additions & 1 deletion server/src/bots/telegram/botResponse/availableResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import * as TelegramBot from 'node-telegram-bot-api';
export const showAvailableCountriesResponse: CallBackQueryHandlerWithCommandArgument = async ({
bot,
chatId,
user,
}: CallBackQueryParameters): Promise<TelegramBot.Message> => {
const countries: Array<Country> = await getAvailableCountries();
return bot.sendMessage(chatId, getShowCountriesMessage(countries));
return bot.sendMessage(
chatId,
getShowCountriesMessage(user.settings?.locale, countries)
);
};
79 changes: 68 additions & 11 deletions server/src/bots/telegram/botResponse/countriesResponse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import {
CountryActiveSituationInfo,
CountrySituationInfo,
WorldOverallInformation,
} from '../../../models/covid19.models';
import {
getCountriesForContinentMessage,
getCountriesTableHTMLMessage,
getCountriesWorldMessage,
getTableCountryRowMessage,
getTableHeader,
} from '../../../messages/feature/countriesMessages';
import { getContinentsInlineKeyboard } from '../services/keyboard';
import {
getContinentCountriesCheckOutOfferMessageInlineKeyboard,
getContinentsInlineKeyboard,
getCountriesInlineKeyboard,
} from '../services/keyboard';
import {
CallBackQueryHandlerWithCommandArgument,
CallBackQueryParameters,
Expand All @@ -17,8 +23,32 @@ import {
getContinentOverallInformation,
getWorldOverallInformation,
} from '../../../services/domain/countries';
import * as TelegramBot from 'node-telegram-bot-api';
import { Continents } from '../../../models/constants';
import { isTextEqual } from '../../../utils/isEqual';

export const countriesByContinentResponse = (continent) => async ({
export const countriesForContinentResponse = async ({
bot,
user,
chatId,
commandParameter,
}: CallBackQueryParameters): Promise<TelegramBot.Message> => {
const { countriesSituation } = await getContinentOverallInformation(
commandParameter
);

const sortedCountriesSituation: Array<CountryActiveSituationInfo> = countriesSituation.sort(
(country1, country2) => country2.active - country1.active
);

return bot.sendMessage(
chatId,
getCountriesForContinentMessage(user.settings?.locale),
getCountriesInlineKeyboard(sortedCountriesSituation)
);
};

export const countriesTableByContinentResponse = (continent: string) => async ({
bot,
user,
chatId,
Expand All @@ -33,13 +63,15 @@ export const countriesByContinentResponse = (continent) => async ({
const portionMessage = [getTableHeader(user.settings?.locale)];
portionMessage.push();

countriesSituation
.sort((country1, country2) => country2.active - country1.active)
.forEach(({ name, active, recovered, deaths }) =>
portionMessage.push(
getTableCountryRowMessage(name, active, recovered, deaths)
)
);
const sortedCountriesSituation: Array<CountryActiveSituationInfo> = countriesSituation.sort(
(country1, country2) => country2.active - country1.active
);

sortedCountriesSituation.forEach(({ name, active, recovered, deaths }) =>
portionMessage.push(
getTableCountryRowMessage(name, active, recovered, deaths)
)
);

return bot.sendMessage(
chatId,
Expand All @@ -52,15 +84,40 @@ export const countriesByContinentResponse = (continent) => async ({
countriesSituation,
portionMessage
),
{ parse_mode: 'HTML' }
{
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: getContinentCountriesCheckOutOfferMessageInlineKeyboard(
user.settings?.locale,
continent
),
},
}
);
};

export const countriesResponse: CallBackQueryHandlerWithCommandArgument = async ({
export const worldByContinentOverallResponse: CallBackQueryHandlerWithCommandArgument = async ({
bot,
user,
chatId,
message,
commandParameter,
}: CallBackQueryParameters) => {
if (
!!commandParameter &&
Object.keys(Continents).some((continent) =>
isTextEqual(continent, commandParameter)
)
) {
return countriesForContinentResponse({
bot,
user,
chatId,
message,
commandParameter,
});
}

const {
confirmed,
recovered,
Expand Down
6 changes: 3 additions & 3 deletions server/src/bots/telegram/botResponse/helpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CallBackQueryParameters,
} from '../models';
import * as TelegramBot from 'node-telegram-bot-api';
import { getFullMenuKeyboard } from '../services/keyboard';
import { getFullClickableFeaturesInlineKeyBoard } from '../services/keyboard';

export const helpInfoResponse: CallBackQueryHandlerWithCommandArgument = async ({
bot,
Expand All @@ -13,7 +13,7 @@ export const helpInfoResponse: CallBackQueryHandlerWithCommandArgument = async (
}: CallBackQueryParameters): Promise<TelegramBot.Message> => {
return bot.sendMessage(
chatId,
getHelpMessage(),
getFullMenuKeyboard(chatId, user.settings?.locale)
getHelpMessage(user.settings?.locale),
getFullClickableFeaturesInlineKeyBoard(user.settings?.locale)
);
};
8 changes: 6 additions & 2 deletions server/src/bots/telegram/botResponse/settingsResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getLocalizationInlineKeyboard } from '../services/keyboard';
import {
getFullMenuKeyboard,
getLocalizationInlineKeyboard,
} from '../services/keyboard';
import {
CallBackQueryHandlerWithCommandArgument,
CallBackQueryParameters,
Expand Down Expand Up @@ -68,6 +71,7 @@ export const settingsLanguageResponse: CallBackQueryHandlerWithCommandArgument =
// We cannot use "User" from parameter in the bot.sendMessage(
// because that "User" still have an old locale, while this
// "resultUser" has updated user settings
languageHasBeenSuccessfullySetup(updatedUser.settings.locale)
languageHasBeenSuccessfullySetup(updatedUser.settings.locale),
getFullMenuKeyboard(chatId, updatedUser.settings?.locale)
);
};
4 changes: 2 additions & 2 deletions server/src/bots/telegram/botResponse/startResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getHelpProposalInlineKeyboard } from '../services/keyboard';
import { getFullMenuKeyboard } from '../services/keyboard';
import { greetUser } from '../../../messages/userMessage';
import {
CallBackQueryHandlerWithCommandArgument,
Expand Down Expand Up @@ -30,6 +30,6 @@ export const startResponse: CallBackQueryHandlerWithCommandArgument = async ({
return bot.sendMessage(
chatId,
greetUser(locale, user),
getHelpProposalInlineKeyboard(locale)
getFullMenuKeyboard(chatId, locale)
);
};
6 changes: 3 additions & 3 deletions server/src/bots/telegram/botResponse/trendResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ export const trendsByCountryResponse: CallBackQueryHandlerWithCommandArgument =

let model = enrichWithTitle(
Transform(periodSituation),
getLocalizedMessages(user.settings.locale, frequencyName) +
getLocalizedMessages(user.settings.locale, 'trends for') +
getLocalizedMessages(user.settings?.locale, frequencyName) +
getLocalizedMessages(user.settings?.locale, 'trends for') +
capitalize(
getLocalizedMessages(user.settings.locale, requestedCountry)
getLocalizedMessages(user.settings?.locale, requestedCountry)
)
);
if (frequency === Frequency.Weekly) {
Expand Down
13 changes: 8 additions & 5 deletions server/src/bots/telegram/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
countriesByContinentResponse,
countriesResponse,
countriesTableByContinentResponse,
worldByContinentOverallResponse,
} from './botResponse/countriesResponse';
import {
showCountryByFlag,
Expand Down Expand Up @@ -81,7 +81,7 @@ export async function runTelegramBot(
],
withSingleParameterAfterCommand(
messageHandlerRegistry,
countriesResponse
worldByContinentOverallResponse
)
)
.registerMessageHandler(
Expand Down Expand Up @@ -184,7 +184,10 @@ export async function runTelegramBot(
)
// Settings
.registerMessageHandler(
[UserSettingsRegExps.Language],
[
...localizeOnLocales(availableLanguages, UserMessages.Language),
UserSettingsRegExps.Language,
],
withSingleParameterAfterCommand(
messageHandlerRegistry,
settingsLanguageResponse
Expand All @@ -200,7 +203,7 @@ export async function runTelegramBot(
for (const continent of Object.keys(Continents)) {
messageHandlerRegistry.registerMessageHandler(
[continent],
countriesByContinentResponse(continent)
countriesTableByContinentResponse(continent)
);
}
getAvailableCountries().then((countries: Array<Country>) => {
Expand Down
1 change: 1 addition & 0 deletions server/src/bots/telegram/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export type CallBackQueryHandlerWithCommandArgument<
> = (parameters: P) => Promise<T>;

export const TELEGRAM_PREFIX: string = 'telegram';
export const COUNTRIES_ROW_ITEMS_NUMBER: number = 4;
export const UNSUBSCRIPTIONS_ROW_ITEMS_NUMBER: number = 3;
109 changes: 106 additions & 3 deletions server/src/bots/telegram/services/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import {
UserSettingsRegExps,
} from '../../../models/constants';
import { InlineKeyboard, ReplyKeyboard } from 'node-telegram-keyboard-wrapper';
import { UNSUBSCRIPTIONS_ROW_ITEMS_NUMBER } from '../models';
import {
COUNTRIES_ROW_ITEMS_NUMBER,
UNSUBSCRIPTIONS_ROW_ITEMS_NUMBER,
} from '../models';
import * as TelegramBot from 'node-telegram-bot-api';
import { getLocalizedMessages } from '../../../services/domain/localization.service';
import { Country } from '../../../models/country.models';
import { flag } from 'country-emoji';

export const getFullMenuKeyboard = (
chatId: number,
Expand All @@ -30,14 +35,14 @@ export const getFullMenuKeyboard = (

rk.addRow(
getLocalizedMessages(locale, UserMessages.CountriesData),
getLocalizedMessages(locale, UserMessages.AvailableCountries)
getLocalizedMessages(locale, UserMessages.SubscriptionManager)
)
.addRow(
getLocalizedMessages(locale, UserMessages.Assistant),
getLocalizedMessages(locale, UserMessages.GetAdviceHowToBehave)
)
.addRow(
getLocalizedMessages(locale, UserMessages.SubscriptionManager),
getLocalizedMessages(locale, UserMessages.Language),
getLocalizedMessages(locale, UserMessages.Help)
);

Expand Down Expand Up @@ -177,6 +182,104 @@ export const getLocalizationInlineKeyboard = (
return ik.build();
};

export const getContinentCountriesCheckOutOfferMessageInlineKeyboard = (
locale: string,
continent: string
): Array<Array<TelegramBot.InlineKeyboardButton>> => {
const inlineKeyboard = [];

inlineKeyboard.push([
{
text: getLocalizedMessages(locale, 'Get all continents'),
callback_data: UserRegExps.CountriesData,
},
{
text: getLocalizedMessages(locale, [
[`Check %s countries out`, continent],
]).join(''),
callback_data: `${UserRegExps.CountriesData} ${continent}`,
},
]);

return inlineKeyboard;
};

export const getCountriesInlineKeyboard = (
countries: Array<Country>
): TelegramBot.SendMessageOptions => {
const ik = new InlineKeyboard();

let i: number = 0;
while (i < countries.length) {
const rows = [];
let rowItem: number = 0;
while (!!countries[i] && rowItem < COUNTRIES_ROW_ITEMS_NUMBER) {
const country: Country = countries[i];
rows.push({
text: `${flag(country.name) ?? ''}${country.iso3}`,
callback_data: `${UserRegExps.CountryData} ${country.name}`,
});
i += 1;
rowItem += 1;
}
ik.addRow(...rows);
}

return ik.build();
};

export const getFullClickableFeaturesInlineKeyBoard = (
locale: string
): TelegramBot.SendMessageOptions => {
const ik = new InlineKeyboard();

ik.addRow(
{
text: getLocalizedMessages(locale, UserMessages.CountriesData),
callback_data: UserRegExps.CountryData,
},
{
text: getLocalizedMessages(locale, UserMessages.AvailableCountries),
callback_data: UserRegExps.AvailableCountries,
}
)
.addRow(
{
text: getLocalizedMessages(locale, UserMessages.Assistant),
callback_data: UserRegExps.Assistant,
},
{
text: getLocalizedMessages(
locale,
UserMessages.GetAdviceHowToBehave
),
callback_data: UserRegExps.Advice,
}
)
.addRow(
{
text: getLocalizedMessages(locale, UserMessages.Existing),
callback_data: UserRegExps.Subscribe,
},
{
text: getLocalizedMessages(locale, UserMessages.Unsubscribe),
callback_data: UserRegExps.Unsubscribe,
}
)
.addRow(
{
text: getLocalizedMessages(locale, 'Choose language'),
callback_data: UserSettingsRegExps.Language,
},
{
text: getLocalizedMessages(locale, UserMessages.Help),
callback_data: UserRegExps.Help,
}
);

return ik.build();
};

const getCloseInlineKeyboardRow = (
locale: string
): TelegramBot.InlineKeyboardButton => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const withTwoArgumentsAfterCommand = (
return handlerFn.call(context, {
bot,
message,
user,
chatId,
commandParameter:
(arg1 && arg1.toLowerCase()) || commandParameter,
Expand Down

0 comments on commit f12bd19

Please sign in to comment.