-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added locales
- Loading branch information
Showing
22 changed files
with
563 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import meal from './meal' | ||
|
||
export default { | ||
meal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Meal from '../../helpers/meal' | ||
|
||
|
||
export default async (DB, bot, {callback, chats_id, messages_id}) => { | ||
|
||
/* | ||
* Get meal | ||
*/ | ||
const meal = await DB.models.Meals.get({meals_id: callback.meals_id}); | ||
|
||
|
||
switch (callback.value) { | ||
case 'confirm': | ||
|
||
|
||
/* | ||
* Confirm meal only if Payer is defined | ||
*/ | ||
if(meal.Payer) { | ||
|
||
/* | ||
* Update status to confirmed | ||
*/ | ||
await meal.updateAttributes({confirmed: true}); | ||
|
||
|
||
/* | ||
* Get text and keyboard's markup | ||
* Update message | ||
*/ | ||
const text = await Meal.text(meal); | ||
bot.editMessageText(text, { | ||
chat_id: chats_id, | ||
message_id: messages_id, | ||
parse_mode: 'HTML' | ||
}); | ||
} | ||
|
||
break; | ||
|
||
|
||
case 'remove': | ||
|
||
/* | ||
* Remove meal | ||
*/ | ||
if (meal) await meal.destroy(); | ||
|
||
/* | ||
* Delete message | ||
*/ | ||
bot.deleteMessage(chats_id, messages_id); | ||
|
||
break; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import member from './member' | ||
import action from './action' | ||
|
||
export default async (DB, bot, {callback, message}) => { | ||
|
||
const data = {callback, chats_id: message.chat.id, messages_id: message.message_id}; | ||
switch (callback.type) { | ||
|
||
case 'member': | ||
await member(DB, bot, data); | ||
break; | ||
|
||
case 'action': | ||
await action(DB, bot, data); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import Meal from '../../helpers/meal' | ||
|
||
export default async (DB, bot, {callback, chats_id, messages_id}) => { | ||
|
||
/* | ||
* Switch meal's member | ||
*/ | ||
await DB.models.MealsMembers.switch({meals_id: callback.meals_id, members_id: callback.value}); | ||
|
||
|
||
/* | ||
* Update meal's payer | ||
*/ | ||
await DB.models.Meals.payer({meals_id: callback.meals_id}); | ||
|
||
|
||
/* | ||
* Get meal | ||
*/ | ||
const meal = await DB.models.Meals.get({meals_id: callback.meals_id}); | ||
|
||
|
||
/* | ||
* Get text and keyboard's markup | ||
*/ | ||
const text = await Meal.text(meal); | ||
const keyboard = await Meal.keyboard(meal); | ||
|
||
|
||
bot.editMessageText(text, { | ||
chat_id: chats_id, | ||
message_id: messages_id, | ||
parse_mode: 'HTML', | ||
reply_markup: JSON.stringify({ | ||
inline_keyboard: keyboard | ||
}) | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import localize from "../helpers/localize"; | ||
|
||
/** | ||
* | ||
* Clear all previous meals from chat | ||
* | ||
* @param DB | ||
* @param bot | ||
* @param chats_id | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async (DB, bot, {chats_id}) => { | ||
|
||
/* | ||
* Get all meals from chat | ||
*/ | ||
await DB.models.Meals.clear({chats_id}); | ||
|
||
|
||
/* | ||
* Send message about deletion | ||
*/ | ||
bot.sendMessage(chats_id, localize('clear')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Clear from './clear' | ||
import Meal from './meal' | ||
|
||
export default { | ||
Clear, | ||
Meal | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Meal from './../helpers/meal' | ||
|
||
|
||
/** | ||
* | ||
* Create meal instance | ||
* Get group members as eaters | ||
* Calculate today's payer | ||
* | ||
* @param DB | ||
* @param bot | ||
* @param chats_id | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async (DB, bot, {chats_id}) => { | ||
|
||
|
||
/* | ||
* Create new meal instance in DB | ||
* Re-query created meal because sequelize can't load relations after creating | ||
*/ | ||
const meal = await DB.models.Meals.get({meals_id: (await DB.models.Meals.create({chats_id})).id}); | ||
|
||
|
||
/* | ||
* Create text and keyboard markup | ||
*/ | ||
const message = await Meal.text(meal); | ||
const keyboard = await Meal.keyboard(meal); | ||
|
||
|
||
/* | ||
* Send message to chat with new meal | ||
*/ | ||
bot.sendMessage(chats_id, message, { | ||
parse_mode: 'HTML', | ||
reply_markup: JSON.stringify({ | ||
inline_keyboard: keyboard | ||
}) | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"hook": "WEB_HOOK", | ||
"token": "BOT_TOKEN" | ||
"token": "BOT_TOKEN", | ||
"local": "LOCALE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
"port": 80, | ||
"hook": null, | ||
"token": null, | ||
"db": "foodometer" | ||
"db": "foodometer", | ||
"locale": "en" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import callbacks from './../callbacks' | ||
import get from 'lodash/get' | ||
|
||
|
||
const callers = { | ||
1: 'meal' | ||
}; | ||
|
||
const types = { | ||
1: 'member', | ||
2: 'action' | ||
}; | ||
|
||
const values = { | ||
action: { | ||
1: 'confirm', | ||
2: 'remove' | ||
} | ||
}; | ||
|
||
|
||
export default async (DB, bot, query) => { | ||
|
||
const data = query.data.split(':'); | ||
|
||
/* | ||
* Decode callback | ||
*/ | ||
const caller = callers[data[0]] || null; | ||
const type = types[data[1]] || null; | ||
const value = get(values, [type, data[2]], data[2] || null); | ||
const meals_id = data[3] || null; | ||
const callback = {caller, type, value, meals_id}; | ||
|
||
|
||
const message = query.message; | ||
|
||
|
||
/* | ||
* Process callback | ||
* Get caller and process caller's callback | ||
*/ | ||
switch (callback.caller) { | ||
case 'meal': await callbacks.meal(DB, bot, {callback, message}) | ||
} | ||
|
||
|
||
/* | ||
* Answer to callback | ||
*/ | ||
return bot.answerCallbackQuery({callback_query_id: query.id}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import localize from './../helpers/localize' | ||
|
||
/** | ||
* | ||
* Add creator to chat member | ||
* Welcome new member | ||
* | ||
* @param DB | ||
* @param bot | ||
* @param chats_id | ||
* @param creator | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async (DB, bot, {chats_id, creator}) => { | ||
|
||
|
||
/* | ||
* Collect creator's name and username | ||
*/ | ||
const name = creator.first_name || null + creator.last_name || null; | ||
const username = creator.username || null; | ||
|
||
|
||
/* | ||
* Add new member to db | ||
*/ | ||
const member = await DB.models.Members.add({chats_id, users_id: creator.id, name, username}); | ||
|
||
|
||
/* | ||
* Send message with welcome text | ||
*/ | ||
bot.sendMessage(chats_id, localize('creation', {name: member.name}), {parse_mode: 'HTML'}); | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import group_chat_created from './group_chat_created' | ||
import new_chat_members from './new_chat_members' | ||
import left_chat_member from './left_chat_member' | ||
import callback_query from './callback_query' | ||
|
||
|
||
export default { | ||
group_chat_created, | ||
new_chat_members, | ||
left_chat_member, | ||
callback_query | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import localize from "../helpers/localize"; | ||
|
||
export default async (DB, bot, {chats_id, left_member}) => { | ||
|
||
/* | ||
* Find member in chat | ||
*/ | ||
const member = await DB.models.Members.get({chats_id, users_id: left_member.id}); | ||
|
||
|
||
/* | ||
* If member found - remove him from chat | ||
*/ | ||
if (member) { | ||
|
||
await member.destroy(); | ||
bot.sendMessage(chats_id, localize('left', {name: member.name}), {parse_mode: 'HTML'}); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import localize from "../helpers/localize"; | ||
|
||
/** | ||
* | ||
* Add invited user to chat member | ||
* Welcome new member | ||
* | ||
* @param DB | ||
* @param bot | ||
* @param chats_id | ||
* @param members | ||
* @returns {Promise<void>} | ||
*/ | ||
export default async (DB, bot, {chats_id, members}) => { | ||
|
||
/* | ||
* Add each member | ||
*/ | ||
members.forEach(async m => { | ||
|
||
|
||
/* | ||
* Collect member's name and username | ||
*/ | ||
const name = m.first_name || null + m.last_name || null; | ||
const username = m.username || null; | ||
|
||
/* | ||
* Try to find member in DB | ||
* IF found - restore | ||
* IF not - add new | ||
*/ | ||
let member = await DB.models.Members.get({chats_id, users_id: m.id, paranoid: false}); | ||
if (member) await member.restore(); | ||
else member = await DB.models.Members.add({chats_id, users_id: m.id, name, username}); | ||
|
||
|
||
/* | ||
* Send message with welcome text | ||
*/ | ||
bot.sendMessage(chats_id, localize('welcome', {name: member.name}), {parse_mode: 'HTML'}); | ||
|
||
}); | ||
|
||
} |
Oops, something went wrong.