python制作机器人的实现方法
作者:川川菜鸟 发布时间:2022-11-09 21:09:05
标签:python,机器人
是否也像拥有自己的机器人呢?
不挨个展示了。
比如说你想实现一个夸人的功能:
"""
作者:川川
时间:2021/4/6
"""
from nonebot.adapters.cqhttp import Message, PokeNotifyEvent,Bot
from nonebot import on_notice
import warnings
from nonebot.permission import *
import requests
warnings.filterwarnings("ignore")
from aiocqhttp.exceptions import Error as CQHttpError
poke = on_notice()
@poke.handle()
async def _(bot: Bot, event: PokeNotifyEvent):
if event.is_tome() and event.user_id != event.self_id:
msg=await kua()
chuo = f"[CQ:tts,text={msg}]"
try:
await poke.send(Message(f'{Message(chuo)}'))
except CQHttpError:
pass
async def kua():
url = 'https://chp.shadiao.app/api.php'
resp = requests.get(url)
return resp.text
或者你也想机器人发送美女图片:
"""
作者:川川
时间:2021/5/5
"""
from nonebot.adapters.cqhttp import Message
from nonebot import on_keyword,on_notice
from nonebot.typing import T_State
from nonebot.adapters import Bot, Event
import requests, re
from aiocqhttp.exceptions import Error as CQHttpError
from nonebot.adapters.cqhttp import message,GroupMessageEvent,Message,MessageEvent
from nonebot.typing import T_State
from nonebot.permission import SUPERUSER
yulu = on_keyword({'涩图'},priority=10)
@yulu.handle()
async def j(bot: Bot, event:message, state: T_State):
msg = await mei()
try:
await yulu.send(Message(msg))
except CQHttpError:
pass
async def mei():
url = 'https://api.66mz8.com/api/rand.img.php?type=美女&format=json'
resp = requests.get(url)
data = resp.json()
ur = data.get('pic_url')
tu = f"[CQ:image,file={ur}]"
return tu
再或者你想要机器人每天定时给你发天气预报:
"""
作者:川川
时间:2021/5/10
"""
from nonebot import require
import nonebot
import requests
scheduler = require('nonebot_plugin_apscheduler').scheduler
@scheduler.scheduled_job('cron', hour='12',minute='08', id='sleep4')
async def co():
# d = time.strftime("%m-%d %H:%M:%S", time.localtime())
url = 'http://apis.juhe.cn/simpleWeather/query?city=上海&key=a8b3dd5052f0e3e2dff14175165500d6'
data = requests.get(url=url, timeout=5).json()
# to=resp['result']['future'][0]
t = "时间:" + data['result']['future'][0]['date']
w = "温度:" + data['result']['future'][0]['temperature']
e = "天气:" + data['result']['future'][0]['weather']
f = "风向:" + data['result']['future'][0]['direct']
a = "时间:" + data['result']['future'][1]['date']
b = "温度:" + data['result']['future'][1]['temperature']
c = "天气:" + data['result']['future'][1]['weather']
g = "风向:" + data['result']['future'][1]['direct']
tu=str(t + '\n' + w + '\n' + e + '\n' + f + '\n\n\n' + a + '\n' + b + '\n' + c + '\n' + g)
bot = nonebot.get_bots()['1786691956']
return await bot.call_api('send_msg', **{
'message': '天气预报:\n{}'.format(tu),
'user_id': '2835809579'
})
当然也有bot源码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import nonebot
from nonebot.adapters.cqhttp import Bot as CQHTTPBot
# Custom your logger
#
# from nonebot.log import logger, default_format
# logger.add("error.log",
# rotation="00:00",
# diagnose=False,
# level="ERROR",
# format=default_format)
# You can pass some keyword args config to init function
nonebot.init()
app = nonebot.get_asgi()
driver = nonebot.get_driver()
driver.register_adapter("cqhttp", CQHTTPBot)
nonebot.load_builtin_plugins()
nonebot.load_from_toml("pyproject.toml")
# Modify some config / config depends on loaded configs
#
# config = driver.config
# do something...
if __name__ == "__main__":
nonebot.logger.warning("Always use `nb run` to start the bot instead of manually running!")
nonebot.run(app="__mp_main__:app")
当然还有一些配置文件源码,我觉得可能没有多的必要继续发,新手可能看不懂了。大概就
似乎群友玩得挺嗨!
来源:https://blog.csdn.net/weixin_46211269/article/details/117921934
0
投稿
猜你喜欢
- 结合mysql数据库查询,实现分页效果@user.route("/user_list",methods=['PO
- Jupyter Notebook默认不显示行号,可是当我们代码报错时,发现会显示自己多少行出现错误。eg:这时候我们总不能一行行去数吧,因此
- web.config第一种方法:<?xml version="1.0" encoding="utf-8&
- Runtime包GOMAXPROCS()用来设置可以并行计算的CPU核数最大值,并返回之前的值,具体使用方法上一篇有些,这里不再赘述Gosc
- 简介:倒计时秒杀组件在电商网站中层出不穷 不过思路万变不离其踪,我自己根据其他资料设计了一个vue版的核心思路:1、时间不能是本
- 如下所示:在组件上添加属性 this.$set(this.data,"obj",value');删除属性this
- python的库一般都用pip安装。但是有时候也会出现在线安装失败的情况,如下图安装PIL模块时报错:这时候可以采取离线安装的方式;一、首先
- Dapper的简介Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,
- Python安装过程,供大家参考,具体内容如下1.下载安装程序我们安装Python的一个重要目的是为了用IAR编译CC2640 OAD文件时
- JS调试技巧技巧一:格式化压缩代码 技巧二:快速跳转到某个断点的位置右侧的Breakpoints会汇总你在JS文件所有打过的断点,点击跟ch
- 1 界面美化1.1 Chinese (Simplified) | 简体中文1.2 vscode-icons | 美化图标1.3 Bracke
- //方法1:$ip = $_SERVER["REMOTE_ADDR"];echo $ip;//方法2:$user_IP
- 本文实例讲述了Python3.6实现连接mysql或mariadb的方法。分享给大家供大家参考,具体如下:python3.6的安装查看前面一
- 拆包是指将一个结构中的数据拆分为多个单独变量中。以元组为例:>>> a = ('windows', 10,
- <?php /** 默认首页 **/ class DefaultController extends AppController {
- 两种方法拼接#img = np.vstack((img, img2)) # vstack按垂直方向,hstack按水平方向img
- 如下所示:import torchfrom torch.autograd import Variableimport matplotlib.
- 我就废话不多说了,大家还是直接看代码吧~注释讲解版:# Classifier exampleimport numpy as np# for
- 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出。Python中内置有对字符串进行格式化的操作%。模
- 一个完整的域名,由根域、顶级域、二级、 * ……域名构成,每级域名之间用点分开,每级域名由字母、数字和减号构成(第一个字母不能是减号),不区分