2019-08-22 18:33:10 +08:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
# coding=UTF-8
|
|
|
|
|
|
|
|
|
|
import os, time,\
|
|
|
|
|
requests, \
|
|
|
|
|
json, ntpath
|
|
|
|
|
|
|
|
|
|
from libs.core.TalkService import *
|
2019-08-23 18:12:41 +08:00
|
|
|
|
from .connection import Yuuki_Connect
|
|
|
|
|
|
|
|
|
|
from .i18n import Yuuki_LangSetting
|
2019-08-22 18:33:10 +08:00
|
|
|
|
|
|
|
|
|
class Yuuki:
|
2019-08-23 18:12:41 +08:00
|
|
|
|
def __init__(self, Seq, Yuuki_Connection, helper_LINE_ACCESS_KEYs, Lang="en", Admin=[]):
|
2019-08-22 18:33:10 +08:00
|
|
|
|
self.Seq = Seq
|
2019-08-23 18:12:41 +08:00
|
|
|
|
self.Admin = Admin
|
|
|
|
|
self.i18n = Yuuki_LangSetting(Lang)
|
|
|
|
|
|
2019-08-22 18:33:10 +08:00
|
|
|
|
self.LINE_Media_server = "https://obs.line-apps.com"
|
|
|
|
|
|
2019-08-23 18:12:41 +08:00
|
|
|
|
self.Connect = Yuuki_Connect(Yuuki_Connection)
|
2019-08-22 18:33:10 +08:00
|
|
|
|
|
2019-08-22 18:52:24 +08:00
|
|
|
|
(self.client, self.listen) = self.Connect.connect()
|
2019-08-22 18:33:10 +08:00
|
|
|
|
self.connectHeader = Yuuki_Connection.connectHeader
|
|
|
|
|
|
2019-08-22 18:52:24 +08:00
|
|
|
|
for access in helper_LINE_ACCESS_KEYs:
|
|
|
|
|
self.Connect.helperConnect(access)
|
|
|
|
|
|
2019-08-22 18:33:10 +08:00
|
|
|
|
self.MyMID = self.client.getProfile().mid
|
|
|
|
|
|
2019-08-23 18:12:41 +08:00
|
|
|
|
_ = self.i18n._
|
|
|
|
|
global _
|
|
|
|
|
|
2019-08-22 18:33:10 +08:00
|
|
|
|
def exit(restart=False):
|
|
|
|
|
if restart:
|
2019-08-23 18:12:41 +08:00
|
|
|
|
with open(".cache.sh", "w") as c:
|
|
|
|
|
c.write(sys.executable + " ./main.py")
|
2019-08-22 18:33:10 +08:00
|
|
|
|
os.system("sh .cache.sh")
|
|
|
|
|
os.system("rm .cache.sh")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
else:
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
|
|
def readCommandLine(self, msgs):
|
|
|
|
|
replymsg = ""
|
|
|
|
|
for msg in msgs:
|
|
|
|
|
replymsg = replymsg + " " + msg
|
|
|
|
|
return replymsg
|
|
|
|
|
|
|
|
|
|
def checkInInvitationList(self, ncMessage):
|
|
|
|
|
if ncMessage.param3 == self.MyMID:
|
|
|
|
|
inList = True
|
|
|
|
|
elif "\x1e" in ncMessage.param3:
|
|
|
|
|
if self.MyMID in ncMessage.param3.split("\x1e"):
|
|
|
|
|
inList = True
|
|
|
|
|
else:
|
|
|
|
|
inList = False
|
|
|
|
|
else:
|
|
|
|
|
inList = False
|
|
|
|
|
return inList
|
|
|
|
|
|
|
|
|
|
def changeGroupUrlStatus(self, group, stat):
|
|
|
|
|
if stat == True:
|
|
|
|
|
us = False
|
|
|
|
|
else:
|
|
|
|
|
us = True
|
|
|
|
|
group.members, group.invitee = None, None
|
|
|
|
|
group.preventJoinByTicket = us
|
|
|
|
|
self.client.send_updateGroup(self.Seq, group)
|
|
|
|
|
|
|
|
|
|
def cleanMyGroupInvitations(self):
|
|
|
|
|
for cleanInvitations in self.client.getGroupIdsInvited():
|
|
|
|
|
self.client.send_acceptGroupInvitation(self.Seq, cleanInvitations)
|
|
|
|
|
self.client.send_leaveGroup(self.Seq, cleanInvitations)
|
|
|
|
|
|
|
|
|
|
def getContact(self, mid):
|
|
|
|
|
if len(mid) == len(self.MyMID) and mid[0] == "u":
|
|
|
|
|
try:
|
|
|
|
|
contactInfo = self.getContact(mid)
|
|
|
|
|
except:
|
|
|
|
|
contactInfo = False
|
|
|
|
|
else:
|
|
|
|
|
contactInfo = False
|
|
|
|
|
return contactInfo
|
|
|
|
|
|
|
|
|
|
def sendToWho(self, Message):
|
|
|
|
|
if Message.message.toType == MIDType.USER:
|
|
|
|
|
return Message.message.from_
|
|
|
|
|
elif Message.message.toType == MIDType.ROOM:
|
|
|
|
|
return Message.message.to
|
|
|
|
|
elif Message.message.toType == MIDType.GROUP:
|
|
|
|
|
return Message.message.to
|
|
|
|
|
|
|
|
|
|
def sendText(self, toid, msg):
|
|
|
|
|
message = Message(to=toid, text=msg.encode('utf-8'))
|
|
|
|
|
self.client.send_sendMessage(self.Seq, message)
|
|
|
|
|
|
|
|
|
|
def sendUser(self, toid, mid):
|
|
|
|
|
message = Message(contentType=ContentType.CONTACT,
|
|
|
|
|
text='',
|
|
|
|
|
contentMetadata={
|
|
|
|
|
'mid': mid,
|
|
|
|
|
'displayName': 'LINE User',
|
|
|
|
|
},
|
|
|
|
|
to=toid)
|
|
|
|
|
self.client.send_sendMessage(self.Seq, message)
|
|
|
|
|
|
|
|
|
|
def sendMedia(self, toid, type, path):
|
|
|
|
|
if os.path.exists(path):
|
|
|
|
|
file_name = ntpath.basename(path)
|
|
|
|
|
file_size = len(open(path, 'rb').read())
|
|
|
|
|
message = Message(to=toid, text=None)
|
|
|
|
|
message.contentType = type
|
|
|
|
|
message.contentPreview = None
|
|
|
|
|
message.contentMetadata = {
|
|
|
|
|
'FILE_NAME': str(file_name),
|
|
|
|
|
'FILE_SIZE': str(file_size),
|
|
|
|
|
}
|
|
|
|
|
if type == ContentType.FILE:
|
|
|
|
|
media_name = file_name
|
|
|
|
|
else:
|
|
|
|
|
media_name = 'media'
|
|
|
|
|
message_id = self.client.sendMessage(self.Seq, message).id
|
|
|
|
|
files = {
|
|
|
|
|
'file': open(path, 'rb'),
|
|
|
|
|
}
|
|
|
|
|
params = {
|
|
|
|
|
'name': media_name,
|
|
|
|
|
'oid': message_id,
|
|
|
|
|
'size': file_size,
|
|
|
|
|
'type': ContentType._VALUES_TO_NAMES[type].lower(),
|
|
|
|
|
'ver': '1.0',
|
|
|
|
|
}
|
|
|
|
|
data = {
|
|
|
|
|
'params': json.dumps(params)
|
|
|
|
|
}
|
|
|
|
|
url = self.LINE_Media_server + '/talk/m/upload.nhn'
|
|
|
|
|
r = requests.post(url, headers=self.connectHeader, data=data, files=files)
|
|
|
|
|
if r.status_code != 201:
|
|
|
|
|
self.client.sendText(toid, "Error!")
|
|
|
|
|
|
|
|
|
|
def Poll(self):
|
|
|
|
|
NoWork = 0
|
|
|
|
|
catchedNews = []
|
|
|
|
|
ncMessage = Operation()
|
|
|
|
|
Revision = self.client.getLastOpRevision()
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
if NoWork == 300:
|
|
|
|
|
Revision = self.client.getLastOpRevision()
|
|
|
|
|
catchedNews = self.listen.fetchOperations(Revision, 50)
|
|
|
|
|
if catchedNews:
|
|
|
|
|
NoWork = 0
|
|
|
|
|
for ncMessage in catchedNews:
|
|
|
|
|
if ncMessage.type == OpType.RECEIVE_MESSAGE:
|
|
|
|
|
self.Commands(ncMessage)
|
|
|
|
|
elif ncMessage.type == OpType.NOTIFIED_INVITE_INTO_GROUP:
|
|
|
|
|
self.Main(ncMessage)
|
|
|
|
|
if ncMessage.reqSeq != -1:
|
|
|
|
|
Revision = max(Revision, ncMessage.revision)
|
|
|
|
|
else:
|
|
|
|
|
NoWork = NoWork + 1
|
|
|
|
|
except SystemExit:
|
|
|
|
|
self.exit()
|
|
|
|
|
except EOFError:
|
|
|
|
|
pass
|
|
|
|
|
except:
|
|
|
|
|
err1, err2, err3 = sys.exc_info()
|
|
|
|
|
try:
|
|
|
|
|
if catchedNews and ncMessage:
|
|
|
|
|
Finded = False
|
|
|
|
|
for Catched in catchedNews:
|
|
|
|
|
if Catched.revision == ncMessage.revision:
|
|
|
|
|
Finded = True
|
|
|
|
|
if Finded:
|
|
|
|
|
Revision = Catched.revision
|
|
|
|
|
break
|
|
|
|
|
if not Finded:
|
|
|
|
|
Revision = self.client.getLastOpRevision()
|
2019-08-23 18:12:41 +08:00
|
|
|
|
for Root in self.Admin:
|
2019-08-22 18:33:10 +08:00
|
|
|
|
self.sendText(Root, "Star Yuuki BOT - Something was wrong...\nError:\n%s\n%s\n%s" %
|
|
|
|
|
(err1, err2, err3))
|
|
|
|
|
except:
|
|
|
|
|
print("Star Yuuki BOT - Damage!\nError:\n%s\n%s\n%s" % (err1, err2, err3))
|
|
|
|
|
self.exit()
|
|
|
|
|
|
|
|
|
|
def Main(self, ncMessage):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def Commands(self, ncMessage):
|
|
|
|
|
if 'BOT_CHECK' in ncMessage.message.contentMetadata:
|
|
|
|
|
pass
|
|
|
|
|
elif ncMessage.message.toType == MIDType.ROOM:
|
|
|
|
|
self.client.send_leaveRoom(self.Seq, ncMessage.message.to)
|
|
|
|
|
elif ncMessage.message.contentType == ContentType.NONE:
|
|
|
|
|
if 'Yuuki/mid' == ncMessage.message.text:
|
2019-08-23 18:12:41 +08:00
|
|
|
|
self.sendText(self.sendToWho(ncMessage), _("LINE System UserID:\n") + ncMessage.message.from_)
|
2019-08-22 18:33:10 +08:00
|
|
|
|
elif 'Yuuki/Speed' == ncMessage.message.text:
|
|
|
|
|
Time1 = time.time()
|
2019-08-23 18:12:41 +08:00
|
|
|
|
self.sendText(self.sendToWho(ncMessage), _("Testing..."))
|
2019-08-22 18:33:10 +08:00
|
|
|
|
Time2 = time.time()
|
2019-08-23 18:12:41 +08:00
|
|
|
|
self.sendText(self.sendToWho(ncMessage), _("Speed:\n%ss") % (Time2 - Time1,))
|