forked from IIIIIHIIIII/WhatBot
/
layer.py
154 lines (146 loc) · 9.96 KB
/
layer.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
from yowsup.layers.interface import YowInterfaceLayer, ProtocolEntityCallback
from yowsup.layers.protocol_messages.protocolentities import TextMessageProtocolEntity
from yowsup.layers.protocol_acks.protocolentities import OutgoingAckProtocolEntity
from yowsup.layers.protocol_receipts.protocolentities import OutgoingReceiptProtocolEntity
import tipbot
import crypto
import threading
import bitrefill
import wiki
import trivia
import time
class EchoLayer(YowInterfaceLayer):
disable = []
def cleanList(self,text):
while "" in text:
text.remove("")
return text
def displayText(self,gotMessage,messageProtocolEntity):
reply = bitrefill.placeOrder(gotMessage,messageProtocolEntity.getFrom())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.disable.remove(messageProtocolEntity.getFrom())
self.toLower(messageEntity)
def Timer(self,qfor,messageProtocolEntity):
time.sleep(25)
try:
trivia.answer[qfor]
del(trivia.answer[qfor])
reply = "Trivia timeout."
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
except KeyError:
pass
@ProtocolEntityCallback("message")
def onMessage(self, messageProtocolEntity):
if True:
if messageProtocolEntity.getType() == 'text':
receipt = OutgoingReceiptProtocolEntity(messageProtocolEntity.getId(), messageProtocolEntity.getFrom(), 'read', messageProtocolEntity.getParticipant())
self.toLower(receipt)
gotMessage = self.cleanList(messageProtocolEntity.getBody().split(" "))
if (messageProtocolEntity.getParticipant() and messageProtocolEntity.getParticipant() not in self.disable):
if(gotMessage[0] == "!register" and len(gotMessage) == 1):
reply = tipbot.register(messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!balance" and len(gotMessage) == 1):
reply = tipbot.balance(messageProtocolEntity.getParticipant(),"")
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!tip" and len(gotMessage) == 3):
reply = tipbot.tip(gotMessage,messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!tag" and len(gotMessage) == 1):
reply = tipbot.getUserTag(messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!withdraw" and len(gotMessage) == 3):
reply = tipbot.withdraw(gotMessage,messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!address" and len(gotMessage) == 1):
reply = tipbot.getUserAdd(messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!change" and len(gotMessage) == 2):
reply = tipbot.changeTag(gotMessage,messageProtocolEntity.getParticipant())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!help" and len(gotMessage) == 1):
reply = tipbot.commands()
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getParticipant())
self.toLower(messageEntity)
elif(gotMessage[0] == "!market" and len(gotMessage) == 2):
reply = crypto.market(gotMessage)
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!convert" and len(gotMessage) == 4):
reply = crypto.convert(gotMessage)
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!info" and len(gotMessage) == 1):
reply = crypto.info()
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!trivia" and len(gotMessage) == 1):
reply = trivia.question(messageProtocolEntity.getFrom())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
threading.Thread(target=self.Timer,args=(messageProtocolEntity.getFrom(),messageProtocolEntity)).start()
elif(gotMessage[0] == "!ans" and len(gotMessage) >= 2):
result = trivia.ans(gotMessage[1:],messageProtocolEntity.getParticipant(),messageProtocolEntity.getFrom())
if result:
reply = "Correct!"
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
reply = tipbot.tip(["",tipbot.getUserTag(messageProtocolEntity.getParticipant()).replace("Your tag : ",""),"10"],"919892633961@s.whatsapp.net")
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!wiki" and len(gotMessage) >= 2):
for i in range(2,len(gotMessage)):
gotMessage[i] = gotMessage[i].capitalize()
reply = wiki.getWiki("%20".join(gotMessage[1:]))
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!cbid" or gotMessage[0] == "!cask" and len(gotMessage) == 3):
if gotMessage[0] == "!cbid":
createType = "createbid"
else:
createType = "createask"
reply = tipbot.placeBidAsk(gotMessage,messageProtocolEntity.getParticipant(),createType)
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!dbid" or gotMessage[0] == "!dask" and len(gotMessage) == 2):
if gotMessage[0] == "!dbid":
cancelType = "cancelbid"
else:
cancelType = "cancelask"
reply = tipbot.cancelBidAsk(gotMessage,messageProtocolEntity.getParticipant(),cancelType)
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!fbal" or gotMessage[0] == "!cbal" and len(gotMessage) == 1):
if gotMessage[0] == "!fbal":
baltype = "fiatbalance"
else:
baltype = "coinbalance"
reply = tipbot.coinbal(messageProtocolEntity.getParticipant(),baltype)
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(messageProtocolEntity.getFrom() not in self.disable):
if(gotMessage[0] == "!coinapi" and len(gotMessage) == 2):
reply = tipbot.coinAdd(gotMessage,messageProtocolEntity.getFrom())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!addr" and len(gotMessage) == 1):
reply = tipbot.getCoinsecAddr(messageProtocolEntity.getFrom())
messageEntity = TextMessageProtocolEntity(reply,to = messageProtocolEntity.getFrom())
self.toLower(messageEntity)
elif(gotMessage[0] == "!refill" and len(gotMessage) == 3):
self.disable.append(messageProtocolEntity.getFrom())
threading.Thread(target=self.displayText, args =(gotMessage,messageProtocolEntity)).start()
self.toLower(messageProtocolEntity.ack())
self.toLower(messageProtocolEntity.ack(True))
@ProtocolEntityCallback("receipt")
def onReceipt(self, entity):
ack = OutgoingAckProtocolEntity(entity.getId(), "receipt", entity.getType(), entity.getFrom())
self.toLower(ack)