Skip to content

Commit

Permalink
Merge f2bb05d into 9f99fe0
Browse files Browse the repository at this point in the history
  • Loading branch information
danbilokha committed May 8, 2020
2 parents 9f99fe0 + f2bb05d commit 6ae85a9
Show file tree
Hide file tree
Showing 23 changed files with 192 additions and 128 deletions.
12 changes: 8 additions & 4 deletions server/src/bots/telegram/botResponse/adviceResponse.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { TelegramCallBackQueryHandlerWithCommandArgument } from '../models';
import * as TelegramBot from 'node-telegram-bot-api';
import { getAdviceMessage } from '../../../messages/feature/adviceMessages';
import { getAdviceWithVideoMessage } from '../../../messages/feature/adviceMessages';

export const showAdvicesHowToBehaveResponse: TelegramCallBackQueryHandlerWithCommandArgument = async ({
bot,
user,
chatId,
}): Promise<TelegramBot.Message> => {
return bot.sendMessage(chatId, getAdviceMessage(user.settings.locale), {
parse_mode: 'HTML',
});
return bot.sendMessage(
chatId,
getAdviceWithVideoMessage(user.settings.locale),
{
parse_mode: 'HTML',
}
);
};
18 changes: 1 addition & 17 deletions server/src/bots/telegram/botResponse/subscribeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import {
subscriptionManagerResponseMessage,
subscriptionResultMessage,
} from '../../../messages/feature/subscribeMessages';
import { CustomSubscriptions, UserRegExps } from '../../../models/constants';
import { subscribeOn } from '../../../services/domain/subscriptions';
import { catchAsyncError } from '../../../utils/catchError';
import { getSubscriptionMessageInlineKeyboard } from '../services/keyboard';
import { getUserMessageFromIKorText } from '../utils/getUserMessageFromIKorText';
import { removeCommandFromMessageIfExist } from '../../../utils/removeCommandFromMessageIfExist';
import * as TelegramBot from 'node-telegram-bot-api';
import {
TelegramCallBackQueryHandlerWithCommandArgument,
Expand Down Expand Up @@ -71,20 +68,7 @@ export const subscribingStrategyResponse: TelegramCallBackQueryHandlerWithComman
}

const [err, result] = await catchAsyncError<string>(
subscribeOn(
message.chat,
user,
// TODO: Probably should be replaced with 'commandParameter'
removeCommandFromMessageIfExist(
getUserMessageFromIKorText(
message,
CustomSubscriptions.SubscribeMeOn,
''
),
UserRegExps.Subscribe
),
telegramStorage()
)
subscribeOn(message.chat, user, commandParameter, telegramStorage())
);
if (err) {
return bot.sendMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class TelegramMessageRegistry extends MessageRegistry {
}

public sendUserNotification(
locale: string,
chatId: number,
notification: string
): Promise<TelegramBot.Message> {
Expand Down
4 changes: 2 additions & 2 deletions server/src/bots/viber/botResponses/vAdviceResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ViberTextMessage,
} from '../models';
import { Message } from 'viber-bot';
import { getAdviceMessage } from '../../../messages/feature/adviceMessages';
import { getAdviceWithVideoMessage } from '../../../messages/feature/adviceMessages';
import { vGetFullMenuKeyboard } from '../services/keyboard';
import { mapBackToRealViberChatId } from '../utils/getViberChatId';

Expand All @@ -14,7 +14,7 @@ export const vAdviceResponse: ViberCallBackQueryHandlerWithCommandArgument = asy
user,
}: ViberCallBackQueryParameters): Promise<ViberTextMessage> => {
return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Text(getAdviceMessage(user.settings?.locale)),
new Message.Text(getAdviceWithVideoMessage(user.settings?.locale)),
new Message.Keyboard(
vGetFullMenuKeyboard(user.settings?.locale, chatId)
),
Expand Down
4 changes: 2 additions & 2 deletions server/src/bots/viber/botResponses/vCountriesResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '../../../models/covid19.models';
import {
getCountriesForContinentMessage,
getCountriesTableHTMLMessage,
getCountriesTableMessage,
getCountriesWorldMessage,
getTableCountryRowMessage,
getTableHeader,
Expand Down Expand Up @@ -54,7 +54,7 @@ export const vCountriesTableByContinentResponse = (

return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Text(
getCountriesTableHTMLMessage(
getCountriesTableMessage(
user.settings?.locale,
continent,
confirmed,
Expand Down
2 changes: 1 addition & 1 deletion server/src/bots/viber/botResponses/vSettingsResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const vSettingsLanguageResponse: ViberCallBackQueryHandlerWithCommandArgu
languageHasBeenSuccessfullySetup(updatedUser.settings?.locale)
),
new Message.Keyboard(
vGetFullMenuKeyboard(chatId, updatedUser.settings?.locale)
vGetFullMenuKeyboard(updatedUser.settings?.locale, chatId)
),
]);
};
22 changes: 17 additions & 5 deletions server/src/bots/viber/botResponses/vSubscribeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export const vShowExistingSubscriptionsResponse: ViberCallBackQueryHandlerWithCo
chatId
);
if (!activeUserSubscription?.subscriptionsOn?.length) {
return bot.sendMessage(
{ id: mapBackToRealViberChatId(chatId) },
return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Text(
noSubscriptionsResponseMessage(user?.settings?.locale)
)
);
),
new Message.Keyboard(
vGetSubscriptionMessageInlineKeyboard(user.settings?.locale)
),
]);
}

