Skip to content

Commit

Permalink
Merge 5fdabfc into c6108c3
Browse files Browse the repository at this point in the history
  • Loading branch information
danbilokha committed Apr 19, 2020
2 parents c6108c3 + 5fdabfc commit ac05c89
Show file tree
Hide file tree
Showing 30 changed files with 824 additions and 621 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ jobs:
with:
node-version: 12
- run: npm ci
- run: npm test
- run: npm run tslint
- run: npm run test
- run: npm run build
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 100,
"printWidth": 80,
"tabWidth": 4,
"trailingComma": "es5",
"singleQuote": true,
Expand Down
4 changes: 2 additions & 2 deletions server/src/bots/telegram/botResponse/adviceResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
socialDistancing,
suggestedBehaviors,
} from '../../../messages/feature/adviceMessages';
import { CallBackQueryHandler } from '../models';
import { CallBackQueryHandlerWithCommandArgument } from '../models';
import * as TelegramBot from 'node-telegram-bot-api';

export const showAdvicesHowToBehaveResponse: CallBackQueryHandler = async (
export const showAdvicesHowToBehaveResponse: CallBackQueryHandlerWithCommandArgument = async (
bot,
message,
chatId
Expand Down
67 changes: 25 additions & 42 deletions server/src/bots/telegram/botResponse/assistantResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,24 @@ import {
getAssistantFeaturesMessage,
noAnswersOnQuestionMessage,
} from '../../../messages/feature/assistantMessages';
import { textAfterUserCommand } from '../../../utils/textAfterCommand';
import {
isCommandOnly,
isMatchingDashboardItem,
isMessageStartsWithCommand,
} from '../../../utils/incomingMessages';
import { KnowledgebaseMeta } from '../../../models/knowledgebase/meta.models';
import { UserMessages } from '../../../models/constants';
import { CallBackQueryHandler } from '../models';
import { CallBackQueryHandlerWithCommandArgument } from '../models';
import * as TelegramBot from 'node-telegram-bot-api';

export const assistantStrategyResponse: CallBackQueryHandler = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId?: number
): Promise<TelegramBot.Message> => {
if (
(isMessageStartsWithCommand(message.text) && isCommandOnly(message.text)) ||
isMatchingDashboardItem(message.text, UserMessages.Assistant)
) {
return showAssistantFeatures(bot, message, chatId) as Promise<TelegramBot.Message>;
}

return answerOnQuestion(bot, message, chatId) as Promise<TelegramBot.Message>;
};

export const showAssistantFeatures: CallBackQueryHandler = async (
export const showAssistantFeatures: CallBackQueryHandlerWithCommandArgument = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId: number
): Promise<TelegramBot.Message> => {
const knowledgebaseMeta: KnowledgebaseMeta = await fetchKnowledgeMetainformation();
return bot.sendMessage(chatId, getAssistantFeaturesMessage(knowledgebaseMeta));
return bot.sendMessage(
chatId,
getAssistantFeaturesMessage(knowledgebaseMeta)
);
};

export const answerOnQuestion: CallBackQueryHandler = async (
export const assistantNoAnswerResponse = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId: number
): Promise<TelegramBot.Message> => {
const question: string = textAfterUserCommand(message.text);
const answers: Array<Answer> = await fetchAnswer(question);

if (!answers.length) {
return assistantNoAnswerResponse(bot, message, chatId) as Promise<TelegramBot.Message>;
}

return assistantResponse(bot, answers, chatId);
};

export const assistantNoAnswerResponse: CallBackQueryHandler = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId: number
): Promise<TelegramBot.Message> => {
return bot.sendMessage(chatId, noAnswersOnQuestionMessage());
Expand All @@ -73,3 +38,21 @@ export const assistantResponse = async (
) => {
return bot.sendMessage(chatId, getAnswersOnQuestionMessage(answers));
};

export const assistantStrategyResponse: CallBackQueryHandlerWithCommandArgument = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId: number,
commandParameter?: string
): Promise<TelegramBot.Message> => {
if (!commandParameter) {
return showAssistantFeatures(bot, message, chatId);
}

const answers: Array<Answer> = await fetchAnswer(commandParameter);
if (!answers.length) {
return assistantNoAnswerResponse(bot, chatId);
}

return assistantResponse(bot, answers, chatId);
};
4 changes: 2 additions & 2 deletions server/src/bots/telegram/botResponse/availableResponse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getAvailableCountries } from '../../../services/domain/covid19';
import { Country } from '../../../models/country.models';
import { getShowCountriesMessage } from '../../../messages/feature/availableMessages';
import { CallBackQueryHandler } from '../models';
import { CallBackQueryHandlerWithCommandArgument } from '../models';
import * as TelegramBot from 'node-telegram-bot-api';

