forked from peakshift/telegram-dogecoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
141 lines (127 loc) Β· 4.7 KB
/
run.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
import requests
import time
from block_io import BlockIo
import os
token = os.environ['TELEGRAM_BOT_TOKEN'] #Telegram bot token
url = "https://api.telegram.org/bot%s/" %(token)
n = 0
version = 2
block_io = BlockIo(os.environ['BLOCKIO_API_KEY'], os.environ['BLOCKIO_PIN'], version)
active_users = {}
monikers_tuple = [
("sandwich","sandwiches",21),
("coffee", "coffees",7),
("tea", "teas",5),
("lunch", "lunches",49)
]
monikers_dict = {n[i]: n[2] for n in monikers_tuple for i in range(2)}
monikers_flat = [monikers_tuple[i][j] for i in range(len(monikers_tuple)) for j in range(3)]
monikers_str = '\n'.join(f"{i[0]}: {i[2]} doge" for i in monikers_tuple)
def getCount(chatid):
n = []
t = time.time()
chat_users = active_users[chatid]
for i in chat_users:
if t - chat_users[i] <= 600:
n.append(i)
return n
def sendMsg(message,chatid):
requests.get(url + "sendMessage", data={"chat_id":chatid,"text":message})
def returnBal(username):
data = block_io.get_address_balance(labels=username)
balance = data['data']['balances'][0]['available_balance']
pending_balance = data['data']['balances'][0]['pending_received_balance']
return (balance, pending_balance)
def process(message,username,chatid):
message = message.split(" ")
for i in range(message.count(' ')):
message.remove(' ')
if "/register" in message[0]:
try:
block_io.get_new_address(label=username)
sendMsg("@"+username+" you are now registered.",chatid)
except:
sendMsg("@"+username+" you are already registered.",chatid)
elif "/balance" in message[0]:
try:
(balance, pending_balance) = returnBal(username)
sendMsg("@"+username+" Balance : "+balance+ "Doge ("+pending_balance+" Doge)",chatid)
except:
sendMsg("@"+username+" you are not registered yet. use /register to register.",chatid)
elif "/tip" in message[0]:
try:
person = message[1].replace('@','')
amount_msg = 1 if message[2] in ('a', 'an', '1') else message[2]
amount = abs(float(amount_msg)) * monikers_dict.get(message[3], 1)
if monikers_dict.get(message[3], 0) == 0:
sin_plu = "doge"
elif amount_msg == 1:
sin_plu = monikers_tuple[monikers_flat.index(message[3])//3][0]
else:
sin_plu = monikers_tuple[monikers_flat.index(message[3])//3][1]
block_io.withdraw_from_labels(amounts=str(amount), from_labels=username, to_labels=person)
sendMsg("@"+username+" tipped "+ str(amount_msg) + " " + sin_plu +
("" if monikers_dict.get(message[3], 0) == 0 else f" ({str(amount)} doge)") +
" to @"+person+"",chatid)
(balance, pending_balance) = returnBal(person)
sendMsg("@"+person+" Balance : "+balance+ "Doge ("+pending_balance+" Doge)",chatid)
except ValueError:
sendMsg("@"+username+" invalid amount.",chatid)
except:
sendMsg("@"+username+" insufficient balance or @"+person+" is not registered yet.",chatid)
elif "/address" in message[0]:
try:
data = block_io.get_address_by_label(label=username)
sendMsg("@"+username+" your address is "+data['data']['address']+"",chatid)
except:
sendMsg("@"+username+" you are not registered yet. use /register to register.",chatid)
elif "/withdraw" in message[0]:
try:
amount = abs(float(message[1]))
address = message[2]
data = block_io.withdraw_from_labels(amounts=str(amount), from_labels=username, to_addresses=address)
except ValueError:
sendMsg("@"+username+" invalid amount.",chatid)
except:
sendMsg("@"+username+" insufficient balance or you are not registered yet.",chatid)
elif "/rain" in message[0]:
try:
users = getCount(chatid)
if username in users:
users.remove(username)
number = len(users)
amount = ("10," * (number - 1)) + '10'
name = username
username = ((username+',') * (number - 1)) + username
if number < 2:
sendMsg("@"+username+" less than 2 shibes are active.",chatid)
else:
print(amount)
print(username)
block_io.withdraw_from_labels(amounts=amount, from_labels=username, to_labels=','.join(users))
sendMsg("@"+name+" is raining on "+','.join(users)+"",chatid)
except:
pass
elif "/monikers" in message:
sendMsg("--MONIKERS--\n" +
monikers_str,chatid)
elif "/active" in message:
sendMsg("Current active : %d shibes" %(len(getCount(chatid))),chatid)
else:
global active_users
try:
active_users[chatid][username] = time.time()
except KeyError:
active_users[chatid] = {}
active_users[chatid][username] = time.time()
while True:
try:
print("such dogeshift. much running.")
data = requests.get(url+"getUpdates", data={"offset":n}).json()
n = data["result"][0]["update_id"] + 1
username = data["result"][0]["message"]["from"]['username']
chatid = data["result"][0]["message"]["chat"]["id"]
message = data["result"][0]["message"]["text"]
process(message,username,chatid)
except:
pass