return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
Expand Down Expand Up @@ -79,7 +81,14 @@ export const vSubscribingStrategyResponse: ViberCallBackQueryHandlerWithCommandA
}

const [err, result] = await catchAsyncError<string>(
subscribeOn(message.chat, user, commandParameter, viberStorage())
subscribeOn(
// Because due to user's ids - they might have shashes
// And Firebase treat slash as the path, so ...
{ ...message.chat, id: chatId },
user,
commandParameter,
viberStorage()
)
);
if (err) {
return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
Expand All @@ -89,6 +98,9 @@ export const vSubscribingStrategyResponse: ViberCallBackQueryHandlerWithCommandA
'Something went wrong, sorry'
)
),
new Message.Keyboard(
vGetSubscriptionMessageInlineKeyboard(user.settings?.locale)
),
]);
}

Expand Down
69 changes: 36 additions & 33 deletions server/src/bots/viber/botResponses/vTrendResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Message } from 'viber-bot';
import { vGetAfterCountryResponseInlineKeyboard } from '../services/keyboard';
import * as shortUrl from 'node-url-shortener';
import { mapBackToRealViberChatId } from '../utils/getViberChatId';
import { trendsErrorMessage } from '../../../messages/feature/trendMessages';

export const vTrendsByCountryResponse: ViberCallBackQueryHandlerWithCommandArgument = async ({
bot,
Expand Down Expand Up @@ -88,13 +89,9 @@ export const vTrendsByCountryResponse: ViberCallBackQueryHandlerWithCommandArgum
);
let model = enrichWithTitle(
Transform(periodSituation, statuses),
[
getLocalizedMessages(user.settings?.locale, frequencyName),
getLocalizedMessages(user.settings?.locale, 'trends for'),
capitalize(
getLocalizedMessages(user.settings?.locale, requestedCountry)
),
].join(' ')
capitalize(
getLocalizedMessages(user.settings?.locale, requestedCountry)
)
);
if (frequency === Frequency.Weekly) {
model = enrichWithType(model, 'barStacked');
Expand All @@ -104,36 +101,41 @@ export const vTrendsByCountryResponse: ViberCallBackQueryHandlerWithCommandArgum
// https://developers.viber.com/docs/all/#picture-message
// Limit is 2048
if (trendsUrl.length > 2048) {
shortUrl.short(trendsUrl, async (err, url) => {
const [error, result] = await catchAsyncError(
bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Picture(url),
new Message.Keyboard(
vGetAfterCountryResponseInlineKeyboard(
requestedCountry,
user.settings?.locale
)
),
])
);
return new Promise((resolve, reject) => {
// trendsUrl should not have spaces, otherwise it service
// will fail and shortUrl will silently fail
shortUrl.short(trendsUrl, async (err, url) => {
const [error, result] = await catchAsyncError(
bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Picture(url),
new Message.Keyboard(
vGetAfterCountryResponseInlineKeyboard(
requestedCountry,
user.settings?.locale
)
),
])
);

if (error) {
bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Keyboard(
vGetAfterCountryResponseInlineKeyboard(
requestedCountry,
user.settings?.locale
)
),
]);
if (error) {
bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Text(
trendsErrorMessage(user.settings?.locale)
),
new Message.Keyboard(
vGetAfterCountryResponseInlineKeyboard(
requestedCountry,
user.settings?.locale
)
),
]);

return;
}
return reject(error);
}

return result;
return resolve(result);
});
});

