python制作一个机器人
发布日期:2021-06-29 14:39:38
浏览次数:3
分类:技术文章
本文共 3716 字,大约阅读时间需要 12 分钟。
是否也像拥有自己的机器人呢?
不挨个展示了。 比如说你想实现一个夸人的功能:"""作者:川川时间:2021/4/6"""from nonebot.adapters.cqhttp import Message, PokeNotifyEvent,Botfrom nonebot import on_noticeimport warningsfrom nonebot.permission import *import requestswarnings.filterwarnings("ignore")from aiocqhttp.exceptions import Error as CQHttpErrorpoke = 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: passasync def kua(): url = 'https://chp.shadiao.app/api.php' resp = requests.get(url) return resp.text
或者你也想机器人发送美女图片:
"""作者:川川时间:2021/5/5"""from nonebot.adapters.cqhttp import Messagefrom nonebot import on_keyword,on_noticefrom nonebot.typing import T_Statefrom nonebot.adapters import Bot, Eventimport requests, refrom aiocqhttp.exceptions import Error as CQHttpErrorfrom nonebot.adapters.cqhttp import message,GroupMessageEvent,Message,MessageEventfrom nonebot.typing import T_Statefrom nonebot.permission import SUPERUSERyulu = 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: passasync 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 requireimport nonebotimport requestsscheduler = 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 nonebotfrom 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 functionnonebot.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")
当然还有一些配置文件源码,我觉得可能没有多的必要继续发,新手可能看不懂了。大概就
似乎群友玩得挺嗨! 如果你对python感兴趣,也想制作一个自己的机器人,那么欢迎你加入大家庭:970353786
,我会将完整的文件分享给大家。非爱好者就不要来,没有意义。 转载地址:https://chuanchuan.blog.csdn.net/article/details/117921934 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月13日 05时03分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JAVA 线程同步机制 synchronized
2019-04-29
MySQL 安装教程(无脑版)
2019-04-29
IDEA 怎么删除一个Module
2019-04-29
走进数据科学:最好是通过比网课更好的方法
2019-04-29
AI革命第一步:最容易被忽略但必不可少的物联网
2019-04-29
2020年开发运维工具清单:选择开发运维工具堆栈吧
2019-04-29
效率提升法则:高效人士不会去做的4件事
2019-04-29
8.PostgreSQL约束
2019-04-29
【技术分享】使用AES加密技术保障数据安全
2019-04-29
【应用实例】布线多?成本高?不可靠?泽耀方案没烦恼!
2019-04-29
数据可视化工具:Matplotlib绘图
2019-04-29
用Python写个超级小恐龙跑酷游戏,上班摸鱼我能玩一天
2019-04-29
闺蜜看我用Python画了一幅樱花图,吵着要我给他介绍程序员小哥哥
2019-04-29
【Python爬虫实战】知乎热榜数据采集,上班工作摸鱼两不误,知乎热门信息一网打尽
2019-04-29
Python抓取哔哩哔哩up主信息:只要爬虫学的好,牢饭吃的早
2019-04-29
有个码龄5年的程序员跟我说:“他连wifi从来不用密码”
2019-04-29
领导让我整理上个季度的销售额,幸好我会Python数据分析,你猜我几点下班
2019-04-29
【Python爬虫实战】为何如此痴迷Python?还不是因为爱看小姐姐图
2019-04-29
零基础自学Python,你也可以实现经济独立!
2019-04-29