我正在尝试编写一个 Twitter 机器人脚本,该机器人将响应其中包含方程式的提及。首先,我让提及工作(它会回应任何提及它的人)。然后,我尝试实现使用正则表达式的数学函数(我已经创建了这个,它只是将它集成到主机器人程序中的一种手段)。
提及代码:
import mathbotcreds as mtc
import logging
import re
import tweepy
from time import sleep as wait
auth = tweepy.OAuthHandler(mtc.CONSUMER_KEY, mtc.CONSUMER_SECRET)
auth.set_access_token(mtc.ACCESS_TOKEN, mtc.ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=2)
try:
api.verify_credentials()
print("Authentication Successful!")
except:
print("Error during authentication! :(")
mentions = api.mentions_timeline()
pattern = r'([0-9]+.*[-+*/%].*[0-9]+)+'
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def check_mentions(api, since_id):
logger.info("Collecting mentions... ")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline, since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
if tweet.in_reply_to_status_id is not None:
continue
api.update_status(
status=f"Hello! \n\nIt worked! \nYay! ^-^ \n\n (You said: \"{tweet.text}\".)",
in_reply_to_status_id=tweet.id)
return new_since_id
def main():
since_id = 1
while True:
since_id = check_mentions(api, since_id)
logger.info("Waiting... ")
wait(15)
if __name__ == "__main__":
logger.info("Running script... ")
wait(1)
main()
# for m in mentions:
# api.update_status(f"@{m.user.screen_name} Hello! \nYou said: \n{m.text}", m.id)
# wait(15)
提及和方程函数的代码:
import mathbotcreds as mtc
import logging
import re
import tweepy
from time import sleep as wait
auth = tweepy.OAuthHandler(mtc.CONSUMER_KEY, mtc.CONSUMER_SECRET)
auth.set_access_token(mtc.ACCESS_TOKEN, mtc.ACCESS_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
retry_count=2)
try:
api.verify_credentials()
print("Authentication Successful!")
except:
print("Error during authentication! :(")
mentions = api.mentions_timeline()
pattern = r'([0-9]+.*[-+*/%].*[0-9]+)+'
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
def check_mentions(api, since_id):
logger.info("Collecting mentions... ")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline, since_id=since_id).items():
match = re.search(pattern, tweet.text)
equation = tweet.text[match.start():match.end()]
new_since_id = max(tweet.id, new_since_id)
if tweet.in_reply_to_status_id is not None:
continue
if match:
ans = eval(tweet.text[match.start():match.end()])
api.update_status(
status=f"The answer to {str(equation)} is {ans}. ",
in_reply_to_status_id=tweet.id)
elif not match:
api.update_status(
status=f"Hello! \n\nIt worked! \nYay! ^-^ \n\n (You said: \"{tweet.text}\".)",
in_reply_to_status_id=tweet.id)
return new_since_id
def main():
since_id = 1
while True:
since_id = check_mentions(api, since_id)
logger.info("Waiting... ")
wait(15)
if __name__ == "__main__":
logger.info("Running script... ")
wait(1)
main()
# for m in mentions:
# api.update_status(f"@{m.user.screen_name} Hello! \nYou said: \n{m.text}", m.id)
# wait(15)
AttributeError: 'NoneType' object has no attribute 'start'
当我运行它时,我在eval()
函数 ( equation = tweet.text[match.start():match.end()]
)上收到一条错误消息。我研究了这个以及如何索引推文文本(使用 Tweepy)。NoneType
如果我在函数的正上方有一个函数,我很困惑为什么会出现错误eval()
。这不应该抓住吗?为什么会这样?
谢谢!