33
loading...
This website collects cookies to deliver better user experience
# Install Tweepy
$ pip install tweepy
# Import Tweepy
import tweepy
# Change with your API keys
API_KEY = ['API_KEY']
API_KEY_SECRET = ['API_KEY_SECRET']
ACCESS_TOKEN = ['ACCESS_TOKEN']
ACCESS_SECRET = ['ACCESS_SECRET']
# Connect with the API
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
# Add your code
fileName = 'lastId.txt'
# Function to save the last id answered on ./lastId.txt
def saveLastId(lastId, fileName):
f_write = open(fileName, 'w')
f_write.write(str(lastId))
f_write.close()
return
# Function to read the last id that is saved on ./lastId.txt
def readLastId(fileName):
f_read = open(fileName, 'r')
ultimo_id_lido = int(f_read.read().strip())
f_read.close()
return ultimo_id_lido
# Function to answer a mention
def tweet():
print('BOT WORKING...')
lastId = readLastId(fileName) # Get the last ID
mentions = api.mentions_timeline(lastId, tweet_mode='extended') # Get all mentions since the last ID
for mention in reversed(mentions): # Reads the mentions in reverse order
if '@youruser'.upper() in mention.full_text.upper(): # Verifies if your bot was mentioned
lastId = mention.id
print(str(mention.id) + ' - ' + mention.full_text) # Prints the mention
saveLastId(lastId, fileName) #Save the last id
print('Answering tweet')
message = 'Hello @{}!'.format(mention.user.screen_name) # Message to be sent
api.update_status(status=message, in_reply_to_status_id=mention) # Reply to the mention
import time
#Loop that runs the bot every 30 seconds
while True:
tweet()
time.sleep(30)
# Import Tweepy
import tweepy
import time
# Change with your API keys
API_KEY =['API_KEY']
API_KEY_SECRET = ['API_KEY_SECRET']
ACCESS_TOKEN = ['ACCESS_TOKEN']
ACCESS_SECRET = ['ACCESS_SECRET']
# Connect with the API
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
fileName = 'lastId.txt'
# Function to save the last id answered on ./lastId.txt
def saveLastId(lastId, fileName):
f_write = open(fileName, 'w')
f_write.write(str(lastId))
f_write.close()
return
# Function to read the last id that is saved on ./lastId.txt
def readLastId(fileName):
f_read = open(fileName, 'r')
ultimo_id_lido = int(f_read.read().strip())
f_read.close()
return ultimo_id_lido
# Function to answer a mention
def tweet():
print('BOT WORKING...')
lastId = readLastId(fileName) # Get the last ID
mentions = api.mentions_timeline(lastId, tweet_mode='extended') # Get all mentions since the last ID
for mention in reversed(mentions): # Reads the mentions in reverse order
if '@youruser'.upper() in mention.full_text.upper(): # Verifies if your bot was mentioned
lastId = mention.id
print(str(mention.id) + ' - ' + mention.full_text) # Prints the mention
saveLastId(lastId, fileName) #Save the last id
print('Answering tweet')
message = 'Hello @{}!'.format(mention.user.screen_name) # Message to be sent
api.update_status(status=message, in_reply_to_status_id=mention) # Reply to the mention
#Loop that runs the bot every 30 seconds
while True:
tweet()
time.sleep(30)