export const showAvailableCountriesResponse: CallBackQueryHandler = async (
export const showAvailableCountriesResponse: CallBackQueryHandlerWithCommandArgument = async (
bot: TelegramBot,
message: TelegramBot.Message,
chatId: number
Expand Down
161 changes: 56 additions & 105 deletions server/src/bots/telegram/botResponse/countriesResponse.ts
Original file line number Diff line number Diff line change
@@ -1,131 +1,82 @@
import {
ContinentCountriesSituation,
CountrySituation,
CountrySituationInfo,
WorldOverallInformation,
} from '../../../models/covid19.models';
import { getCountriesSituation } from '../../../services/domain/covid19';
import {
getTableHeader,
getTableRowMessageForCountry,
} from '../../../messages/feature/countryMessages';
import { Country } from '../../../models/country.models';
import {
getCountriesSumupMessage,
getCountriesTableHTML,
getCountriesWorldMessage,
getTableCountryRowMessage,
getTableHeader,
} from '../../../messages/feature/countriesMessages';
import { getContinentsInlineKeyboard } from '../services/keyboard';
import { CallBackQueryHandler } from '../models';

// TODO: Move this logic to domain and leave here only Telegram bot specific message response
// Sending response itself
export const countriesByContinent = (continent) => async (bot, message, chatId) => {
const countriesSituation: Array<[
Country,
Array<CountrySituationInfo>
]> = await getCountriesSituation();
const continentCountries: ContinentCountriesSituation = {};
import { CallBackQueryHandlerWithCommandArgument } from '../models';
import {
getContinentOverallInformation,
getWorldOverallInformation,
} from '../../../services/domain/countries';

countriesSituation.forEach(([country, situations]: [Country, Array<CountrySituationInfo>]) => {
const { confirmed, recovered, deaths } = situations[situations.length - 1];

const countrySituationResult: CountrySituation = {
lastUpdateDate: situations[situations.length - 1].date,
country,
confirmed,
recovered,
deaths,
};
const prevCountriesResult: Array<CountrySituation> = continentCountries[country.continent]
? continentCountries[country.continent]
: [];
continentCountries[country.continent] = [...prevCountriesResult, countrySituationResult];
});
export const countriesByContinentResponse = (continent) => async (
bot,
message,
chatId
) => {
const {
confirmed,
recovered,
deaths,
countriesSituation,
} = await getContinentOverallInformation(continent);

const portionMessage = [getTableHeader()];
let continentTotalConfirmed: number = 0;
let continentTotalRecovered: number = 0;
let continentTotalDeath: number = 0;

portionMessage.push();
continentCountries[continent]
.sort((country1, country2) => country2.confirmed - country1.confirmed)
.forEach(
({
country: { name },
lastUpdateDate,
confirmed,
recovered,
deaths,
}: CountrySituation) => {
continentTotalConfirmed += confirmed;
continentTotalRecovered += recovered;
continentTotalDeath += deaths;
portionMessage.push(
getTableRowMessageForCountry({
name,
confirmed,
recovered,
deaths,
lastUpdateDate,
})
);
}

countriesSituation
.sort((country1, country2) => country2.active - country1.active)
.forEach(({ name, active, recovered, deaths }) =>
portionMessage.push(
getTableCountryRowMessage(name, active, recovered, deaths)
)
);

await bot.sendMessage(
return bot.sendMessage(
chatId,
getCountriesTableHTML({
getCountriesTableHTML(
continent,
continentTotalConfirmed,
continentTotalRecovered,
continentTotalDeath,
portionMessage,
}),
confirmed,
recovered,
deaths,
countriesSituation,
portionMessage
),
{ parse_mode: 'HTML' }
);
};

// TODO: Move this logic to domain and leave here only Telegram bot specific message response
// Sending response itself
export const countriesResponse: CallBackQueryHandler = async (bot, message, chatId) => {
const countriesSituation: Array<[
Country,
Array<CountrySituationInfo>
]> = await getCountriesSituation();
const continentCountries: ContinentCountriesSituation = {};
let worldTotalConfirmed = 0;
let worldTotalRecovered = 0;
let worldTotalDeaths = 0;

countriesSituation.forEach(([country, situations]: [Country, Array<CountrySituationInfo>]) => {
const { confirmed, recovered, deaths } = situations[situations.length - 1];

worldTotalConfirmed += confirmed;
worldTotalRecovered += recovered;
worldTotalDeaths += deaths;

const countrySituationResult: CountrySituation = {
lastUpdateDate: situations[situations.length - 1].date,
country,
confirmed,
recovered,
deaths,
};
const prevCountriesResult = continentCountries[country.continent]
? continentCountries[country.continent]
: [];
continentCountries[country.continent] = [...prevCountriesResult, countrySituationResult];
});
export const countriesResponse: CallBackQueryHandlerWithCommandArgument = async (
bot,
message,
chatId
) => {
const {
confirmed,
recovered,
deaths,
continentCountriesSituations,
}: WorldOverallInformation = await getWorldOverallInformation();

// Send overall world info,
return bot.sendMessage(
chatId,
getCountriesSumupMessage(
worldTotalConfirmed,
worldTotalRecovered,
worldTotalDeaths,
countriesSituation.length,
Object.keys(continentCountries).length
getCountriesWorldMessage(
confirmed,
recovered,
deaths,
Object.values(continentCountriesSituations).reduce(
(acc: number, val: Array<CountrySituationInfo>): number =>
acc + val.length,
0
) as number,
Object.keys(continentCountriesSituations).length
),
getContinentsInlineKeyboard()
);
Expand Down

0 comments on commit ac05c89

Please sign in to comment.