python制作機器人的實現(xiàn)方法
是否也像擁有自己的機器人呢?




不挨個展示了。
比如說你想實現(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 = "風向:" + 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': '天氣預(yù)報:\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")
當然還有一些配置文件源碼,我覺得可能沒有多的必要繼續(xù)發(fā),新手可能看不懂了。大概就
似乎群友玩得挺嗨!

到此這篇關(guān)于python制作機器人的實現(xiàn)方法的文章就介紹到這了,更多相關(guān)python 機器人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在pycharm上mongodb配置及可視化設(shè)置方法
今天小編就為大家分享一篇在pycharm上mongodb配置及可視化設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
Python基礎(chǔ)教程之Matplotlib圖形繪制詳解
Matplotlib是一個廣泛使用的數(shù)據(jù)可視化庫,提供了豐富的繪圖功能,用于創(chuàng)建各種類型的靜態(tài)、動態(tài)和交互式圖形,本文將通過多個例子給大家詳細介紹一下Python的Matplotlib圖形繪制,需要的朋友可以參考下2023-07-07
如何解決MNIST數(shù)據(jù)集下載速度較慢并失敗的問題
這篇文章主要介紹了如何解決MNIST數(shù)據(jù)集下載速度較慢并失敗的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
Python使用MongoDB運算符進行數(shù)據(jù)查詢詳解
MongoDB 是一個非關(guān)系型數(shù)據(jù)庫,具有靈活的數(shù)據(jù)模型和豐富的查詢功能,本文將介紹在 Python 中使用 MongoDB 運算符進行數(shù)據(jù)查詢的常用方法,需要的可以參考下2024-04-04
Python使用random.shuffle()隨機打亂字典排序
本文主要介紹了Python使用random.shuffle()隨機打亂字典排序,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08
解決python3 整數(shù)數(shù)組轉(zhuǎn)bytes的效率問題
這篇文章主要介紹了解決python3 整數(shù)數(shù)組轉(zhuǎn)bytes的效率問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03

