forked from mar9331/line-benri-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenri.py
More file actions
289 lines (253 loc) · 9.52 KB
/
Copy pathbenri.py
File metadata and controls
289 lines (253 loc) · 9.52 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from linepy import LINE, OEPoll
from akad.ttypes import OpType, MIDType, ContentType
import re
import time
import json
import requests
class Main:
def __init__(self):
self.msgcmd = MessageFunction(self)
self.action = OperationFunction(self)
self.ops = Operation(self)
self.sakura = LINE()
self.mid = self.sakura.profile.mid
self.sett = self.sakura.getSettings()
self.help = """mid
>>>個人アカウントのid
gid
>>>グループのid
leave
>>>グループから抜ける
curl
>>>グループうらる拒否
ourl
>>>グループうらる許可
ginfo
>>>グループ情報
gcreator
>>>グループ作成者
setpoint
>>>既読ポイント設置
delpoint
>>>既読ポイント破棄
checkread
>>>既読確認
help
>>>helpを表示
url
>>>追加URLを発行
mid @
>>>メンションした人のmid確認"""
self.poll = OEPoll(self.sakura)
self.timesleep = {}
self.checkread = {}
def running(self):
while True:
try:
ops = self.poll.singleTrace(count=50)
if ops:
for op in ops:
self.poll.setRevision(op.revision)
self.ops.getOperation(op)
except Exception as e:
print(e)
def check_time(self, to):
if to in self.timesleep:
if time.time() - self.timesleep[to] < 3:
return False
else:
self.timesleep[to] = time.time()
return True
class Operation:
def __init__(self, main):
self.main = main
def getOperation(self, op):
try:
if op.type == OpType.END_OF_OPERATION:
return
elif op.type == OpType.RECEIVE_MESSAGE:
self.main.action.RECEIVE_MESSAGE(op)
elif op.type == OpType.NOTIFIED_ADD_CONTACT:
self.main.action.NOTIFIED_ADD_CONTACT(op)
elif op.type == OpType.NOTIFIED_INVITE_INTO_GROUP:
self.main.action.NOTIFIED_INVITE_INTO_GROUP(op)
elif op.type == OpType.NOTIFIED_READ_MESSAGE:
self.main.action.NOTIFIED_READ_MESSAGE(op)
except Exception as e:
print(e)
class OperationFunction:
def __init__(self, main):
self.main = main
def NOTIFIED_READ_MESSAGE(self, op):
if op.param1 in self.main.checkread:
if op.param2 not in self.main.checkread[op.param1]:
self.main.checkread[op.param1].append(op.param2)
def RECEIVE_MESSAGE(self, op):
msg = op.message
if msg.contentType == ContentType.NONE and msg.toType == MIDType.GROUP:
if msg.text in self.main.msgcmd.command:
if self.main.check_time(msg.tp):
self.main.msgcmd.command[msg.text.lower()](msg)
elif msg.text.startswith(tuple(self.main.msgcmd.startswith_command.keys())):
cmds = [
x
for x in self.main.msgcmd.startswith_command
if msg.text.startswith(x)
]
if self.main.check_time(msf.to):
self.main.msgcmd.startswith_command[cmds[0]](msg)
elif len(msg.text) > 32:
if self.main.check_time(msg.to):
try:
self.main.msgcmd.sendCon(msg)
except:
pass
try:
self.main.msgcmd.sendGrp(msg)
except:
pass
elif msg.contentType == ContentType.CONTACT:
if self.main.check_time(msg.to):
self.main.msgcmd.contactInfo(msg)
def NOTIFIED_ADD_CONTACT(self, op):
pass # ほんとはここにある
def NOTIFIED_INVITE_INTO_GROUP(self, op):
if self.main.mid in op.param3:
self.main.sakura.acceptGroupInvitation(op.param1)
self.main.sakura.sendMessage(
op.param1,
"はじめまして!!\n便利ボットです\n連投対策として3秒間の空きが必要です\nコマンドはhelpで確認できます\n誰でも使用可能なのでお気軽にグループにお誘いください",
)
class MessageFunction:
def __init__(self, main):
self.main = main
self.command = {
"mid": self.yourMid,
"leave": self.leave,
"gid": self.groupId,
"curl": self.curl,
"ourl": self.ourl,
"ginfo": self.groupInfo,
"gurl": self.gurl,
"gcreator": self.gcreator,
"setpoint": self.setpoint,
"delpoint": self.delpoint,
"checkread": self.checkpoint,
"help": self.sendhelp,
"url": self.addurl,
}
self.startswith_command = {"mid ": self.getmid}
def sendhelp(self, msg):
self.main.sakura.sendMessage(msg.to, self.main.help)
def getIdFromStr(self, msg, types="mid"):
if types == "mid":
pattrn = r"u\w{32}"
elif types == "gid":
pattrn = r"c\w{32}"
else:
return []
ids = re.findall(pattrn, msg.text)
return ids
def sendCon(self, msg):
tar = self.getIdFromStr(msg, "mid")
if tar:
for mid in tar:
time.sleep(0.1)
self.main.sakura.sendContact(msg.to, mid)
def addurl(self, msg):
if self.main.sett.contactMyTicket:
self.main.sakura.sendMessage(
msg.to, f"line.me/ti/p/{self.main.sett.contactMyTicket}"
)
else:
self.main.sakura.sendMessage(
msg.to, f"line.me/ti/p/{self.main.sakura.reissueUserTicket()}"
)
def yourMid(self, msg):
self.main.sakura.sendMessage(msg.to, msg._from)
def sendGrp(self, msg):
tar = self.getIdFromStr(msg, "gid")
txt = ""
if tar:
for gid in tar:
grp = self.main.sakura.getCompactGroup(gid)
url = "OPEN" if not grp.preventedJoinByTicket == True else "CLOSE"
txt += f"Group Name : {grp.name}\n"
txt += f"Group Invitee : {len(grp.invitee) if grp.invitee else 0}\n"
txt += f"Group Members : {len(grp.members)}\n"
txt += f"Group Ticket : {url}\n\n"
txt += f"Total : {len(tar)}"
self.main.sakura.sendMessage(msg.to, txt)
def contactInfo(self, msg):
if "displayName" in msg.contentMetadata:
mid = msg.contentMetadata["mid"]
con = self.main.sakura.getContact(mid)
txt = f"mid : {mid}\ndisplayName : {con.displayName}"
self.main.sakura.sendMessage(msg.to, txt)
def setpoint(self, msg):
self.main.checkread[msg.to] = []
self.main.sakura.sendMessage(msg.to, "Set Ok")
def checkpoint(self, msg):
if msg.to in self.main.checkread:
cons = self.main.sakura.getContacts(self.main.checkread[msg.to])
txt = "\n".join([con.displayName for con in cons])
txt += f"\n{len(cons)}人が既読しました"
self.main.sakura.sendMessage(msg.to, txt)
def delpoint(self, msg):
if msg.to in self.main.checkread:
del self.main.checkread[msg.to]
self.main.sakura.sendMessage(msg.to, "Del Ok")
else:
self.main.sakura.sendMessage(msg.to, "No Point")
def groupInfo(self, msg):
grp = self.main.sakura.getCompactGroup(msg.to)
url = "OPEN" if not grp.preventedJoinByTicket == True else "CLOSE"
txt = f"Group Name : {grp.name}\n"
txt += f"Group Id : {grp.id}\n"
txt += f"Group Invitee : {len(grp.invitee) if grp.invitee else 0}\n"
txt += f"Group Members : {len(grp.members)}\n"
txt += f"Group Ticket : {url}"
self.main.sakura.sendMessage(msg.to, txt)
def mention(self, msg):
if "MENTION" in msg.contentMetadata:
key = eval(msg.contentMetadata["MENTION"])
return [x["M"] for x in key["MENTIONEES"]]
else:
return []
def getmid(self, msg):
for mid in self.mention(msg):
self.main.sakura.sendMessage(msg.to, mid)
def leave(self, msg):
self.main.sakura.leaveGroup(msg.to)
def groupId(self, msg):
self.main.sakura.sendMessage(msg.to, msg.to)
def gurl(self, msg):
self.main.sakura.sendMessage(
msg.to,
f"https://line.me/R/ti/g/{self.main.sakura.reissueGroupTicket(msg.to)}",
)
def ourl(self, msg):
grp = self.main.sakura.getGroup(msg.to)
if grp.preventedJoinByTicket:
grp.preventedJoinByTicket = False
self.main.sakura.updateGroup(grp)
self.main.sakura.sendMessage(msg.to, "開きました")
else:
self.main.sakura.sendMessage(msg.to, "既に開いています")
def curl(self, msg):
grp = self.main.sakura.getGroup(msg.to)
if grp.preventedJoinByTicket == False:
grp.preventedJoinByTicket = True
self.main.sakura.updateGroup(grp)
self.main.sakura.sendMessage(msg.to, "閉じました")
else:
self.main.sakura.sendMessage(msg.to, "既に閉じています")
def gcreator(self, msg):
grp = self.main.sakura.getGroup(msg.to)
if grp.creator:
self.main.sakura.sendContact(msg.to, grp.creator.mid)
else:
self.main.sakura.sendMessage(msg.to, "削除済みです")
if __name__ == "__main__":
main = Main()
main.running()