Graia简单使用

安装

配置好mirai后
python后端安装模块

pip install graia-application-mirai
pip install graia-broadcast

更新

pip install graia-application-mirai --upgrade
pip install graia-broadcast --upgrade

使用

官方文档
基本架构

from graia.broadcast import Broadcast
from graia.application import GraiaMiraiApplication, Session
from graia.application.message.chain import MessageChain
import asyncio

from graia.application.message.elements.internal import Plain,Image,Face,Quote,At,Source
from graia.application.friend import Friend
from graia.application.group import Group,Membe

loop = asyncio.get_event_loop()

bcc = Broadcast(loop=loop)
app = GraiaMiraiApplication(
    broadcast=bcc,
    connect_info=Session(
        host="http://localhost:8080", # 填入 httpapi 服务运行的地址
        authKey="graia-mirai-api-http-authkey", # 填入 authKey
        account=5234120587, # 你的机器人的 qq 号
        websocket=True # Graia 已经可以根据所配置的消息接收的方式来保证消息接收部分的正常运作.
    )
)

@bcc.receiver("FriendMessage")
async def friend_message_listener(app: GraiaMiraiApplication, friend: Friend, message: MessageChain):

    await app.sendFriendMessage(friend, MessageChain.join(MessageChain.create([Plain('get message:')]), message.asSendable())  
# asSendable 方法将MessageChain对象转为可发送的状态
# join方法将两个MessageChain对象连接起来
@bcc.receiver("GroupMessage")
async def groupMessage(app: GraiaMiraiApplication, group: Group, member: Member, message: MessageChain):
	text = message[Plain]  # 返回一个列表
	if text:
	text = text[0].text

@bcc.receiver('TempMessage')
async def TempMessage(app: GraiaMiraiApplication, group: Group, member: Member, message: MessageChain):
   await app.sendTempMessage(group,member,MessageChain.join(MessageChain.create([Plain('get message:')]), message.asSendable())

app.launch_blocking()
获取MessageChain中的文字
@bcc.receiver("FriendMessage")
async def friend_message_listener(app: GraiaMiraiApplication, friend: Friend, message: MessageChain):
	text = message[Plain]  # 返回一个列表
	if text:
	text = text[0].text

	# 或使用 message.asDisplay() 将消息转为字符
     
将消息链转为字符,将字符转为消息链
@bcc.receiver("GroupMessage")
async def groupMessage(app: GraiaMiraiApplication, group: Group, member: Member, message: MessageChain):
	message_str = message.asSerializationString()  # 将MessageChain对象序列化
	message = MessageChain.fromSerializationString(message_str) # 将字符消息反序列化
在消息中添加At某人
from graia.application.message.elements.internal import Plain,Image,Face,Quote,At,Source

@bcc.receiver("GroupMessage")
async def groupMessage(app: GraiaMiraiApplication, group: Group, member: Member, message: MessageChain):
	app.sendGroupMessage(group, MessageChain.create([Plain('xxx'), At(111111111)]))

你可能感兴趣的:(Graia简单使用)