feat: Support input_select messages on telegram (#5887)

- Adding interactive button support for telegram for outgoing and incoming messages. 


Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Chamath K.B. Attanayaka
2023-03-28 22:50:07 +05:30
committed by GitHub
co-authored by Sojan Jose
parent bc8e8f3bb5
commit 6002394fcf
5 changed files with 162 additions and 22 deletions
+31 -4
View File
@@ -8,11 +8,38 @@ RSpec.describe Channel::Telegram do
message = create(:message, message_type: :outgoing, content: 'test',
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
telegram_message_response = double
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
.with(
body: 'chat_id=123&text=test&reply_markup='
)
.to_return(
status: 200,
body: { result: { message_id: 'telegram_123' } }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_123')
end
it 'send message with reply_markup' do
message = create(
:message, message_type: :outgoing, content: 'test', content_type: 'input_select',
content_attributes: { 'items' => [{ 'title' => 'test', 'value' => 'test' }] },
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' })
)
stub_request(:post, "https://api.telegram.org/bot#{telegram_channel.bot_token}/sendMessage")
.with(
body: 'chat_id=123&text=test' \
'&reply_markup=%7B%22one_time_keyboard%22%3Atrue%2C%22inline_keyboard%22%3A%5B%5B%7B%22text%22%3A%22test%22%2C%22' \
'callback_data%22%3A%22test%22%7D%5D%5D%7D'
)
.to_return(
status: 200,
body: { result: { message_id: 'telegram_123' } }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
allow(telegram_message_response).to receive(:success?).and_return(true)
allow(telegram_message_response).to receive(:parsed_response).and_return({ 'result' => { 'message_id' => 'telegram_123' } })
allow(telegram_channel).to receive(:message_request).and_return(telegram_message_response)
expect(telegram_channel.send_message_on_telegram(message)).to eq('telegram_123')
end
end
@@ -232,5 +232,33 @@ describe Telegram::IncomingMessageService do
expect(telegram_channel.inbox.messages.first.attachments.first.file_type).to eq('location')
end
end
context 'when valid callbac_query params' do
it 'creates appropriate conversations, message and contacts' do
params = {
'update_id' => 2_342_342_343_242,
'callback_query' => {
'id' => '2342342309929423',
'from' => {
'id' => 5_171_248,
'is_bot' => false,
'first_name' => 'Sojan',
'last_name' => 'Jose',
'username' => 'sojan',
'language_code' => 'en',
'is_premium' => true
},
'message' => message_params,
'chat_instance' => '-89923842384923492',
'data' => 'Option 1'
}
}.with_indifferent_access
described_class.new(inbox: telegram_channel.inbox, params: params).perform
expect(telegram_channel.inbox.conversations.count).not_to eq(0)
expect(Contact.all.first.name).to eq('Sojan Jose')
expect(telegram_channel.inbox.messages.first.content).to eq('Option 1')
end
end
end
end