是否也像擁有自己的機器人呢?
不挨個展示了。
比如說你想實現(xiàn)一個夸人的功能:
"""
作者:川川
時間: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
或者你也想機器人發(fā)送美女圖片:
"""
作者:川川
時間: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
再或者你想要機器人每天定時給你發(fā)天氣預(yù)報:
"""
作者:川川
時間: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 = "風(fēng)向:" + data['result']['future'][0]['direct']
a = "時間:" + data['result']['future'][1]['date']
b = "溫度:" + data['result']['future'][1]['temperature']
c = "天氣:" + data['result']['future'][1]['weather']
g = "風(fēng)向:" + 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': '天氣預(yù)報:\n{}'.format(tu),
'user_id': '2835809579'
})
當(dāng)然也有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")
當(dāng)然還有一些配置文件源碼,我覺得可能沒有多的必要繼續(xù)發(fā),新手可能看不懂了。大概就
似乎群友玩得挺嗨!
到此這篇關(guān)于python制作機器人的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)python 機器人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- python基于itchat實現(xiàn)微信群消息同步機器人
- 教你用Python創(chuàng)建微信聊天機器人
- python操作微信自動發(fā)消息的實現(xiàn)(微信聊天機器人)
- Python實現(xiàn)企業(yè)微信機器人每天定時發(fā)消息實例
- 快速實現(xiàn)基于Python的微信聊天機器人示例代碼
- Python實現(xiàn)聊天機器人的示例代碼
- Python3從零開始搭建一個語音對話機器人的實現(xiàn)
- python實現(xiàn)機器人行走效果
- python 實現(xiàn)語音聊天機器人的示例代碼