问题描述
我希望每次提及该机器人时,该机器人以一定的文字和图片进行回复。例如文字是“小熊维尼”,图片是小熊维尼。我已经将图像和文本存储在json文件中。我得到的错误是这样的:
api.update_status("@"+tweet.user.screen_name+data[n]["text"]+media_ids=media_list,tweet.id)
^
SyntaxError: expression cannot contain assignment,perhaps you meant "=="?
我只是不知道如何写包含文本和图像的回复。同样,lastseen.txt文件是最后提到该机器人的推文的ID。
import tweepy
import time
import random
import json
consumer_key=''
consumer_secret=''
key=''
secret=''
auth=tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(key,secret)
api = tweepy.API(auth)
with open('pll/file.json') as json_file:
data = json.load(json_file)
randomlist = [0,1,2,3,4]
for i in range(0,4):
n = random.randint(0,4)
randomlist.append(n)
media_list = list()
response = api.media_upload(data[n]["image"])
media_list.append(response.media_id_string)
FILE_NAME = 'pll/lastseen.txt'
def read_last_seen(FILE_NAME):
file_read = open(FILE_NAME,'r')
last_seen_id = int(file_read.read().strip())
file_read.close()
return last_seen_id
def store_last_seen(FILE_NAME,last_seen_id):
file_write=open(FILE_NAME,'w')
file_write.write(str(last_seen_id))
file_write.close()
return
def reply():
tweets=api.mentions_timeline(read_last_seen(FILE_NAME),tweet_mode='extended')
for tweet in reversed(tweets):
print(str(tweet.id) + ' _ ' + tweet.full_text)
api.update_status("@"+tweet.user.screen_name+data[n]["text"]+media_ids=media_list,tweet.id)
store_last_seen(FILE_NAME,tweet.id)
reply()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)