-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackbot.py
130 lines (111 loc) · 3.68 KB
/
slackbot.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import print_function
from slackclient import SlackClient
from collections import OrderedDict
from apiclient.discovery import build
from apiclient import errors
from httplib2 import Http
from oauth2client import file, client, tools
import requests
import os
import json
import sys
import imaplib
import getpass
import email
import email.header
import datetime
EMAIL_ACCOUNT = "[email protected]"
# Use 'INBOX' to read inbox. Note that whatever folder is specified,
# after successfully running this script all emails in that folder
# will be marked as read.
EMAIL_FOLDER = "INBOX"
token = os.environ.get('SLACKBOT_TOKEN')
def process_mailbox(M):
"""
Do something with emails messages in the folder.
For the sake of this example, print some headers.
"""
rv, data = M.search(None, "ALL")
if rv != 'OK':
print("No messages found!")
return
for num in data[0].split():
rv, data = M.fetch(num, '(RFC822)')
if rv != 'OK':
print("ERROR getting message", num)
return
msg = email.message_from_bytes(data[0][1])
body = msg.get_payload()
hdr = email.header.make_header(email.header.decode_header(msg['Subject'])[0])
subject = str(hdr)
print('Message %s: %s' % (num, subject))
print('Message body: ' + body)
print('Raw Date:', msg['Date'])
# Now convert to local date-time
date_tuple = email.utils.parsedate_tz(msg['Date'])
if date_tuple:
local_date = datetime.datetime.fromtimestamp(
email.utils.mktime_tz(date_tuple))
print ("Local Date:", \
local_date.strftime("%a, %d %b %Y %H:%M:%S"))
def print_email_headers():
M = imaplib.IMAP4_SSL('imap.gmail.com')
try:
rv, data = M.login(EMAIL_ACCOUNT, getpass.getpass())
except imaplib.IMAP4.error:
print ("LOGIN FAILED!!! ")
sys.exit(1)
print(rv, data)
rv, mailboxes = M.list()
if rv == 'OK':
print("Mailboxes:")
print(mailboxes)
rv, data = M.select(EMAIL_FOLDER)
if rv == 'OK':
print("Processing mailbox...\n")
process_mailbox(M)
M.close()
else:
print("ERROR: Unable to open mailbox ", rv)
M.logout()
def get_market_data(ticker):
request_url = "https://api.binance.com/api/v1/ticker/24hr?symbol=" + ticker
r = requests.get(request_url)
response = json.loads(r.text, object_pairs_hook=OrderedDict)
myObject = {
"symbol" : response['symbol'],
"24HrChangePercent" : response['priceChangePercent'],
"lastPrice" : response['lastPrice'],
"24HourHigh" : response['highPrice'],
"24HourLow" : response['lowPrice'],
"quoteVolume" : response['quoteVolume'],
}
return(myObject)
def slack_message(message, channel):
sc = SlackClient(token)
sc.api_call('chat.postMessage', channel=channel,
text=message, username='pythonbot',
icon_emoji=':robot_face:')
def gmail_setup():
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))
return service
def gmail_exec(service):
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
if __name__ == "__main__":
print_email_headers()
# service = gmail_setup()
# gmail_exec(service)
# slack_message(get_market_data("ETHBTC"), "cmc-alerts")