Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Okay, so I have this bot I am making for twitter. The goal of the bot is to search for tweets starting with "I'm" and then I want it to copy everything that comes after "I'm." Then respond with "Hi ____________, I'm Dad!" But currently, I am having a key error for my variable in the tweet. I have no idea how to fix this Key Error, at all. Then, I don't know how to make it actually say whatever comes after "I'm" as it automatically just says "Hi I'm Dad" and then the rest of the tweet, but I have yet to test the newest version because of this Key Error.

Here's my code
Python
import tweepy
import tweepy as tt
import tim
import sys
import importlib
from importlib import reload
from collections import defaultdict

importlib.reload(sys)

# login credentials twitter account
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''

# login
auth = tt.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tt.API(auth)
search_query = "I'm"
user = api.me()
print(user.name)

max_tweets = 100

for tweet in tweepy.Cursor(api.search, q=search_query, tweet_mode='extended').items(max_tweets):
    t = tweet.full_text
    answer = "@{name} Hi {t}, I'm dad!".format(name=tweet.user.screen_name)
api.update_status(in_reply_to_status_id=answer)

print("Reply:", answer)

time.sleep(0)  # every 5 minutes


-----------------------------------------------

Traceback (most recent call last):
File "C:\Users\alexo\PycharmProjects\Twitter Daddy\Twitter Daddy.py", line 29, in module>
answer = "@{name} Hi {t}, I'm dad!".format(name=tweet.user.screen_name)
KeyError: 't'


All help is appreciated and thank you very much!

What I have tried:

A lot of things, I've been working on this for about 12 hours. Nothing too relevant as I am new.
Posted
Updated 2-Dec-19 5:05am
v3
Comments
Richard MacCutchan 1-Dec-19 13:48pm    
You have declared a variable called t but you do not assign it a value.
Munchacho 1-Dec-19 14:02pm    
I assigned it to tweet.full_text didn't I?
Richard MacCutchan 1-Dec-19 14:11pm    
I don’t know Twitter, but in the format statement t is not declared, so the resulting answer is most likely incomplete.

1 solution

You probably need something like this;

tweet_mode='extended').items(max_tweets):
    answer = "@{name} Hi {t}, I'm dad!".format(name=tweet.user.screen_name, t=tweet.full_text)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900