freeCodeCamp / 100DaysOfCode-twitter-bot

Twitter bot for #100DaysOfCode

Home Page:https://twitter.com/_100DaysOfCode

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bot spams encouragement with multiple hashtags

mathewmorris opened this issue Β· comments

Issue

Basically had 24 notifications in less than 30 seconds and they were all from the bot mentioning me.

Possible Solution

Maybe we can teach it to only encourage once per day?



image

I have been getting multiple encouraging tweets, can we get the id of the tweet that triggered the response and then reply to that so there's an audit trail?

If no one else is looking at this I'm thinking about using level db add a user ID, something like.

db.put('twitterId_encourage', 'true')
db.put('twitterId_congratulate', 'true')

Then destroy the db every 24 hrs so no one is given too many inspirational quotes πŸ˜„

https://github.com/Level/level

@spences10 why don't you create a prototype of suggested with leveldb, and then we both can test it?

Hi @amandeepmittal I have and am currently educating myself with LevelDB, something I thought was quite straight forward has consumed quite a bit of time now.

I have db.js for the database

var level = require('level');  
var path = require('path');

var dbPath = process.env.DB_PATH || path.join(__dirname, 'mydb');  
var db = level(dbPath);

module.exports = db;

And I want to be able to query the db, so in app.js

var db = require('./db', {
  valueEncoding: 'json'
})

db.put('name', 'ID001')
db.put('name', 'ID002')
db.put('name', 'ID003')
db.put('name', 'ID004')
db.put('name', 'ID005')
db.put('name', 'ID006')
db.put('name', 'ID007')

db.createReadStream()
  .on('data', function (entry) {
    console.log(entry.value);
  })

Output is

spences10:~/workspace/level-db $ node app.js
ID006
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID005
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID007
spences10:~/workspace/level-db $ node app.js
ID006
spences10:~/workspace/level-db $ node app.js
ID005
spences10:~/workspace/level-db $ node app.js
ID003
spences10:~/workspace/level-db $ node app.js
ID007

That's from mashing the node app.js in the terminal

So, I'm trying to work out how to iterate or just get data from the db

Added PR #30 for this πŸ‘ @amandeepmittal you can test from my branch if you like

https://github.com/spences10/100DaysOfCode-twitter-bot/tree/add-user-blacklist

or if you prefer I can give you access to my c9 environment as a collaborator, mail me: spences10apps@gmail.com

@spences10 Is it good to go? No breaks, I guess?

@amandeepmittal yis g2g 😎

@amandeepmittal maybe I should explain the changes before you merge?

Added logic to the sentiment detection, and add user name as the key and encourage as the value

There's a check to see if the screen_name is already in the db before the .put

      // if sentiment is Negative and the confidence is above 75%
      if (sentim == 'Negative' && confidence >= 75) {
        // get a random quote
        var phrase = sentiment.randomQuote()
        var screen_name = tweet.user.screen_name
        // Check key isn't in db already, key being the screen_name
        db.get(screen_name, function(err, value) {
          if (typeof(value) !== 'undefined') {
            console.log('ALREADY IN DB USER ', screen_name);
          }
          else {
            // Put a user name and that they have been encouraged 
            db.put(screen_name, 'encourage', function(err) {
              if (err) return console.log('Ooops!', err) // some kind of I/O error
              console.log('LOGGED USER ', screen_name)
              // tweet a random encouragement phrase
              tweetNow('@' + screen_name + ' ' + phrase)
            })
          }
        })
      }

I also added in fs-extra so that the database could be rebuilt each day

var refreshDB = function() {
  var fs = require('fs-extra')

  fs.remove('./blacklistUsersDb', function(err) {
    if (err) return console.error(err)

    console.log('success!')
  })
}

refreshDB()
  // retweet every 24 hrs
setInterval(refreshDB, 60000 * 1440)

Maybe I could add something a bit more detailed other than success for the deletion of the database though.

Ok, for some reason I couldn't trigger sentiment on my test bot on the #100DaysOfCode tag but the production bot found them just fine

image

I switched to my @DroidScott twitter account for the testing and got the result I wanted on #someTestHashTag and got the result I was after

image

So this is g2g, no changes needed to the PR #30

Ok, this is still an issue @Ilyes-Hammadi are you able to add anything to this?

The logging for the database functions as expected, see the output here:

image

That has my twitter handle ScottDevTweets added to the database, but the output from the bot is double as per the original issue:

image

So the next time I tweet with negative sentiment I get this output:

image

So the maximum any user is going to get currently is two every 24 hours, this should be one.

Technically it shouldn't post the two tweets as the second tweet should be handled

        // Check key isn't in db already, key being the screen_name
        db.get(screen_name, function(err, value) {

          if (typeof(value) !== 'undefined') {
            console.log('ALREADY IN DB USER ', screen_name);
          }
          else {
            // Put a user name and that they have been encouraged 
            db.put(screen_name, 'encourage', function(err) {
              if (err) return console.log('Ooops!', err) // some kind of I/O error

              console.log('LOGGED USER ', screen_name)

              // tweet a random encouragement phrase
              tweetNow('@' + screen_name + ' ' + phrase)

            })
          }

        })

If the user key is in the database then tweetNow isn't called, @amandeepmittal is that your understanding too?

I'm going to do some more testing on the #someTestHashTag I'd like to hear peoples thoughts πŸ˜„ ❓

Ok, so I set up this bot on a different account and different hashtag and repeated the test so there are three accounts I'm testing with, so first I test with my personal Twitter account @spences10 output here:

image

I get the output we get on the @_100DaysOfCode bot production console, same but there is only one response from the bot account @DroidScott, so I test with my @ScottDevTweets account

I check the twitter account output:

image

So there is one tweet per user that has tweeted negative sentiment more than once:

image

So, it looks like there must be another instance of the @_100DaysOfCode twitter bot running

Solution?

Regenerate the Twitter keys, @amandeepmittal can you do this?

Anyone going to own up to leaving the bot running?

Lol I wish there was an easier way to test twitter bots locally instead of having to use the same auth tokens as production. πŸ˜… This problem is also noted in #22

@robbawebba The purpose of giving access to tokens is that user can test themselves. But I guess, the purpose is no longer fulfilled.

@spences10 you were absolutely right. Someone was running another instance of the bot. This issue can be closed once again!

@robbawebba this was quite puzzling for me so I used my own twitter accounts for testing which worked gloriously well.

@amandeepmittal I think this is the way forward, just have contributors use the code base but use their own keys on either a throw away account or one they will be using for their own development.

Also πŸŽ‰ πŸŽ† 🍾

quite happy with myself at figuring that one out 😎