您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么用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")
當然還有一些配置文件源碼,我覺得可能沒有多的必要繼續發,新手可能看不懂了。大概就
似乎群友玩得挺嗨!
“怎么用python制作機器人”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。