return;
}
const [error, result] = await catchAsyncError(
bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
Expand All @@ -149,6 +151,7 @@ export const vTrendsByCountryResponse: ViberCallBackQueryHandlerWithCommandArgum

if (error) {
return bot.sendMessage({ id: mapBackToRealViberChatId(chatId) }, [
new Message.Text(trendsErrorMessage(user.settings?.locale)),
new Message.Keyboard(
vGetAfterCountryResponseInlineKeyboard(
requestedCountry,
Expand Down
1 change: 1 addition & 0 deletions server/src/bots/viber/botResponses/vUnsubscribeResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const buildUnsubscribeInlineResponse: ViberCallBackQueryHandlerWithComman
),
new Message.Keyboard(
vGetUnsubscribeMessageInlineKeyboard(
user.settings?.locale,
userSubscription.subscriptionsOn.map((v) => v.value)
)
),
Expand Down
63 changes: 33 additions & 30 deletions server/src/bots/viber/services/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Continents,
CustomSubscriptions,
Emojii,
Frequency,
UserActionsRegExps,
UserInlineActions,
UserMessages,
Expand Down Expand Up @@ -104,7 +105,7 @@ export const vGetAfterCountryResponseInlineKeyboard = (
ActionBody: `${getLocalizedMessages(locale, [
UserRegExps.Subscribe,
])} ${country}`,
Columns: 3,
Columns: 6,
Rows: 1,
Text: `${getLocalizedMessages(locale, [
CustomSubscriptions.SubscribeMeOn,
Expand All @@ -113,31 +114,31 @@ export const vGetAfterCountryResponseInlineKeyboard = (
{
ActionType: 'reply',
ActionBody: `${UserRegExps.Trends} ${country}`,
Columns: 3,
Columns: 2,
Rows: 1,
Text: getLocalizedMessages(locale, 'Weekly chart'),
},
{
ActionType: 'reply',
ActionBody: `${UserRegExps.Trends} \"${country}\" ${Frequency.Monthly}`,
Columns: 2,
Rows: 1,
Text: getLocalizedMessages(locale, 'Monthly chart'),
},
{
ActionType: 'reply',
ActionBody: `${UserRegExps.Trends} \"${country}\" ${Frequency.WholePeriod}`,
Columns: 2,
Rows: 1,
Text: getLocalizedMessages(locale, 'Whole period chart'),
},
{
ActionType: 'reply',
ActionBody: ViberUserMessages.MainMenu,
Columns: 6,
Rows: 1,
Text: getLocalizedMessages(locale, ViberUserMessages.MainMenu),
},
// {
// ActionType: 'reply',
// ActionBody: `${UserRegExps.Trends} \"${country}\" ${Frequency.Monthly}`,
// Columns: 2,
// Rows: 1,
// Text: getLocalizedMessages(locale, 'Monthly chart'),
// },
// {
// ActionType: 'reply',
// ActionBody: `${UserRegExps.Trends} \"${country}\" ${Frequency.WholePeriod}`,
// Columns: 2,
// Rows: 1,
// Text: getLocalizedMessages(locale, 'Whole period chart'),
// },
],
};
};
Expand All @@ -163,18 +164,13 @@ export const vGetSubscriptionMessageInlineKeyboard = (
Rows: 1,
Text: getLocalizedMessages(locale, UserMessages.Unsubscribe),
},
{
ActionType: 'reply',
ActionBody: ViberUserMessages.MainMenu,
Columns: 6,
Rows: 1,
Text: getLocalizedMessages(locale, ViberUserMessages.MainMenu),
},
vGetMainMenuKeyboardRow(locale, 6),
],
};
};

export const vGetUnsubscribeMessageInlineKeyboard = (
locale: string | null,
values: Array<string>
): Keyboard => {
const keyboard: Keyboard = {
Expand Down Expand Up @@ -205,6 +201,8 @@ export const vGetUnsubscribeMessageInlineKeyboard = (
keyboard.Buttons.push(...rows);
}

keyboard.Buttons.push(vGetMainMenuKeyboardRow(locale, 6));

return keyboard;
};

Expand Down Expand Up @@ -276,13 +274,7 @@ export const vGetContinentCountriesCheckOutOfferMessageInlineKeyboard = (
[`Check %s countries out`, continent],
]).join(''),
},
{
ActionType: 'reply',
ActionBody: ViberUserMessages.MainMenu,
Columns: 6,
Rows: 1,
Text: getLocalizedMessages(locale, ViberUserMessages.MainMenu),
},
vGetMainMenuKeyboardRow(locale, 6),
],
};
};
Expand Down Expand Up @@ -404,6 +396,17 @@ export const vGetHelpProposalInlineKeyboard = (locale: string): Keyboard => ({
],
});

const vGetMainMenuKeyboardRow = (
locale: string,
columns: number = 6
): KeyboardButton => ({
ActionType: 'reply',
ActionBody: ViberUserMessages.MainMenu,
Columns: columns,
Rows: 1,
Text: getLocalizedMessages(locale, ViberUserMessages.MainMenu),
});

const getCloseInlineKeyboardRow = (locale: string): KeyboardButton => {
return {
ActionType: 'reply',
Expand Down

0 comments on commit 6ae85a9

Please sign in to comment.