-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
90 lines (68 loc) · 2.36 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import tweepy
import json
import time
import ast
from subprocess import call
print("Starting twitter bot!")
CONSUMER_KEY = ""
CONSUMER_KEY_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
print(api.me().name)
#user = "jplsek"
#print("Sending %s a message!" % user)
#api.send_direct_message(screen_name=user, text="Hello Creator!")
#dms = api.direct_messages()
#print(dms)
def runHelp(user, message=""):
if (message != ""):
message = message + ", "
sendDM(user, message + "The Available commands are: Math")
def sendDM(user, message):
print("Sending message (to %s): %s" % (user, message))
api.send_direct_message(user=user, text=message)
# sleep the script to not spam
time.sleep(10)
class StreamListener(tweepy.StreamListener):
def on_connect(self):
print("Connected!")
def on_disconnect(self, notice):
print(notice)
def on_status(self, status):
status = json.loads(status)
print(status.keys())
def on_data(self, status):
status = json.loads(status)
if ("direct_message" in status.keys()):
message = status["direct_message"]["text"].lower()
user = status["direct_message"]["sender"]["id"]
print("Message received (from %s): %s" % (str(user), message))
# don't message myself
if (user == 829050530928418816):
print("Dont message myself")
return
words = message.split(" ")
if (words[0] == "help"):
runHelp(user)
elif (words[0] == "math"):
words.remove("math")
expression = ' '.join(words)
ret = ""
try:
ret = ast.literal_eval(expression)
except:
ret = "Error evaluating expression!"
sendDM(user, ret)
elif (words[0] == "play"):
if (words[1] == "journey"):
call(["cvlc", "http://birch.ethanjs.com/Stone-In-Love.mp4"])
else:
runHelp(user, "Unknown command")
def on_error(self, status):
print(status)
stream = tweepy.Stream(auth, StreamListener())
stream.userstream()