atipugin / telegram-bot-ruby

Ruby wrapper for Telegram's Bot API
https://core.telegram.org/bots/api
Do What The F*ck You Want To Public License
1.35k stars 217 forks source link

Support for ChatMember types #278

Closed marat93 closed 1 year ago

marat93 commented 1 year ago

Maybe I'm doing something wrong but old_chat_member and new_chat_member fields in ChatMemberUpdated are always cast into just ChatMember regardless of the status value.

So that's what I get by calling bot.api.getUpdates when a user is added to the group:

{
  "ok" => true, "result" => [{
    "update_id" => 669365017,
    "chat_member" => {
      "chat" => {
        "id" => -1001944500528, "title" => "Test group", "type" => "supergroup"
      }, "from" => {
        "id" => 60234266, "is_bot" => false, "first_name" => "Marat", "username" => "username", "language_code" => "en"
      }, "date" => 1688893164, "old_chat_member" => {
        "user" => {
          "id" => 5469162229, "is_bot" => false, "first_name" => "Anton", "last_name" => "Chigurh"
        }, "status" => "left"
      }, "new_chat_member" => {
        "user" => {
          "id" => 5469162229, "is_bot" => false, "first_name" => "Anton", "last_name" => "Chigurh"
        }, "status" => "member"
      }
    }
  }]
}

And that's what I get from bot.listen:

#<Telegram::Bot::Types::ChatMemberUpdated
    chat=#<Telegram::Bot::Types::Chat ...
    from=#<Telegram::Bot::Types::User...
    date=1688893164
    old_chat_member=#<Telegram::Bot::Types::ChatMember>
    new_chat_member=#<Telegram::Bot::Types::ChatMember>
    invite_link=nil>

I expect old_chat_member and new_chat_member to be ChatMemberLeft and ChatMemberMember objects respectively.

atipugin commented 1 year ago

Hey @marat93

I agree but according to the official docs both old_chat_member and new_chat_member should be ChatMember. I'm not sure why; maybe they expect to return more than just ChatMemberLeft and ChatMemberMember in future.

marat93 commented 1 year ago

Thanks, @atipugin

if anyone needs it, I end up overriding the ChatMember adding all the attributes chat member types have. I looked up this solution in telegram-bot-rb

# frozen_string_literal: true

module Telegram
  module Bot
    module Types
      class ChatMember < Base
        attribute :user, User
        attribute :status, Types::String
        attribute? :custom_title, Types::String
        attribute? :is_anonymous, Types::Bool
        attribute? :can_be_edited, Types::Bool
        attribute? :can_manage_chat, Types::Bool
        attribute? :can_post_messages, Types::Bool
        attribute? :can_edit_messages, Types::Bool
        attribute? :can_delete_messages, Types::Bool
        attribute? :can_manage_voice_chats, Types::Bool
        attribute? :can_restrict_members, Types::Bool
        attribute? :can_promote_members, Types::Bool
        attribute? :can_change_info, Types::Bool
        attribute? :can_invite_users, Types::Bool
        attribute? :can_pin_messages, Types::Bool
        attribute? :is_member, Types::Bool
        attribute? :can_send_messages, Types::Bool
        attribute? :can_send_media_messages, Types::Bool
        attribute? :can_send_polls, Types::Bool
        attribute? :can_send_other_messages, Types::Bool
        attribute? :can_add_web_page_previews, Types::Bool
        attribute? :until_date, Types::Integer
      end
    end
  end
end
AlexWayfer commented 1 year ago

@marat93 I've fixed it in #285 now.

It's a huge PR now, yeah. With global logic change and a lot of small API updates.

Specific ChatMember changes are here: https://github.com/atipugin/telegram-bot-ruby/pull/285/commits/19baae0b7038089bf725eb73c0159a3d5fe3ff8d

AlexWayfer commented 1 year ago

@atipugin you can close this issue now, it accidentally has not been auto-closed by PR.