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

Commit

Permalink
Merge pull request #138 from cyanray/dev/cyanray
Browse files Browse the repository at this point in the history
正确实现ImageMessage.ToJson()
  • Loading branch information
cyanray authored Feb 28, 2022
2 parents b4a557e + 1fb4bb9 commit 85eb9cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
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

0 comments on commit 85eb9cd

Please sign in to comment.