How can I make this twitter bot work? Python 3.8

北城余情 提交于 2019-12-23 07:08:23

问题


I have all kinds of problems with this code, this is a large portion of another stackoverflow user's code, but I'm assuming they might have never finished it, because I get all kinds of errors. Famously, 'Status' object has no attribute 'replace.' Which I THINK I have fixed, no idea, couldn't find anything on it online, although it was a variable so I tried the non variable version and still nothing. Now I have this problem.

Yes
Traceback (most recent call last):
File "C:\Users\alexo\Desktop\Twitter Dad.py", line 28, in <module>
answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
TypeError: can only concatenate str (not "tuple") to str

Don't know what is wrong with this, the point of the script is for this bot, to search for when someone's tweet starting with "I'm ____" and respond with "Hi ____, I'm dad!" Just a humorous thing, but I'm totally lost. All help is appreciated, thank you!

New problem!

    import tweepy
    import tweepy as tt
    import time
    import sys
    import importlib 
    from importlib import reload
    importlib.reload(sys)

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

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

    max_tweets = 100

    for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
    c = tweet
    c = ("im ","")
    answer="@"+tweet.user.screen_name+" Hi " + c + ", I'm Dad!"
    print("Reply:",answer)
    api.update_status(status=answer)
    time.sleep(300) #every 5 minutes

Traceback (most recent call last):

File "C:\Users\alexo\Desktop\Twitter Dad.py", line 26, in tweet_contents = tweet.split() # assuming tweet is a str AttributeError: 'Status' object has no attribute 'split'

Error:

       Traceback (most recent call last):
       File "C:\Users\alexo\Desktop\Twitter Dad.py", line 26, in <module>
       tweet_contents = tweet.split() # assuming tweet is a str
       AttributeError: 'Status' object has no attribute 'split'

Code

import tweepy
import tweepy as tt
import time
import sys
import importlib
from importlib import reload
importlib.reload(sys)

#login credentials twitter account
consumer_key = '4QGxdJFmn4phBS0z9JTMU1S27'
consumer_secret = 'VuOkfqCEq6YcEeI2Sg6hhP4a7xrMHbhzORr9gQKyX8XfETbbnX'
access_token = '1200414502304591872-lj89lAd7r8oFbXEwDaVxg7YnH8pddO'
access_secret = '3PUfawYhaQJYDU6VYriKiVZNA9zYAAc1UOXe1UREgtWH7'

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

max_tweets = 100

for tweet in tweepy.Cursor(api.search, q=search_query).items(max_tweets):
    tweet_contents = tweet.split() # assuming tweet is a str
i = tweet_contents.index("I'm")
c = tweet_contents[i + 1]
answer = "@{name} Hi {c}, I'm dad!".format(name=tweet.user.screen_name)
print("Reply:",answer)

api.update_status.(status=answer)
time.sleep(300) #every 5 minutes

回答1:


First of all, there is no apparent reason c should be assigned to a tuple, so I'll assume that is removed and replaced with the tweet content.

That said, you should use Python's build in string formatting rather than string concatenation.

# Using the .format() function:
answer = "@{name} Hi {c}, I'm dad!".format(name=tweet.user.screen_name, c=c)

# Using an "f-string":
answer = f"@{tweet.user.screen_name} Hi {c}, I'm dad!"

EDIT

Per comment from OP:

Okay so I got it to tweet now thanks to you! But it only tweets @JohnnyAppleseed Hi ('im ', ''), I'm dad! Do you know how I could get it to quote whatever came after "I'm"? Only thing I changed was the .format() line. Thank you so much!

To grab whatever comes after the string I'm in the source tweet, you'll need to first find the index of the word I'm, then pull the word that follows it. Forgive the variable names, I'm commuting.

tweet_contents = tweet.split() # assuming tweet is a str
i = tweet_contents.index("I'm")
c = tweet_contents[i + 1]

For example, if the tweet was "I'm sad", this would split that into a list, ["I'm", "sad"], then get the index of "I'm" at 0, then get "sad" from index 0 + 1.



来源:https://stackoverflow.com/questions/59119530/how-can-i-make-this-twitter-bot-work-python-3-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!