-
Notifications
You must be signed in to change notification settings - Fork 8
/
robot.py
270 lines (209 loc) · 8.58 KB
/
robot.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import logging
import gettext
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
from telegram.ext import (
Updater, CommandHandler, Job,
RegexHandler, ConversationHandler,CallbackContext
)
from telegram.ext.dispatcher import run_async
from telegram import Update
from modules import config
from modules.ticker import Ticker, Markets
t = gettext.translation('robot', localedir='./locales', languages=['tr_TR'])
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%m-%d-%Y %I:%M:%S %p',
level=logging.WARN)
logger = logging.getLogger('CryptoCurrency Bot')
logger.addHandler(logging.FileHandler('.log', encoding='utf-8'))
LANGUAGE = 0
PAIR, MARKET = range(2)
tickr = Ticker()
def start(update: Update, context: CallbackContext):
user = update.message.from_user
reply_keyboard = [['🇬🇧 English', '🇹🇷 Türkçe']]
update.message.reply_text(
_("%s, choose your language.", user.id) % user.first_name,
reply_markup=ReplyKeyboardMarkup(reply_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
)
return LANGUAGE
def language(update: Update, context: CallbackContext):
user = update.message.from_user
lang = 'ENG' if 'English' in update.message.text else 'TR'
config.write(user.id, language=lang, user=user.first_name)
logger.info("Language of %s: %s", user.first_name, update.message.text)
update.message.reply_text(
_("Perfect, your language is set: %s", user.id) % update.message.text,
reply_markup=ReplyKeyboardRemove()
)
return -1
def ticker(update: Update, context: CallbackContext):
user = update.message.from_user
reply_keyboard = [['BTC/TRY'],
['BTC/USD', 'BTC/EUR'],
['ETH/USD', 'ETH/BTC'],
['ETC/USD', 'ETC/BTC'],
['LTC/USD', 'LTC/BTC'],
['XRP/USD', 'XRP/BTC'],
['XMR/USD', 'XMR/BTC']]
update.message.reply_text(
_("Choose a pair.", user.id),
reply_markup=ReplyKeyboardMarkup(reply_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
)
return PAIR
def pair(update: Update, context: CallbackContext):
user = update.message.from_user
couple = update.message.text
reply_keyboard = [['⬅️ Back'],
['Paribu', 'Koinim'],
['Bitstamp', 'Bitfinex'],
['Cexio', 'Poloniex'],
['Coinbase', 'Kraken'],
['BTCE', 'OKCoin']]
tickr.pair = couple
update.message.reply_text(
_("Now choose a market.", user.id),
reply_markup=ReplyKeyboardMarkup(reply_keyboard,
resize_keyboard=True,
one_time_keyboard=True)
)
return MARKET
def market(update: Update, context: CallbackContext):
user = update.message.from_user
market = update.message.text
if market == '⬅️ Back':
return ticker(bot, update)
tickr.market = Markets[market]
market_ticker = tickr.ticker()
if market_ticker is None:
update.message.reply_text(_("Unsupported pair.", user.id))
return
update.message.reply_text(
_("@ {}\n"
"Price: {}\n"
"Open: {}\n"
"24h High: {}\n"
"24h Low: {}\n"
"24h Volume: {}\n"
"24h Change: {}", user.id).format(tickr.market.value,
market_ticker['price'],
market_ticker['open24h'],
market_ticker['high24h'],
market_ticker['low24h'],
market_ticker['volume24h'],
market_ticker['change24h']),
)
def _(message, user_id):
translate = t.gettext
lang = config.language(user_id)
if lang == 'ENG':
return message
return translate(message)
@run_async
def notify_callback(bot, job):
user_id = job.context['user_id']
market = job.context['market']
pair = job.context['pair']
condition = job.context['condition']
condition_price = job.context['price']
try:
tickr.market = Markets[market]
tickr.pair = pair
last_price = float(tickr.ticker()['price'].split(' ')[1].replace(',', ''))
except:
job.schedule_removal()
chat_data = job.context['chat_data']
del chat_data['job']
bot.send_message(job.context['chat_id'], text=_('Notify Error: Incorrect parameters.', user_id))
return
if condition == '>':
if last_price > condition_price:
job.schedule_removal()
chat_data = job.context['chat_data']
del chat_data['job']
bot.send_message(job.context['chat_id'], text='Alarm: Last price: %s' % last_price)
elif condition == '<':
if last_price < condition_price:
job.schedule_removal()
chat_data = job.context['chat_data']
del chat_data['job']
bot.send_message(job.context['chat_id'], text='Alarm: Last price: %s' % last_price)
def notify(bot, update, args, job_queue, chat_data):
user = update.message.from_user
chat_id = update.message.chat_id
try:
if args[0] == 'remove':
if 'job' not in chat_data:
update.reply_text('You have no active alarm yet.')
return
_job = chat_data['job']
_job.schedule_removal()
del chat_data['job']
update.message.reply_text(_('Alarm successfully removed !', user.id))
return
arguments = {
'user_id': user.id,
'chat_id': chat_id,
'chat_data': chat_data,
'market': args[0],
'pair': args[1],
'condition': args[2],
'price': float(args[3])
}
job = job_queue.run_repeating(notify_callback, 60.0, context=arguments)
chat_data['job'] = job
update.message.reply_text(_('I will notify you when the price reach at the level you want ! (:', user.id))
except (IndexError, ValueError):
update.message.reply_text(_('Usage: /notify market pair > price\n'
'Example: /notify Poloniex BTC/USD < 2290', user.id))
def help(update: Update, context: CallbackContext):
user = update.message.from_user
update.message.reply_text(
_(
'/start - init bot\n'
'/ticker - ticker\n'
'/notify - to receive notifications from market\n'
'/cancel - cancel current operation\n'
'/help - see all available commands', user.id
)
)
def cancel(update: Update, context: CallbackContext):
user = update.message.from_user
logger.info("User %s canceled the conversation.", user.first_name)
update.message.reply_text(_('Removed keyboard.', user.id), reply_markup=ReplyKeyboardRemove())
return -1
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"', update, error)
def main():
updater = Updater(config.token, use_context=True)
dp = updater.dispatcher
conv_handler = ConversationHandler(
entry_points=[CommandHandler('start', start)],
states={
LANGUAGE: [RegexHandler('^(🇬🇧 English|🇹🇷 Türkçe)', language)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
ticker_handler = ConversationHandler(
entry_points=[CommandHandler('ticker', ticker)],
states={
PAIR: [RegexHandler('^(BTC/TRY|BTC/USD|BTC/EUR|ETH/USD|ETH/BTC|ETC/USD|ETC/BTC|LTC/USD|LTC/BTC|XRP/USD|XRP/BTC|XMR/USD|XMR/BTC)', pair)],
MARKET: [RegexHandler('^(⬅️ Back|Paribu|Koinim|Bitstamp|Bitfinex|Cexio|Poloniex|Coinbase|Kraken|BTCE|OKCoin)', market)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
dp.add_handler(conv_handler)
dp.add_handler(ticker_handler)
dp.add_handler(CommandHandler('help', help))
dp.add_handler(CommandHandler("notify", notify,
pass_args=True,
pass_job_queue=True,
pass_chat_data=True))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()