import
qqbot
from
qqbot
import
QQBotSlot as qqbotslot, RunBot
from
qqbot
import
_bot as bot
import
time
import
json
import
urllib
keyList
=
[
'捡'
,
'丢'
,
'饭卡'
, ]
def
check(keylist,
str
):
for
key
in
keyList:
if
(key
in
str
):
return
True
return
False
@qqbot
.QQBotSlot
def
onQQMessage(bot, contact, member, content):
if
'@ME'
in
content:
message
=
content.replace(
'[@ME] '
, '')
asciistr
=
''
for
i
in
range
(
len
(member.name)):
asciistr
+
=
(
str
(
ord
(member.name[i])))
if
i >
3
:
break
bot.SendTo(contact, get_message(message,
int
(asciistr)).replace(
'图灵机器人'
,
'浮沉沉'
))
elif
content
=
=
'-stop'
:
bot.SendTo(contact,
'QQ机器人已关闭'
)
bot.Stop()
elif
check(keyList, content)
and
member.name !
=
'静默'
:
datatime
=
time.strftime(
'%Y.%m.%d %H:%M:%S'
, time.localtime(time.time()))
print
(
'member ='
, member.name
+
'
', '
contact
=
', contact.name)
strzz
=
contact.name
+
':'
+
datatime
+
" "
+
member.name
+
"发送消息:"
+
content
sendMsgToGroup(strzz, [
'测试数据群'
], bot)
print
(strzz
+
" contact.mark"
+
contact.mark)
def
sendMsgToGroup(msg, groupList, bot):
for
group
in
groupList:
print
(
'group ='
, group)
bg
=
bot.
List
(
'group'
, group)
if
bg:
b
=
bg[
0
]
bot.SendTo(b, msg)
def
sendMsgToBuddy(msg, buddyList, bot):
for
buddy
in
buddyList:
print
(
'buddy'
,
type
(buddy), buddy)
bb
=
bot.
List
(
'buddy'
, buddy)
if
bb:
b
=
bb[
0
]
bot.SendTo(b, msg)
def
main(bot):
groupMsg
=
'测试消息是发送到群里面的'
buddyMsg
=
'测试消息是发送给好友的'
with
open
(
'./qq.txt'
,
'r'
, encoding
=
'UTF-8'
) as fr:
qqGroup
=
fr.readline().strip()
qqBuddy
=
fr.readline().strip()
print
(
'fr'
, fr,
'\nqqGroup ='
, qqGroup,
'\nqqBuddy'
, qqBuddy)
qqGroupList
=
qqGroup.split(
','
)
qqBuddyList
=
qqBuddy.split(
','
)
def
get_message(message, userid):
tuling
=
'2581f443bf364fd8a927fe87832e3d33'
api_url
=
"http://openapi.tuling123.com/openapi/api/v2"
req
=
{
"perception"
:
{
"inputText"
:
{
"text"
: message
},
"selfInfo"
:
{
"location"
:
{
"city"
:
"深圳"
,
"province"
:
"广州"
,
"street"
:
"XXX"
}
}
},
"userInfo"
:
{
"apiKey"
: tuling,
"userId"
: userid
}
}
req
=
json.dumps(req).encode(
'utf8'
)
http_post
=
urllib.request.Request(api_url, data
=
req, headers
=
{
'content-type'
:
'application/json'
})
response
=
urllib.request.urlopen(http_post)
response_str
=
response.read().decode(
'utf8'
)
response_dic
=
json.loads(response_str)
results_text
=
response_dic[
'results'
][
0
][
'values'
][
'text'
]
return
results_text
if
__name__
=
=
'__main__'
:
bot.Login([
'-q'
,
'710469775'
])
RunBot()