-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto-telegram-bot.py
213 lines (193 loc) · 8.28 KB
/
crypto-telegram-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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import re
from time import sleep
from bs4 import BeautifulSoup
from decouple import config
from praw import Reddit
from requests import get, codes
from selenium import webdriver
from telebot import TeleBot, util
def get_token(grant_type, client_id, client_secret):
data = {
'grant_type': grant_type,
'client_id': client_id ,
'client_secret': client_secret
}
url = 'https://api.coinmarketcal.com/oauth/v2/token'
r = get(url, data)
if r.status_code == codes.ok:
return r.json().get('access_token')
return None
def get_coinmarketcal_events(coin):
token = get_token(config('GRANT_TYPE'), config('COINMARKETCAL_CLIENT_ID'),
config('COINMARKETCAL_CLIENT_SECRET'))
if token:
url = 'https://api.coinmarketcal.com/v1/events'
data = {
'access_token': token,
'coins': coin
}
try:
r = get(url, data)
if r.status_code == codes.ok:
content = r.json()
if content:
coin_events = coin.title() + ' CoinMarketCal Events:\n\n'
for event in content:
coin_events += f"{event.get('date_event')[:10]}\n{event.get('title')}\n"\
f"{event.get('description')}\n{event.get('proof')}\n\n"
return coin_events
except Exception:
return "Oops! Invalid coin name or event wasn't found."
return None
def get_coincalendarinfo_events(event_type):
types = {
'': '3,1266,1267,564',
'hot': '3',
'conf': '1266',
'meetup': '1267',
'ann': '564'
}
try:
type = types[event_type]
url = f'http://www.coincalendar.info/wp-json/eventon/calendar?event_type={type}\
&number_of_months=1&event_count=20'
except Exception:
return 'Opps! Invalid event type.'
r = get(url)
if codes.ok:
content = r.json().get('html').strip()
pattern = re.compile(r'(\'name\'>.*?</span>)')
events = (pattern.findall(content))
pattern = re.compile(r'(itemprop=\'image\' content=\'.*?jpg)')
images = (pattern.findall(content))
coin_events = 'CoinCalendarInfo Events:\n\n'
for i in range(len(events)):
coin_events += f'{events[i][7:events[i].index("<")]}\n{images[i][26:]}\n\n'
return coin_events
return None
def get_reddit_data(subreddit_name, limit):
try:
reddit = Reddit(
client_id=config('REDDIT_ID'),
client_secret=config('REDDIT_SECRET'),
username=config('REDDIT_USERNAME'),
password=config('REDDIT_PASSWORD'),
user_agent=config('REDDIT_USER_AGENT')
)
subreddit = reddit.subreddit(subreddit_name).hot(limit=limit)
subreddit_content = f'{subreddit_name.title()} Reddit posts: \n\n'
for sub in subreddit:
subreddit_content += f'{sub.title}\nhttps://reddit.com{sub.permalink}\n\n'
return subreddit_content
except Exception:
return 'Oops! Invalid API credentials or subreddit name.'
def get_bitcointalk_data(coin):
try:
driver = webdriver.Chrome()
driver.get('http://www.google.com/')
driver.find_element_by_class_name('gsfi')\
.send_keys(f'{coin} btctalk ann 2018')
driver.find_element_by_name('btnK').click()
result = driver.find_element_by_class_name('r')
result.find_element_by_tag_name('a').click()
driver.find_elements_by_class_name('navPages')[-2].click()
posts = driver.find_elements_by_class_name('post')
content = f'{coin.title()} Bitcointalk posts:\n\n'
for post in posts:
content += f'{post.text}\n\n'
except Exception as e:
return 'Oops! Invalid coin name.'
finally:
driver.quit()
return content
def get_cointelegraph_news():
r = get('https://cointelegraph.com/')
bs = BeautifulSoup(r.content, 'html.parser')
post_titles = [p.text for p in bs.select('span.postTitle')][:20]
post_links = [p.attrs.get('href') for p in bs.select('div.image > a')][:20]
content = 'Cointelegraph posts:\n\n'
for title, link in zip(post_titles, post_links):
content += f'{title}\n{link}\n\n'
return content
def get_price_alert_notify(coin, alert_price, currency):
while True:
try:
url = f'https://coinmarketcap.com/currencies/{coin}/'
r = get(url)
if r.status_code == codes.ok:
bs = BeautifulSoup(r.content, 'html.parser')
if currency == 'usd':
price_usd = bs.select('span#quote_price > span')[0].text
if float(price_usd) > float(alert_price):
return f'{coin.title()} hit your alert price ${alert_price} - ${price_usd}!'
price_sats = bs.select('span.text-gray > span')[0].text
if float(price_sats) > float(alert_price):
return f'{coin.title()} hit your alert price {alert_price} sats - {price_sats} sats!'
except Exception as e:
return 'Oops! Invalid coin name.'
sleep(5)
return None
def main():
try:
bot = TeleBot(token=config('TELEGRAM_BOT_TOKEN'))
@bot.message_handler(commands=['start'])
def welcome(msg):
bot.reply_to(msg, 'Welcome to Crypto Bot!\nType /help to see available commands!')
@bot.message_handler(commands=['help'])
def help(msg):
bot.reply_to(msg, 'Commands:\n/cmcal (coin name) - Get coin events from coinmarketcal.com\n' +
'/coincal (event_type) - Get coin events from coincalendar.info\n' +
'/reddit (subreddit name, limit) - Get subreddit posts\n' +
'/btctalk (coin name) - Get latest posts from bitcointalk.org\n' +
'/news - Get latest crypto news from cointelegraph.com\n' +
'/alert (coin name, price_alert, currency) - Get notify when coin hit specified price!')
@bot.message_handler(func=lambda msg: True)
def answer(msg):
split_msg = msg.text.split()
choice = split_msg[0]
if choice == '/cmcal':
try:
coin = split_msg[1]
except IndexError:
bot.reply_to(msg, 'Opps! The parameter is missing.')
finally:
bot.reply_to(msg, get_coinmarketcal_events(coin))
elif choice == '/coincal':
try:
event_type = split_msg[1]
except IndexError:
event_type = ''
finally:
bot.reply_to(msg, get_coincalendarinfo_events(event_type))
elif choice == '/reddit':
try:
subreddit = split_msg[1]
limit = int(split_msg[2])
except IndexError:
limit = 5
finally:
bot.reply_to(msg, get_reddit_data(subreddit, limit))
elif choice == '/btctalk':
coin = split_msg[1]
content = get_bitcointalk_data(coin)
splitted_text = util.split_string(content, 3000)
for txt in splitted_text:
bot.reply_to(msg, txt)
elif choice == '/cryptonews':
bot.reply_to(msg, get_cointelegraph_news())
elif choice == '/alert':
try:
bot.reply_to(msg, get_price_alert_notify(split_msg[1],
split_msg[2],
split_msg[3]))
except IndexError:
bot.reply_to(msg, 'Oops! The parameters are missing.')
else:
bot.reply_to(msg, 'Oops! Wrong choice.')
except Exception as ex:
print(str(ex))
bot.reply_to(msg, 'Oops! An unexpected error has occurred.')
finally:
bot.polling()
if __name__ == '__main__':
main()