python制作机器人的实现方法
作者:川川菜鸟 时间:2022-11-09 21:09:05
是否也像拥有自己的机器人呢?
不挨个展示了。
比如说你想实现一个夸人的功能:
"""
作者:川川
时间: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
标签:python,机器人
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
学会这个炫酷图表利器pyecharts,还怕不被公司重用?
2023-01-16 23:33:39
![](https://img.aspxhome.com/file/2023/4/124234_0s.png)
Python 字符串、列表、元组的截取与切片操作示例
2021-12-10 09:47:34
Python实现照片卡通化
2021-03-29 18:45:40
![](https://img.aspxhome.com/file/2023/2/86552_0s.jpg)
Python实现炸金花游戏的示例代码
2022-01-15 05:24:17
Python及PyCharm下载与安装教程
2022-10-04 08:19:54
![](https://img.aspxhome.com/file/2023/3/90433_0s.png)
Python wxPython库使用wx.ListBox创建列表框示例
2021-12-06 21:10:56
![](https://img.aspxhome.com/file/2023/5/95745_0s.png)
Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法
2023-07-14 14:37:00
Python 的类、继承和多态详解
2021-05-29 17:31:51
Oracle 安装和卸载问题收集(集合篇)第1/6页
2024-01-22 02:31:33
SQL Server忘记sa账号密码重新添加新管理账号
2024-01-18 09:59:44
![](https://img.aspxhome.com/file/2023/8/92728_0s.png)
mysql一次将多条不同sql查询结果并封装到一个结果集的实现方法
2024-01-19 23:30:05
![](https://img.aspxhome.com/file/2023/2/72442_0s.png)
如何使用Python对Excel表格进行拼接合并
2021-03-15 18:01:54
![](https://img.aspxhome.com/file/2023/9/76039_0s.png)
CentOS6.8使用cmake安装MySQL5.7.18
2024-01-27 06:00:06
python基于xml parse实现解析cdatasection数据
2023-08-18 13:27:36
Scrapy实现模拟登录的示例代码
2023-07-13 21:53:11
![](https://img.aspxhome.com/file/2023/1/131041_0s.png)
Python数值方法及数据可视化
2021-07-26 17:32:51
![](https://img.aspxhome.com/file/2023/9/66789_0s.png)
js实现文字列表无缝滚动效果
2024-04-19 10:16:12
Dreamweaver4使用技巧之--为你的站点定做颜色
2010-09-05 21:12:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723112948340s.gif)
JQuery 选项卡效果(JS与HTML的分离)
2024-04-19 10:43:05
go语言reflect.Type 和 reflect.Value 应用示例详解
2023-07-18 11:28:43