Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

正确实现ImageMessage.ToJson() #138

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions examples/ImageMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ int main(int argc, char* argv[])
bot.Connect(opts);
cout << "Bot working..." << endl;

FriendImage img = bot.UploadFriendImage("D:\\test.png");
GroupImage gImg = bot.UploadGroupImage("D:\\test.png");
TempImage tImg = bot.UploadTempImage("D:\\test.png");
string ImagePath("E:/test.png");

bot.On<FriendMessage>(
[&](FriendMessage fm)
[&](FriendMessage m)
{
try
{
fm.Reply(MessageChain().Image(img));
FriendImage img = bot.UploadFriendImage(ImagePath);
m.Reply(MessageChain().Image(img));
}
catch (const std::exception& ex)
{
Expand All @@ -31,11 +30,12 @@ int main(int argc, char* argv[])
});

bot.On<GroupMessage>(
[&](GroupMessage gm)
[&](GroupMessage m)
{
try
{
bot.SendMessage(gm.Sender.Group.GID, MessageChain().Image(gImg));
GroupImage gImg = bot.UploadGroupImage(ImagePath);
bot.SendMessage(m.Sender.Group.GID, MessageChain().Image(gImg));
}
catch (const std::exception& ex)
{
Expand All @@ -44,11 +44,12 @@ int main(int argc, char* argv[])
});

bot.On<TempMessage>(
[&](TempMessage gm)
[&](TempMessage m)
{
try
{
gm.Reply(MessageChain().Image(tImg));
TempImage tImg = bot.UploadTempImage(ImagePath);
m.Reply(MessageChain().Image(tImg));
}
catch (const std::exception& ex)
{
Expand Down
24 changes: 18 additions & 6 deletions include/mirai/messages/ImageMessage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,26 @@ namespace Cyan
}
virtual json ToJson() const override
{
return
json result =
{
{ "type", GetType() },
{ "imageId", imageId_ },
{ "url", url_ },
{ "path", path_ },
{ "base64", base64_ }
{ "type", GetType() }
};
imageId_.empty()
? result["imageId"] = json(nullptr)
: result["imageId"] = imageId_;

url_.empty()
? result["url"] = json(nullptr)
: result["url"] = url_;

path_.empty()
? result["path"] = json(nullptr)
: result["path"] = path_;

base64_.empty()
? result["base64"] = json(nullptr)
: result["base64"] = base64_;
return result;
}
virtual ~ImageMessage() {}

Expand Down