-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
executable file
·84 lines (66 loc) · 2.9 KB
/
commands.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
#!/usr/bin/env python
import subprocess
from twython import Twython
import configparser
def read_twitter_bot_config():
try:
config = configparser.ConfigParser()
config.read('config_auth.properties')
# Twython authentication
# your twitter consumer and access information goes here
apiKey = config['twitter']['apiKey']
apiSecret = config['twitter']['apiSecret']
accessToken = config['twitter']['accessToken']
accessTokenSecret = config['twitter']['accessTokenSecret']
receiverTwitterId = config['twitter']['receiverTwitterId']
return apiKey, apiSecret, accessToken, accessTokenSecret, receiverTwitterId
except Exception as e:
print(str(e))
print('Could not find the required config_auth.properties file with proper values. Kindly go through Readme.md!')
raise ValueError('Could not get config!')
def get_formatted_data(receiverTwitterId, textMessage):
return {
'event': {
'type': 'message_create',
'message_create': {
'target': {'recipient_id': receiverTwitterId, },
'message_data': {
'text': textMessage,
}
}
}
}
def check_dm_and_respond():
# get configs
apiKey, apiSecret, accessToken, accessTokenSecret, receiverTwitterId = read_twitter_bot_config()
# initialise Twython
api = Twython(apiKey, apiSecret, accessToken, accessTokenSecret)
# Get the last DM
DMs = api.get_direct_messages(count=1, skip_status='true')
# print(DMs)
if 'events' in DMs and len(DMs['events']) > 0:
msg = DMs['events'][0]
if 'message_create' in msg and 'message_data' in msg['message_create'] and 'text' in msg['message_create'][
'message_data']:
text = msg['message_create']['message_data']['text']
text = text.lower()
print('Received DM : {}'.format(text))
if 'ip' in text:
output = subprocess.check_output("ifconfig | grep inet", shell=True)
event_data = get_formatted_data(receiverTwitterId, str(output))
api.send_direct_message(**event_data)
print('Sent IP address')
if 'ifconfig' in text:
output = subprocess.check_output("ifconfig", shell=True)
event_data = get_formatted_data(receiverTwitterId, str(output))
api.send_direct_message(**event_data)
print('Sent ifconfig')
if 'wifi' in text or 'iwconfig' in text:
output = subprocess.check_output("iwgetid", shell=True)
event_data = get_formatted_data(receiverTwitterId, str(output))
api.send_direct_message(**event_data)
else:
print("Don't know how to handle this text")
print("Done")
if __name__ == "__main__":
check_dm_and_respond()