Reputation: 450
I'm trying to script a Twitter bot that will respond to mentions that have equations in them. First, I got the mentions to work (it would respond to anyone who mentions it). Then, I tried to implement the math function which uses regex (I had already created this, it was just a means of integrating it into the main bot program).
The Code for Mentions:
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)
The Code for Mentions and Equations Functions:
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)
When I run this, I get an error stating, AttributeError: 'NoneType' object has no attribute 'start'
on the eval()
function (equation = tweet.text[match.start():match.end()]
). I have researched this and how to index tweet text (with Tweepy). I'm confused as to why I get a NoneType
error if I have a function directly above the eval()
function. Shouldn't this catch that? Why does this happen?
Thanks!
Upvotes: 1
Views: 132
Reputation: 7513
re.search
returns a NoneType
when it doesn't find a match. You should check the return value before using it, like this:
match = re.search(pattern, tweet.text)
if match:
equation = tweet.text[match.start():match.end()]
Upvotes: 1