Skip to content

Commit

Permalink
Add new commands, Support steemradar.js
Browse files Browse the repository at this point in the history
- New command for account posts
- New command for top trending posts
- Scene wizard for trending
- Action handler for account posts
- Steemradar.js for notifications.
  • Loading branch information
gigatoride committed Jul 19, 2019
1 parent 87319d4 commit 525e91e
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 90 deletions.
1 change: 0 additions & 1 deletion .env.example
@@ -1,4 +1,3 @@
NODE_ENV=development
BOT_TOKEN=your_api_token
BOT_TOKEN_DEV=your_development_api_token
MONGODB_URI=your_mongo_db_uri
19 changes: 12 additions & 7 deletions README.md
Expand Up @@ -6,19 +6,22 @@ A personal assistant for Steem blockchain.

## Features

- Steem Account Profile
- Steem Wallet
- Steem Account Profile Viewer
- Steem Account Wallet Viewer
- Steem Account Posts
- Crypto Prices
- Notifications Manager
- Replies
- Mentions
- Transfers
- Replies
- Mentions
- Transfers
- Trending Posts

More features is under development.

## Usage

![](https://media.giphy.com/media/iFUh6qdT0VfOy5U8Sq/giphy.gif)
![](./assets/gif/usage.gif)

1. Start chatting with the bot https://telegram.me/SteevieBot
2. Hit /start
3. Setup your Steem Account /setup
Expand Down Expand Up @@ -61,9 +64,11 @@ then send the following commands in [commands.txt](https://github.com/gigatoride
### Running

To start the bot please use the following command:

```
npm start
```

## Licence
MIT

MIT
Binary file added assets/gif/usage.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion commands.txt
@@ -1,5 +1,7 @@
myaccount - view your profile
myaccount - view your account profile
myposts - get your account posts
setaccount - setup your account in Steem blockchain
notifications - manage your account notifications
trending - get top trending posts
cryptoprices - get current crypto prices in USD
help - get some help!
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -25,18 +25,19 @@
"homepage": "https://github.com/gigatoride/steevie#readme",
"dependencies": {
"dotenv": "^8.0.0",
"mongoose": "^5.5.11",
"mongoose": "^5.6.5",
"node-coinmarketcap": "^0.2.0",
"steem": "^0.7.5",
"steemradar": "^2.0.6",
"telegraf": "^3.29.0",
"telegraf-i18n": "^6.5.0"
},
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.1.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0"
}
}
15 changes: 9 additions & 6 deletions src/bot.js
Expand Up @@ -2,11 +2,12 @@ const Telegraf = require('telegraf');
const TelegrafI18n = require('telegraf-i18n');
const path = require('path');
const handlers = require('./handlers');
const notifications = require('./notifications');

const Stage = require('telegraf/stage');
const session = require('telegraf/session');

const { setAccount } = require('./scenes');
const { setAccount, trending } = require('./scenes');

const bot = new Telegraf(process.env.BOT_TOKEN);

Expand All @@ -15,6 +16,7 @@ const stage = new Stage();

/** Register All scenes */
stage.register(setAccount);
stage.register(trending);

