Using Twitter-bots as a democracy tool

Using Twitter-bots as a democracy tool

By Karlheinz Niebuhr, follow me on twitter, and github

Background information: On Thursday morning 04.06.15 the Paraguayan Senate defeated a mandatory data retention bill that would have compelled local ISPs to retain communications and location details of every user for a period of 12 months.[1] ###Introduction A key tool which was used by Paraguayans to fight the data retention law is a python script named “Pyrawebs”. The name of the Tool which sends automated tweets to the Paraguayan politicians, alludes to the digital version of pyragües, informers who monitored the civilian population’s movements on behalf of dictator Alfredo Stroessner, who ruled between 1954 and 1989. In this post I will explain how this script works and how it was used. Hopefully someone else can give it a meaningful use.

###How it works
The tools which is written in python by Juan and Marce uses the tweepy library to communicate with the twitter API. It can be executed via the command line and takes as input the names of two text files. One of them a list of Twitter users and the other one is the file which contains a list of all the tweets you want to send. IMAGES It iterates over every tweet and sends them to every single user contained in the users text file.

###Evading spam patrol One of the issues we encountered was an error message after a number of consecutive tweets.
“This request looks like it might be automated. To protect our users from spam and other malicious activity, we can’t complete this action right now. Please try again later.”
Apparently the twitter API wasn’t amused.
Fortunately I could solve this by rising the inferior time limit of the random time function. The twitter API gets suspicious if there are several new tweets in a short time period (Twitter engineers if you are reading this, don’t update your code.) And that’s it, now you can deliver meaningful tweets 24/7.

I’ll post the main code below, for more details check it out on Github

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tweepy, time, sys
import random
from random import randint
from time import sleep

#from our keys module (, import the keys dictionary
from my_own_keys import keys


def oauth_login(consumer_key, consumer_secret,access_key,access_secret):
  """Authenticate with twitter using OAuth"""
  auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  auth.set_access_token(access_key, access_secret)
  return tweepy.API(auth)

if __name__ == "__main__":
  print "Authenticated as: %s" %

  l_tweets   = []
  l_targets  = []

  arg_tweets  = str(sys.argv[1])
  arg_target  = str(sys.argv[2])

  f_tweets   = open(arg_tweets,'r')
  f_targets  = open(arg_target,'r')

  s_tweets   = f_tweets.readlines()
  s_targets  = f_targets.readlines()

  # create list of target twitter accounts
  for line in s_targets:

  # create list of tweets
  for line in s_tweets:

  # create and send the tweets
  for tweet in l_tweets:
    actual_target = '@'
    for target in l_targets:
      tweet   = tweet.replace(actual_target, target)
      if target != '':
        actual_target = target
      print tweet

        wait = randint(30,120)
        print 'wait: '+str(wait)+' seconds'
      except Exception as exception:
        print exception
        print "Failed to tweet:", tweet

key file

#!/usr/bin/env python
#visit to create an application and get your keys
keys = dict(
CONSUMER_SECRET = 'L1IKV1DUKKWs4gsdG3x2OUHt6Bby6uJLyhtgX7OUrNigwwb',
# Don't even think of it, those are fake

list of target twitter accounts.. insert as many as you like


list of tweets.. you can insert as many as you like

@ Inconstitucional, desproporcionado, innecesario: ¡Diga no a #pyrawebs!
@ basta ya del espionaje, no a los #pyrawebs
@ usted va a pagar de su propio bolsillo el aumento del costo del internet? #pyrawebs

Check out the code and tutorial on Github


Karl Niebuhr's Picture

About Karl Niebuhr

Karl is a passionate reader, thought leader, author, learning enthusiast, ex knife combat champ, student and general entropy reduction maniac

Saarbrücken Germany