bot_command.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "chat_helpers/bot_command.h"
  8. #include "data/data_channel.h"
  9. #include "data/data_chat.h"
  10. #include "data/data_peer.h"
  11. #include "data/data_user.h"
  12. #include "data/data_session.h"
  13. #include "history/history_item.h"
  14. namespace Bot {
  15. QString WrapCommandInChat(
  16. not_null<PeerData*> peer,
  17. const QString &command,
  18. const FullMsgId &context) {
  19. auto result = command;
  20. if (const auto item = peer->owner().message(context)) {
  21. if (const auto user = item->fromOriginal()->asUser()) {
  22. return WrapCommandInChat(peer, command, user);
  23. }
  24. }
  25. return result;
  26. }
  27. QString WrapCommandInChat(
  28. not_null<PeerData*> peer,
  29. const QString &command,
  30. not_null<UserData*> bot) {
  31. if (!bot->isBot() || bot->username().isEmpty()) {
  32. return command;
  33. }
  34. const auto botStatus = peer->isChat()
  35. ? peer->asChat()->botStatus
  36. : peer->isMegagroup()
  37. ? peer->asChannel()->mgInfo->botStatus
  38. : -1;
  39. return ((command.indexOf('@') < 2) && (botStatus == 0 || botStatus == 2))
  40. ? command + '@' + bot->username()
  41. : command;
  42. }
  43. } // namespace Bot