Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle messages without text or caption #1694

Merged
merged 1 commit into from
Sep 6, 2023
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
6 changes: 6 additions & 0 deletions app/adapters/telegram_adapter/inbound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,14 @@ def initialize_sender(telegram_message)
return sender
end

unless text
trigger(UNKNOWN_CONTRIBUTOR)
return nil
end

telegram_onboarding_token = text.delete_prefix('/start').strip.upcase
sender = Contributor.find_by(telegram_id: nil, telegram_onboarding_token: telegram_onboarding_token)

if sender
sender.username = username
sender.telegram_id = telegram_id
Expand Down
58 changes: 58 additions & 0 deletions spec/adapters/telegram_adapter/inbound_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@
describe '|message| block argument' do
subject { message }
it { should be_a(Message) }

describe 'given the payload does not have a caption nor text' do
let(:telegram_message) { telegram_message_from_spammer }

it { should be_nil }

describe 'when UNKNOWN_CONTRIBUTOR is registered' do
subject do
proc do |block|
adapter.on(TelegramAdapter::UNKNOWN_CONTRIBUTOR, &block)
adapter.consume(telegram_message)
end
end

it { should yield_control }
end
end
end

describe '|message|text' do
Expand Down Expand Up @@ -251,4 +268,45 @@
'width' => 460,
'height' => 460 }] }
end

let(:telegram_message_from_spammer) do
{
message_id: 25,
from: {
id: 5_521_147_309,
is_bot: false,
# rubocop:disable Layout/LineLength
first_name: "\u0423\u043c\u0438\u0440\u0430\u044e \u0438 \u0432\u0441\u0435 \u0440\u0430\u0434\u044b \u043a\u0430\u043a \u0438 \u044f)",
# rubocop:enable Layout/LineLength
username: 'Annazollo'
},
chat: {
id: -1_001_597_054_590,
title: "Milfa \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0447\u0430\u0442",
username: 'milfadanil',
type: 'supergroup'
},
date: 1_693_849_886,
new_chat_participant: {
id: 5_829_883_840,
is_bot: true,
first_name: 'Wir sind Potsdam',
username: 'MAZ_Potsdam_Bot'
},
new_chat_member: {
id: 5_829_883_840,
is_bot: true,
first_name: 'Wir sind Potsdam',
username: 'MAZ_Potsdam_Bot'
},
new_chat_members: [
{
id: 5_829_883_840,
is_bot: true,
first_name: 'Wir sind Potsdam',
username: 'MAZ_Potsdam_Bot'
}
]
}
end
end