Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hkalbasi/async-bot
a framework for creating conversation bots via async/await
https://github.com/hkalbasi/async-bot
Last synced: about 2 months ago
JSON representation
a framework for creating conversation bots via async/await
- Host: GitHub
- URL: https://github.com/hkalbasi/async-bot
- Owner: HKalbasi
- License: unlicense
- Created: 2020-12-26T20:36:50.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-26T21:02:43.000Z (about 4 years ago)
- Last Synced: 2024-10-28T21:05:50.646Z (3 months ago)
- Language: JavaScript
- Size: 20.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-bot
Get rid of hardcoding states when you are developing a conversation bot.## An example
Imagine you are writing a bot that ask user her information and calculater her bmi:### Classic, bad, long, manual way:
```JS
let state = 'init';
let name, height, weight;
bot.on('message', (message) => {
if (state === 'get_name') {
name = message;
state = 'get_height';
sendMsg('What is your height in meters?');
} else if (state === 'get_height') {
height = Number(message);
state = 'get_weight';
sendMsg('What is your weight in kilograms?');
} else if (state === 'get_weight') {
weight = Number(message);
state = 'init';
sendMsg(`hey ${name}, your bmi is ${weight/(height*height)}`);
} else if (state === 'init) {
state = 'get_name';
sendMsg('Hello. What is your name?');
}
});
```
### in async bot:
```JS
const bot = async (ctx) => {
while (true) {
const name = await ask(ctx, 'Hello. What is your name?');
const height = Number(await ask(ctx, 'What is your height in meters?'));
const weight = Number(await ask(ctx, 'What is your weight in kilograms?'));
await sendMsg(ctx, `hey ${name}, your bmi is ${weight/(height*height)}`);
}
};
```