QQ Chat Robot of Python QQQBot Library

Time:2019-10-6

In this paper, we share the specific code of QQ chat robot in Python QQQBot library for your reference. The details are as follows.

Project address: https://github.com/pandolia/qqbot

1. installation


pip install qqbot

2. Actively Send Messages

from qqbot import _bot as bot

Log on to QQ
bot.Login(['-q', '2816626661'])

'''
Buddy gets friends with the specified name / comment
Group acquisition group
'''
buddy = bot.List('buddy', 'b.K')

# Judgment is the buddha's good friend
if buddy:
 b = buddy[0]
 # Send a message
 bot.SendTo(b, 'nihao')

3. Respond to messages based on events

First we need to start QQ on the command line


c:\Python3.6.4\virtual\env_qq\Scripts>activate

(env_qq) c:\Python3.6.4\virtual\env_qq\Scripts>qqbot -q 2816626661

Next, write a script for automatic reply.

from qqbot import _bot as bot

def onQQMessage(bot, contact, member, content):
 if content == '-hello':
  Bot. SendTo (contact,'Hello, I'm a QQ robot')
 elif content == '-stop':
  Bot. SendTo (contact,'QQ robot closed')
  bot.Stop()

Next put it in the C: user / xxx /. qqbot-tmp / plugins / folder

Keep the previous command line window running, restart one, and execute


qq plug recall

Where “recall” is the name of the PY file we just put in

Testing feasibility

Description of parameters:

Bot: QQBot object, providing List/SendTo/Stop/Restart and other interfaces,
Contact: QContact object, sender of message, with ctype/qqq/uin/nick/mark/card/name and other attributes
Member: QContact object, valid only if this message is a group message or a discussion group message, representing the actual member of the message sent
Content: str object, message content

4. Combining Turing Robot

Register a Turing Robot account here at http://www.tuling123.com/

Rewrite the script just now

from qqbot import _bot as bot
import requests
import json

def getReplay(message):
 url = 'http://openapi.tuling123.com/openapi/api/v2'
 data = {
  "perception": {
   "inputText": {
    "text": message
   },
  },
  "userInfo": {
   "ApiKey": "Your APIkey"
   "UserId": "Your uesrID"
  }
 }
 response = requests.post(url=url, json=data)
 return response.text


def onQQMessage(bot, contact, member, content):
 if content == '-hello':
  Bot. SendTo (contact,'Hello, I'm a QQ robot')
 else:
  response = getReplay(content)
  bot.SendTo(contact, json.loads(response)['results'][0]['values']['text'])

Next, just reload it.

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.