You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/usr/bin/env python# -*- coding: utf-8 -*-importsysreload(sys)
sys.setdefaultencoding('utf8')
frommastodonimportMastodon,StreamListenerimportsqlite3aslitefrombs4importBeautifulSoupbasedir="/home/you/yourbot/"mastodon=Mastodon(
api_base_url='https://botsin.space',
client_id=basedir+'clientcred.txt',
access_token=basedir+'usercred.txt'
)
defhtmlText(html):
soup=BeautifulSoup(html, 'html.parser')
lines= []
forpinsoup('p'):
lines.append(p.text)
return'\n'.join(lines)
defresponse(username,vis,text,mentionid):
ifvis=="public": # respond to plublic as unlisted to try to prevent public timeline flooding status=mastodon.status_post("@"+str(username) +" "+text, in_reply_to_id=mentionid,visibility="unlisted")
else: # otherwise respond with the same privacy level status=mastodon.status_post("@"+str(username) +" "+text, in_reply_to_id=mentionid,visibility=vis)
classBotListener( StreamListener ):
defon_update( self, _ ):
passdefon_delete( self, _ ):
passdefon_notification( self, notification ):
ifnotification['type'] =='mention':
mention_html=notification['status']['content'] # get the mentionmention_text=BeautifulSoup(mention_html, 'html.parser')
mention_text=mention_text.get_text() # convert it to textmention_text=mention_text.replace("yourbot ","") # strip the bot name from the mentionmentionid=str(notification['status']['id'])
username=notification['status']['account']['acct']
mention=mention_text.lower() # set mention to lower case to make it easier to deal withvis=notification['status']['visibility']
display_name=notification['status']['account']['display_name'] # get user display name in case you want to use it for somethingif ("give"inmention):
give_arr=mention.split() # split mention into arraytotal=len(give_arr) # count the words as totalglyph=give_arr[1] # assume glyph to be the second termto= (total-2) # assume "to" to be the second to last term ifgive_arr[0] =="give": # confirm that give is the first termif ((give_arr[to] =="to") and ("@"ingive_arr[total-1])): # confirm that "to" is where we think it isrecipient=give_arr[total-1] # set the last term to "recipient"if ("@"inrecipient) and (recipient[:1] !="@"): # make sure there is an @ but they don't start recipient name with an @ so it doesnt link to the user when they make the requeststry: # search for the glyph and get the glyph_idcon=lite.connect(basedir+'glyphs.db')
con.row_factory=lite.Rowwithcon:
cur=con.cursor()
cur.execute("select glyph_id, glyph_description from GLYPHS where username=? and glyph=? limit 1;",(username,glyph))
query_result=cur.fetchall()
con.close()
glyph_id=query_result[0][0]
glyph_description=query_result[0][1]
except:
print"search query failed"try: # update the glyph usernamecon=lite.connect(basedir+'glyphs.db')
con.row_factory=lite.Rowwithcon:
cur=con.cursor()
cur.execute("update GLYPHS set username=? where glyph_id=? limit 1;",(recipient,glyph_id))
query_result=cur.fetchall()
con.close()
text=username+" has presented you with the "+glyph_description+" - "+glyphresponse(username,vis,text,mentionid)
except:
print"update query failed"stream=mastodon.stream_user( listener=BotListener() )
stream.stream_user()