/** Translation manager */
const i18n = new TelegrafI18n({
Expand All @@ -23,20 +25,21 @@ const i18n = new TelegrafI18n({
directory: path.resolve(__dirname, 'locales')
});

/** Notifications operator */
notifications(bot, i18n);

/** Middlewares */
bot.use(i18n.middleware());
bot.use(session());
stage.use(handlers.actions);
stage.use(handlers.commands);

/** Operator for real-time notifications */
require('./operator').notificationsSubscribes(bot, i18n);

bot.use((ctx, next) => {
const start = new Date();
return next().then(() => {
const ms = new Date() - start;
if (process.env.NODE_ENV === 'development') console.log('%s response time %s', ctx.from.id, ms);
if (process.env.NODE_ENV === 'development') {
console.log('%s response time %s', ctx.from.id, ms);
}
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/handlers/actions/account.js
@@ -1,5 +1,5 @@
const Extra = require('telegraf/extra');
const mainAccount = require('./../commands/myaccount');
const myAccount = require('./../commands/myaccount');
const controller = require('../../controllers/account');
const steem = require('steem');

Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = async ctx => {
});
break;
case 'back':
mainAccount(ctx, { edit: true });
myAccount(ctx, { edit: true });
break;
}
};
9 changes: 7 additions & 2 deletions src/handlers/actions/index.js
Expand Up @@ -3,10 +3,15 @@ const { Composer } = require('telegraf');
const composer = new Composer();

const account = require('../actions/account');
const posts = require('../actions/posts');
const notification = require('../actions/notification');

/** Account handler */
/** Actions handler */
composer.action(/account:\w+/, account);
composer.action(/notification:(transfers|mentions|replies):(disable|enable)/, notification);
composer.action(/posts:\d+:(next|previous)/, posts);
composer.action(
/notification:(transfers|mentions|replies):(disable|enable)/,
notification
);

module.exports = composer;
21 changes: 21 additions & 0 deletions src/handlers/actions/posts.js
@@ -0,0 +1,21 @@
const myPosts = require('./../commands/myposts');

/**
* Action handler for account posts next/previous
*/
module.exports = ctx => {
const query = ctx.match[0].split(':');
const type = query[2].match(/(previous|next)/)[0];
let currentPage = parseInt(query[1]);

switch (type) {
case 'next':
currentPage++;
myPosts(ctx, { edit: true, currentPage });
break;
case 'previous':
currentPage--;
myPosts(ctx, { edit: true, currentPage });
break;
}
};
2 changes: 2 additions & 0 deletions src/handlers/commands/index.js
Expand Up @@ -4,6 +4,8 @@ const composer = new Composer();

composer.command('setaccount', require('./setaccount'));
composer.command('myaccount', require('./myaccount'));
composer.command('myposts', require('./myposts'));
composer.command('trending', require('./trending'));
composer.command('notifications', require('./notifications'));
composer.command('cryptoprices', require('./cryptoprices'));
composer.command('start', require('./start'));
Expand Down
87 changes: 87 additions & 0 deletions src/handlers/commands/myposts.js
@@ -0,0 +1,87 @@
const Extra = require('telegraf/extra');
const Markup = require('telegraf/markup');
const { paginate } = require('../../utils');
const steem = require('steem');
const controller = require('../../controllers/account');

/**
* Replies with Steem account posts
*/
module.exports = async (ctx, opts = {}) => {
const chatId = ctx.from.id;
const account = await controller.getAccount(chatId);
if (account && account.steemAccount) {
const accountName = account.steemAccount;
const posts = await steem.api.getDiscussionsByAuthorBeforeDateAsync(
accountName,
null,
null,
10 // limit
);
if (posts.length) {
const buttons = posts.map(post => {
return Markup.urlButton(post.title, 'https://steemit.com' + post.url);
});

const currentPage = opts.currentPage || 1; // default for front-end
const pageSize = 6; // posts per page
const totalPages = Math.ceil(buttons.length / pageSize); // rounds the page number up to the next largest whole number or integer
let currentPagePosts = paginate(buttons, pageSize, currentPage);

const paginating = () => {
const next = Markup.callbackButton(
ctx.i18n.t('next'),
`posts:${currentPage}:next`
);
const previous = Markup.callbackButton(
ctx.i18n.t('previous'),
`posts:${currentPage}:previous`
);
if (currentPage === totalPages) {
return [previous];
} else if (currentPage === 1) {
return [next];
} else {
return [previous, next];
}
};

const keyboard = Markup.inlineKeyboard(
currentPagePosts.concat(paginating()),
{
columns: 1
}
)
.resize()
.oneTime();
if (opts.edit) {
ctx.editMessageText(
ctx.i18n.t('my-posts', {
accountName: accountName,
totalPages: totalPages,
currentPage: currentPage
}),
Extra.markup(keyboard)
);
} else {
ctx.reply(
ctx.i18n.t('my-posts', {
accountName: accountName,
totalPages: totalPages,
currentPage: currentPage
}),
Extra.markup(keyboard)
);
}
} else {
ctx.reply(
ctx.i18n.t('no-posts', {
accountName: accountName
})
);
}
ctx.scene.leave();
} else {
ctx.reply(ctx.i18n.t('please-setup-account'));
}
};
2 changes: 1 addition & 1 deletion src/handlers/commands/notifications.js
Expand Up @@ -6,7 +6,7 @@ module.exports = async (ctx, opts = {}) => {

const chatId = from.id;
const account = await controller.getAccount(chatId);
if (account.steemAccount) {
if (account && account.steemAccount) {
return controller.getNotifications(chatId).then(doc => {
const { notifications } = doc;

Expand Down
3 changes: 3 additions & 0 deletions src/handlers/commands/trending.js
@@ -0,0 +1,3 @@
const Stage = require('telegraf/stage');

module.exports = Stage.enter('trending-wizard');
13 changes: 11 additions & 2 deletions src/locales/en.yaml
@@ -1,13 +1,18 @@
all-tags: All tags
account-already-registered: There is an account already registered. 🧐
account-deleted-success: Your account has been deleted successfully βœ…
account-register-failure: Unable to register a new account πŸ˜₯
account-register-retry: Please enter an exist Steem account ⚠️
account-register-success: Your account has been registered successfully βœ…
back: Β« Back
next: Next Β»
previous: Β« Previous
commands: |
/myaccount - view your profile
/myaccount - view your account profile
/myposts - get your account posts
/setaccount - setup your account in Steem blockchain
/notifications - manage your account notifications
/trending - get top trending posts
/cryptoprices - get current crypto prices in USD
/help - get some help!
current-crypto-prices: |
Expand All @@ -17,8 +22,9 @@ current-crypto-prices: |
Litecoin: ${prices.ltc}πŸ’°
Steem: ${prices.steem}πŸ’°
delete-account: ❌ Delete Account
help-message: I can assist you with Steem blockchain\nYou can control me by sending these commands:\n${commands}
help-message: I can assist you with Steem blockchain\n\nYou can control me by sending these commands:\n\n${commands}
my-wallet: πŸ’Έ My Wallet
my-posts: ${accountName} posts. page ${currentPage} of ${totalPages}
my-wallet-details: |
*${name}* wallet πŸ’°\n
*Available*
Expand All @@ -34,6 +40,7 @@ my-wallet-details: |
new-notification-mention: ${author} has mentioned you πŸ—£
new-notification-reply: You have got a new reply from ${author} \nπŸ’¬ ${reply}
new-notification-transfer: You have received a transfer from ${from}\n${amount}\n${memo}
no-posts: Looks like ${accountName} hasn't started blogging yet!
notification-mentions-disable: πŸ—£ Disable mentions
notification-mentions-enable: πŸ—£ Enable mentions
notification-replies-disable: β†ͺ️ Disable replies
Expand All @@ -42,6 +49,8 @@ notification-transfers-disable: πŸ’Έ Disable transfers
notification-transfers-enable: πŸ’Έ Enable transfers
notifications-main: Here you can enable or disable the notifications πŸ””
please-setup-account: Please setup an account first. please use /setaccount command.
please-enter-tag-trending: Please enter a tag for trending posts
please-enter-valid-tag: Please a valid tag.
question-account-name: Alright, What is your *Steem* account name?
sticker-response: πŸ™„
unknown-message: Sorry, I didn't understand that. please use /help command for more information.
Expand Down

0 comments on commit 525e91e

Please sign in to comment.