Python qqbot 實(shí)現(xiàn)qq機(jī)器人的示例代碼
qqbot 是一個(gè)用 python 實(shí)現(xiàn)的、基于騰訊 SmartQQ 協(xié)議的 QQ 機(jī)器人框架,可運(yùn)行在 Linux 、 Windows 和 Mac OSX 平臺(tái)下。
你可以通過擴(kuò)展 qqbot 來實(shí)現(xiàn):
- 監(jiān)控、收集 QQ 消息
- 自動(dòng)消息推送
- 聊天機(jī)器人
- 通過 QQ 遠(yuǎn)程控制你的設(shè)備
qqbot項(xiàng)目Gayhub地址:https://github.com/pandolia/qqbot
# -*- coding: utf-8 -*-
import qqbot
from qqbot import QQBotSlot as qqbotslot, RunBot
from qqbot import _bot as bot
import time
import json
import urllib
keyList = ['撿', '丟', '飯卡', ] # 匹配關(guān)鍵字
def check(keylist, str):
for key in keyList:
if (key in str):
return True
return False
@qqbot.QQBotSlot
def onQQMessage(bot, contact, member, content):
# bot: QQBot對象,提供List / SendTo / Stop / Restart等接口
# contact: QContact對象,消息的發(fā)送者,具有ctype / qq / uin / nick / mark / card / name等屬性
# member: QContact對象,僅當(dāng)本消息為群消息或討論組消息時(shí)有效,代表實(shí)際發(fā)消息的成員
# content: str對象,消息內(nèi)容
if '@ME' in content: # 如果有人艾特的機(jī)器人
message = content.replace('[@ME] ', '')
# 添加名字的ASCII碼,能夠進(jìn)行語義的連貫,而不是突兀的開啟另外一段對話
asciistr = ''
for i in range(len(member.name)):
asciistr += (str(ord(member.name[i]))) # 組裝名字的字符編碼,盡量的是唯一的
if i > 3:
break
# 調(diào)用圖靈機(jī)器人,進(jìn)行對話的回復(fù),如果出現(xiàn)圖靈機(jī)器人,替換為浮沉沉
bot.SendTo(contact, get_message(message, int(asciistr)).replace('圖靈機(jī)器人', '浮沉沉'))
elif content == '-stop':
bot.SendTo(contact, 'QQ機(jī)器人已關(guān)閉')
bot.Stop()
elif check(keyList, content) and member.name != '靜默':
# bot.SendTo(contact, '您發(fā)送的消息是' + content)
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 + "發(fā)送消息:" + content # 組裝消息
sendMsgToGroup(strzz, ['測試數(shù)據(jù)群'], bot)
print(strzz + " contact.mark" + contact.mark)
def sendMsgToGroup(msg, groupList, bot):
# print('向群里發(fā)送消息')
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):
# print('向好友發(fā)送消息')
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 = '測試消息是發(fā)送到群里面的'
buddyMsg = '測試消息是發(fā)送給好友的'
# print('os.getcwd()', os.getcwd())
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(',')
# sendMsgToGroup(groupMsg, qqGroupList, bot)
# sendMsgToBuddy(buddyMsg, qqBuddyList, bot)
def get_message(message, userid):
tuling = '2581f443bf364fd8a927fe87832e3d33' # 圖靈機(jī)器人的id(用戶自己創(chuàng)建的)
api_url = "http://openapi.tuling123.com/openapi/api/v2" # API接口調(diào)用
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) # 得到網(wǎng)頁HTML代碼
response_str = response.read().decode('utf8') # 將網(wǎng)頁的代碼轉(zhuǎn)化為UTF-8 處理 避免亂碼
response_dic = json.loads(response_str) # 將得到的json格式的信息轉(zhuǎn)換為Python的字典格式
results_text = response_dic['results'][0]['values']['text']
return results_text
if __name__=='__main__':
bot.Login(['-q', '710469775'])
# main(bot)
RunBot()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python 實(shí)現(xiàn)自動(dòng)導(dǎo)入缺失的庫
這篇文章主要介紹了Python 實(shí)現(xiàn)自動(dòng)導(dǎo)入缺失的庫,解決導(dǎo)入 Python 庫失敗的問題,本文分三種情況給大家介紹,需要的朋友可以參考下2019-10-10
使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法
這篇文章主要介紹了使用python將mysql數(shù)據(jù)庫的數(shù)據(jù)轉(zhuǎn)換為json數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python的ORM框架中SQLAlchemy庫的查詢操作的教程
這篇文章主要介紹了Python的ORM框架中SQLAlchemy庫的查詢操作的教程,SQLAlchemy用來操作數(shù)據(jù)庫十分方便,需要的朋友可以參考下2015-04-04
matplotlib 生成的圖像中無法顯示中文字符的解決方法
這篇文章主要介紹了matplotlib 生成的圖像中無法顯示中文字符的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

