Skip to content

Commit

Permalink
Merge pull request #2 from Adamant-im/dev
Browse files Browse the repository at this point in the history
Add /balances and /test twitterapi commands
  • Loading branch information
adamant-al authored Sep 10, 2020
2 parents 9c0e5b7 + 53ec793 commit 1cb2bcd
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 6 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function init() {
require('./helpers/utils/erc20_utils');
require('./server');
require('./modules/checkTwitterFollow');
require('./modules/apiTester');
require('./modules/checkTwitterRetweet');
require('./modules/checkAdamantContacts');
require('./modules/checkAll');
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
"access_token_secret": ""
},

/** Interval in minutes to test Twitter API. Because of different reasons Twitter may temporary block API requests.
To continue, you need manually login into your Twitter account and solve captcha.
This parameter allows to automatically check if Twitter API works well every twitter_api_test_interval minutes.
In case of error the bot will notify you. Also you can run "/test twitterapi" command manually.
0 means disabled.
**/
"twitter_api_test_interval": 600,

/** ADAMANT accounts to accept control commands from. Control commands from other accounts will not be executed. **/
"admin_accounts": [
"U1123..."
Expand Down
24 changes: 24 additions & 0 deletions modules/apiTester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const config = require('./configReader');
const log = require('../helpers/log');
const notify = require('../helpers/notify');
const twitterapi = require('./twitterapi');

async function testTwitterAPI() {
let testResult = await twitterapi.testApi();
let output;
if (testResult.success) {
output = "Twitter API functions well.";
log.info(output);
} else {
output = `Error while making Twitter API request: ${testResult.message}`;
output += "\n";
output += `Make sure Twitter didn't block your API credentials. If so, you need to manually login into your Twitter account and solve captcha.`;
notify(output, 'error');
}
}

if (config.twitter_api_test_interval) {
setInterval(() => {
testTwitterAPI();
}, config.twitter_api_test_interval * 60 * 1000);
}
57 changes: 54 additions & 3 deletions modules/commandTxs.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const $u = require('../helpers/utils');
const config = require('./configReader');
const log = require('../helpers/log');
const notify = require('../helpers/notify');
const twitterapi = require('./twitterapi');

module.exports = async (cmd, tx, itx) => {

Expand All @@ -26,7 +27,7 @@ module.exports = async (cmd, tx, itx) => {
isNonAdmin: true
}, true);
if (config.notify_non_admins) {
$u.sendAdmMsg(tx.senderId, `I won't execute admin commands as you are not an admin. Connect with my master.`);
$u.sendAdmMsg(tx.senderId, `I won't execute admin commands as you are not an admin. Contact my master.`);
}
return;
}
Expand Down Expand Up @@ -153,6 +154,36 @@ async function calc(arr) {

}

async function test(param) {

param = param[0].trim();
if (!param || !["twitterapi"].includes(param)) {
return {
msgNotify: ``,
msgSendBack: 'Wrong arguments. Command works like this: */test twitterapi*.',
notifyType: 'log'
}
}

let output;

if (param === "twitterapi") {
let testResult = await twitterapi.testApi();
if (testResult.success) {
output = "Twitter API functions well.";
} else {
output = `Error while making Twitter API request: ${testResult.message}`;
}
}

return {
msgNotify: ``,
msgSendBack: `${output}`,
notifyType: 'log'
}

}

function version() {
return {
msgNotify: ``,
Expand All @@ -161,13 +192,33 @@ function version() {
}
}

function balances() {
let output = "";
config.known_crypto.forEach(crypto => {
if (Store.user[crypto].balance && Store.user[crypto].balance !== undefined) {
output += `${$u.thousandSeparator(+Store.user[crypto].balance.toFixed(8), true)} _${crypto}_`;
output += "\n";
}
});

return {
msgNotify: ``,
msgSendBack: `My crypto balances:
${output}`,
notifyType: 'log'
}
}

const commands = {
help,
rates,
calc,
version
version,
balances,
test
}

const admin_commands = [
""
"test",
"balances"
]
4 changes: 4 additions & 0 deletions modules/configReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ const fields = {
type: Object,
default: {}
},
twitter_api_test_interval: {
type: Number,
default: 600
},
slack: {
type: String,
default: null
Expand Down
29 changes: 28 additions & 1 deletion modules/twitterapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async function getAccountTimeline(account) {

async function getAccountInfo(account) {
const accountSN = $u.getTwitterScreenName(account);
console.log(`Getting user info for @${accountSN}..`)
// console.log(`Getting user info for @${accountSN}..`)

return await Twitter.get('users/show', {screen_name: accountSN})
.then(function (data) {
Expand All @@ -128,6 +128,33 @@ async function getAccountInfo(account) {

module.exports = {

async testApi() {

let testResult = {
success: false,
message: ""
}

try {

const testAccount = "@TwitterDev";
let result = await getAccountInfo(testAccount);

if (result && result.id_str === '2244994945') {
testResult.success = true;
} else {
testResult.success = false;
testResult.message = "Request *users/show* for @TwitterDev didn't return expected value.";
}
return testResult;

} catch (e) {
testResult.success = false;
testResult.message = "Exception while making *users/show* request.";
return testResult;
}

},
// Search for predefined toFollowIds — save Twitter API requests
// followAccount should be in "twitter_follow" param in config
async checkIfAccountFollowing(twitterAccount, followAccount) {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "adamant-bountybot",
"version": "0.8.1",
"description": "",
"version": "1.0.0",
"description": "ADAMANT Bounty bot — a software that allows you to carry out bounty campaigns & crypto airdrops, with automatic task verifications and payouts.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit 1cb2bcd

Please sign in to comment.