-
Notifications
You must be signed in to change notification settings - Fork 5
/
bot.py
123 lines (97 loc) · 4 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
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
import datetime
import logging
import os
import random
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
PORT = int(os.environ.get('PORT', '8443'))
#from app.utils import FirstTry
#datastorage = FirstTry()
from py3cw.request import Py3CW
p3cw = Py3CW(
key=os.environ.get("KEY"),
secret=os.environ.get("SECRET"),
request_options={
'request_timeout': 10,
'nr_of_retries': 5,
'retry_status_codes': [502],
'retry_backoff_factor': 0.1
}
)
long_array = ['https://c.tenor.com/SY_Rb9FZFb4AAAAS/cat-jam-stonks.gif', "https://i.giphy.com/media/YnkMcHgNIMW4Yfmjxr/giphy.webp", "https://i.kym-cdn.com/photos/images/original/001/956/488/119.gif", "https://www.yankodesign.com/images/design_news/2021/02/auto-draft/youtooz_stonks_stinks_collectible_figure_2.gif"]
short_array = ["https://c.tenor.com/lhey0DPlkiQAAAAM/rage-red-stocks.gif" ]
# Command Handlers
def start(update, context):
# how do we pass class instance here
context.bot.sendAnimation(chat_id=update.message.chat_id,
animation = 'https://c.tenor.com/8IIQDBECgssAAAAM/hello-sexy-hi.gif',
caption='Hello!',
)
#datastorage.add_counter()
"""Send a message when the command /start is issued."""
update.message.reply_text(f'Hi! Enter the id for the marketplace to get the latest signal')
#update.message.reply_text(f'Hi! number:{datastorage.counter}')
def help(update, context):
"""Send a message when the command /help is issued."""
#update.message.reply_text('Help! Help! Help! Help!')
def echo(update, context):
#https://core.telegram.org/bots/api#sendanimation
#https://stackoverflow.com/questions/35294948/telegram-python-chatbot-replying-with-an-animated-gif
"""Echo the user message."""
#print(update.message.text)
error, data = p3cw.request(
entity='marketplace',
action='signals',
action_id = update.message.text
)
if len(error):
update.message.reply_text(f' Enter Correct ID')
#print(data)
if data[0]['signal_type'] == 'long':
context.bot.sendAnimation(
chat_id = update.message.chat_id,
animation = long_array[random.randrange(4)-1],
caption = 'Long Signal!',
)
else:
context.bot.sendAnimation(
chat_id = update.message.chat_id,
animation = short_array[0],
caption = 'Short Signal!',
)
pair = data[0]['pair']
exchange = data[0]['exchange']
id = data[0]['id']
time = datetime.datetime.fromtimestamp(
int(data[0]['timestamp'])
).strftime('%Y-%m-%d %H:%M:%S')
min = data[0]['min']
max = data[0]['max']
update.message.reply_text(f' Pair: {pair}, Exchange: {exchange}, Id: {id}, Time: {time}, Min: {min}, Max: {max}')
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
TOKEN = os.environ.get("API_KEY")
APP_NAME='https://nft-link-telegram-bot.herokuapp.com/'
updater = Updater(TOKEN, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN, webhook_url=APP_NAME + TOKEN)
updater.idle()
if __name__ == '__main__':
main()