您好,登錄后才能下訂單哦!
qqbot 是一個用 python 實現的、基于騰訊 SmartQQ 協議的 QQ 機器人框架,可運行在 Linux 、 Windows 和 Mac OSX 平臺下。
你可以通過擴展 qqbot 來實現:
qqbot項目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 = ['撿', '丟', '飯卡', ] # 匹配關鍵字 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對象,消息的發送者,具有ctype / qq / uin / nick / mark / card / name等屬性 # member: QContact對象,僅當本消息為群消息或討論組消息時有效,代表實際發消息的成員 # content: str對象,消息內容 if '@ME' in content: # 如果有人艾特的機器人 message = content.replace('[@ME] ', '') # 添加名字的ASCII碼,能夠進行語義的連貫,而不是突兀的開啟另外一段對話 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 != '靜默': # bot.SendTo(contact, '您發送的消息是' + 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 + "發送消息:" + content # 組裝消息 sendMsgToGroup(strzz, ['測試數據群'], bot) print(strzz + " contact.mark" + contact.mark) def sendMsgToGroup(msg, groupList, bot): # print('向群里發送消息') 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('向好友發送消息') 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 = '測試消息是發送給好友的' # 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' # 圖靈機器人的id(用戶自己創建的) api_url = "http://openapi.tuling123.com/openapi/api/v2" # API接口調用 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) # 得到網頁HTML代碼 response_str = response.read().decode('utf8') # 將網頁的代碼轉化為UTF-8 處理 避免亂碼 response_dic = json.loads(response_str) # 將得到的json格式的信息轉換為Python的字典格式 results_text = response_dic['results'][0]['values']['text'] return results_text if __name__=='__main__': bot.Login(['-q', '710469775']) # main(bot) RunBot()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。