Python qqbot 实现qq机器人的示例代码

作者:浮沉_y 时间:2021-05-18 12:43:43 

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

来源:https://blog.csdn.net/qq_36706625/article/details/83689724

标签:Python,qqbot,qq机器人
0
投稿

猜你喜欢

  • python中正则表达式的使用详解

    2023-08-08 14:47:13
  • JS实现移动端判断上拉和下滑功能

    2023-07-13 22:05:21
  • PHP的PDO大对象(LOBs)

    2023-06-07 06:45:36
  • mysql myisam优化设置

    2010-03-13 16:59:00
  • Web开发者的百科全书——Google DocType

    2008-07-03 13:06:00
  • golang中defer的基本使用教程

    2023-07-03 01:19:02
  • 如何解决客户机页面刷新时连接不上数据库问题?

    2009-12-16 18:24:00
  • javascript根据像素点取位置示例

    2023-09-03 22:58:54
  • php Exception异常处理详解

    2023-05-29 21:51:37
  • Python字符串字母大小写转换的各种情况详析

    2023-11-23 07:35:41
  • [翻译]标记语言和样式手册 Chapter 10 应用CSS

    2008-02-02 18:44:00
  • Asp 操作Access数据库时出现死锁.ldb的解决方法

    2011-03-29 10:49:00
  • asp多关键词查询方案

    2008-05-09 12:24:00
  • 如何隐藏IP地址的最后一位

    2011-04-04 16:38:00
  • 快速让MySQL数据库服务器支持远程连接

    2010-01-16 13:06:00
  • 两组字符串数据比较合并相同数据

    2008-07-31 17:27:00
  • 47个惊人的CSS3动画演示

    2010-02-07 12:32:00
  • python 中的 return 解析

    2023-11-03 13:21:47
  • ASP中转换unicode编码为gb2312函数

    2007-10-22 17:46:00
  • Java+mysql本地图片上传数据库及下载示例

    2023-07-23 19:49:13
  • asp之家 网络编程 m.aspxhome.com