forked from sollidy/telegram-bot-vercel-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from nikele2001/Branch-Algo
Integrate algorithm into workflow
- Loading branch information
Showing
10 changed files
with
243 additions
and
20 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
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,71 @@ | ||
import createDebug from 'debug'; | ||
|
||
import { Scenes, Markup } from 'telegraf'; | ||
|
||
import { AlgorithmRunner } from '../models/AlgorithmRunner'; | ||
|
||
import { UnknownError, InvalidInputTypeError } from '../exceptions'; | ||
|
||
import { BotContext, updateSessionDataBetweenScenes } from '../BotContext'; | ||
|
||
import { saveProject } from '../db/functions'; | ||
|
||
const debug = createDebug('bot:generate_groupings_command'); | ||
|
||
const getNumGroups = async (ctx: BotContext) => { | ||
updateSessionDataBetweenScenes(ctx); | ||
try { | ||
debug(`Entering getNumGroups scene.`); | ||
updateSessionDataBetweenScenes(ctx); | ||
await ctx.reply( | ||
`How many groups do you want to generate?`, | ||
Markup.removeKeyboard(), | ||
); | ||
return ctx.wizard.next(); | ||
} catch (error) { | ||
const errorMessage = (error as Error).message; | ||
debug(errorMessage); | ||
await ctx.reply(errorMessage); | ||
return ctx.scene.reenter(); | ||
} | ||
}; | ||
|
||
const handleNumGroups = async (ctx: BotContext) => { | ||
debug('User entered number of groups.'); | ||
if (!ctx.message || !ctx.from) { | ||
throw new UnknownError( | ||
'An unknown error occurred. Please try again later.', | ||
); | ||
} | ||
|
||
if (!('text' in ctx.message)) { | ||
throw new InvalidInputTypeError( | ||
'Invalid input type. Please enter a text message.', | ||
); | ||
} | ||
|
||
const numGroups = parseInt(ctx.message.text); | ||
|
||
if (isNaN(numGroups)) { | ||
throw new InvalidInputTypeError( | ||
'Invalid input. Please enter a number.', | ||
); | ||
} | ||
|
||
const logic = new AlgorithmRunner(ctx.scene.session.project, numGroups); | ||
const groupings = logic.prettyPrintGroupings(); | ||
logic.updateInteractionsBasedOnGeneratedGroupings(); | ||
saveProject(ctx.scene.session.project); | ||
const out = `Here are the groupings:\n${groupings}. | ||
Do not delete this message as you will need it to view the groupings again.`; | ||
await ctx.reply(out); | ||
return ctx.scene.enter('mainMenu'); | ||
}; | ||
|
||
const generateGroupingsScene = new Scenes.WizardScene( | ||
'generateGroupings', | ||
getNumGroups, | ||
handleNumGroups, | ||
); | ||
|
||
export { generateGroupingsScene }; |
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
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
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,61 @@ | ||
import createDebug from 'debug'; | ||
|
||
import { Scenes, Markup } from 'telegraf'; | ||
|
||
import { UnknownError, InvalidInputTypeError } from '../exceptions'; | ||
|
||
import { BotContext, updateSessionDataBetweenScenes } from '../BotContext'; | ||
|
||
import { saveProject } from '../db/functions'; | ||
|
||
const debug = createDebug('bot:reset_interactions_command'); | ||
|
||
const resetInteractions = async (ctx: BotContext) => { | ||
updateSessionDataBetweenScenes(ctx); | ||
try { | ||
debug(`Entering resetInteractions scene.`); | ||
updateSessionDataBetweenScenes(ctx); | ||
await ctx.reply( | ||
`Are you sure you want to reset the groupings?`, | ||
Markup.keyboard([['Yes', 'No']]).resize(), | ||
); | ||
return ctx.wizard.next(); | ||
} catch (error) { | ||
const errorMessage = (error as Error).message; | ||
debug(errorMessage); | ||
await ctx.reply(errorMessage); | ||
return ctx.scene.reenter(); | ||
} | ||
}; | ||
|
||
const handleResetInteractions = async (ctx: BotContext) => { | ||
debug('User entered reset interactions option.'); | ||
if (!ctx.message || !ctx.from) { | ||
throw new UnknownError( | ||
'An unknown error occurred. Please try again later.', | ||
); | ||
} | ||
|
||
if (!('text' in ctx.message)) { | ||
throw new InvalidInputTypeError( | ||
'Invalid input type. Please enter a text message.', | ||
); | ||
} | ||
|
||
if (ctx.message?.text === 'Yes') { | ||
ctx.scene.session.project.resetInteractions(); | ||
saveProject(ctx.scene.session.project); | ||
await ctx.reply('Interactions have been reset.'); | ||
} else if (ctx.message?.text === 'No') { | ||
await ctx.reply('Interactions have not been reset.'); | ||
} | ||
return ctx.scene.enter('mainMenu'); | ||
}; | ||
|
||
const resetInteractionsScene = new Scenes.WizardScene( | ||
'resetInteractions', | ||
resetInteractions, | ||
handleResetInteractions, | ||
); | ||
|
||
export { resetInteractionsScene }; |
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
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
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
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
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,7 +1,12 @@ | ||
// import { AlgorithmRunner } from './AlgorithmRunner'; | ||
import { AlgorithmRunner } from './AlgorithmRunner'; | ||
import { AlgorithmTester } from './TestAlgorithmUtil'; | ||
|
||
const project = AlgorithmTester.generateProject(9); | ||
console.log(project.getAdjMatrix()); | ||
// const groupings = AlgorithmRunner.run(project, 3); | ||
// console.log(groupings); | ||
// Random project with 27 nodes and 10 groups and random weights | ||
const project = AlgorithmTester.generateProjectWithRandomWeights(27); | ||
const logic = new AlgorithmRunner(project, 10); // 3 is the number of groups desired | ||
console.log(logic.prettyPrintGroupings()); | ||
|
||
// Project with 27 nodes and 10 groups and 0 weights | ||
const project2 = AlgorithmTester.generateProjectWithNoWeights(27); | ||
const logic2 = new AlgorithmRunner(project2, 10); // 3 is the number of groups desired | ||
console.log(logic2.prettyPrintGroupings()); |