日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python qqbot 實現qq機器人的示例代碼

系統 2057 0

qqbot 是一個用 python 實現的、基于騰訊 SmartQQ 協議的 QQ 機器人框架,可運行在 Linux 、 Windows 和 Mac OSX 平臺下。

你可以通過擴展 qqbot 來實現:

  • 監控、收集 QQ 消息
  • 自動消息推送
  • 聊天機器人
  • 通過 QQ 遠程控制你的設備

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()

          

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 镇原县| 若尔盖县| 张掖市| 丰顺县| 贺兰县| 乐昌市| 永州市| 固阳县| 安乡县| 葵青区| 崇阳县| 任丘市| 惠水县| 屏南县| 集安市| 南通市| 若尔盖县| 固原市| 南昌县| 桦南县| 隆德县| 望奎县| 虹口区| 安阳市| 屏东市| 将乐县| 雷州市| 兰州市| 利津县| 都安| 平罗县| 城固县| 金寨县| 都兰县| 城步| 景宁| 永吉县| 邵东县| 临汾市| 滦平县| 襄城县|