feat: Telegram Channel (#2901)
- Ability to configure telegram bots as a channel in chatwoot - Receive a message sent to the telegram bot in chatwoot - Ability to reply to telegram users from chatwoot - Receive attachment messages in chatwoot fixes: #1843
This commit is contained in:
@@ -255,16 +255,6 @@ RSpec.describe 'Inboxes API', type: :request do
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:valid_params) { { name: 'test', channel: { type: 'web_widget', website_url: 'test.com' } } }
|
||||
|
||||
it 'creates inbox' do
|
||||
post "/api/v1/accounts/#{account.id}/inboxes",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: valid_params,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include('test.com')
|
||||
end
|
||||
|
||||
it 'will not create inbox for agent' do
|
||||
agent = create(:user, account: account, role: :agent)
|
||||
|
||||
@@ -275,6 +265,26 @@ RSpec.describe 'Inboxes API', type: :request do
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
|
||||
it 'creates a webwidget inbox when administrator' do
|
||||
post "/api/v1/accounts/#{account.id}/inboxes",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: valid_params,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include('test.com')
|
||||
end
|
||||
|
||||
it 'creates a email inbox when administrator' do
|
||||
post "/api/v1/accounts/#{account.id}/inboxes",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { name: 'test', channel: { type: 'email', email: 'test@test.com' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.body).to include('test@test.com')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -314,6 +324,34 @@ RSpec.describe 'Inboxes API', type: :request do
|
||||
expect(inbox.reload.enable_auto_assignment).to be_falsey
|
||||
end
|
||||
|
||||
it 'updates api inbox when administrator' do
|
||||
api_channel = create(:channel_api, account: account)
|
||||
api_inbox = create(:inbox, channel: api_channel, account: account)
|
||||
|
||||
patch "/api/v1/accounts/#{account.id}/inboxes/#{api_inbox.id}",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { enable_auto_assignment: false, channel: { webhook_url: 'webhook.test' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(api_inbox.reload.enable_auto_assignment).to be_falsey
|
||||
expect(api_channel.reload.webhook_url).to eq('webhook.test')
|
||||
end
|
||||
|
||||
it 'updates email inbox when administrator' do
|
||||
email_channel = create(:channel_email, account: account)
|
||||
email_inbox = create(:inbox, channel: email_channel, account: account)
|
||||
|
||||
patch "/api/v1/accounts/#{account.id}/inboxes/#{email_inbox.id}",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { enable_auto_assignment: false, channel: { email: 'emailtest@email.test' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(email_inbox.reload.enable_auto_assignment).to be_falsey
|
||||
expect(email_channel.reload.email).to eq('emailtest@email.test')
|
||||
end
|
||||
|
||||
it 'updates avatar when administrator' do
|
||||
# no avatar before upload
|
||||
expect(inbox.avatar.attached?).to eq(false)
|
||||
|
||||
12
spec/controllers/webhooks/telegram_controller_spec.rb
Normal file
12
spec/controllers/webhooks/telegram_controller_spec.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Webhooks::TelegramController', type: :request do
|
||||
describe 'POST /webhooks/telegram/{:bot_token}' do
|
||||
it 'call the telegram events job with the params' do
|
||||
allow(Webhooks::TelegramEventsJob).to receive(:perform_later)
|
||||
expect(Webhooks::TelegramEventsJob).to receive(:perform_later)
|
||||
post '/webhooks/telegram/random_bot_token', params: { content: 'hello' }
|
||||
expect(response).to have_http_status(:success)
|
||||
end
|
||||
end
|
||||
end
|
||||
16
spec/factories/channel/channel_telegram.rb
Normal file
16
spec/factories/channel/channel_telegram.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
FactoryBot.define do
|
||||
factory :channel_telegram, class: 'Channel::Telegram' do
|
||||
bot_token { '2324234324' }
|
||||
account
|
||||
|
||||
before(:create) do |channel_telegram|
|
||||
# we are skipping some of the validation methods
|
||||
channel_telegram.define_singleton_method(:ensure_valid_bot_token) { return }
|
||||
channel_telegram.define_singleton_method(:setup_telegram_webhook) { return }
|
||||
end
|
||||
|
||||
after(:create) do |channel_telegram|
|
||||
create(:inbox, channel: channel_telegram, account: channel_telegram.account)
|
||||
end
|
||||
end
|
||||
end
|
||||
59
spec/jobs/send_reply_job_spec.rb
Normal file
59
spec/jobs/send_reply_job_spec.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe SendReplyJob, type: :job do
|
||||
subject(:job) { described_class.perform_later(message) }
|
||||
|
||||
let(:message) { create(:message) }
|
||||
|
||||
it 'enqueues the job' do
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.with(message)
|
||||
.on_queue('high')
|
||||
end
|
||||
|
||||
context 'when the job is triggered on a new message' do
|
||||
let(:process_service) { double }
|
||||
|
||||
before do
|
||||
allow(process_service).to receive(:perform)
|
||||
end
|
||||
|
||||
it 'calls Facebook::SendOnFacebookService when its facebook message' do
|
||||
facebook_channel = create(:channel_facebook_page)
|
||||
facebook_inbox = create(:inbox, channel: facebook_channel)
|
||||
message = create(:message, conversation: create(:conversation, inbox: facebook_inbox))
|
||||
allow(Facebook::SendOnFacebookService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(Facebook::SendOnFacebookService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
|
||||
it 'calls ::Twitter::SendOnTwitterService when its twitter message' do
|
||||
twitter_channel = create(:channel_twitter_profile)
|
||||
twitter_inbox = create(:inbox, channel: twitter_channel)
|
||||
message = create(:message, conversation: create(:conversation, inbox: twitter_inbox))
|
||||
allow(::Twitter::SendOnTwitterService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(::Twitter::SendOnTwitterService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
|
||||
it 'calls ::Twilio::SendOnTwilioService when its twilio message' do
|
||||
twilio_channel = create(:channel_twilio_sms)
|
||||
message = create(:message, conversation: create(:conversation, inbox: twilio_channel.inbox))
|
||||
allow(::Twilio::SendOnTwilioService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(::Twilio::SendOnTwilioService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
|
||||
it 'calls ::Telegram::SendOnTelegramService when its telegram message' do
|
||||
telegram_channel = create(:channel_telegram)
|
||||
message = create(:message, conversation: create(:conversation, inbox: telegram_channel.inbox))
|
||||
allow(::Telegram::SendOnTelegramService).to receive(:new).with(message: message).and_return(process_service)
|
||||
expect(::Telegram::SendOnTelegramService).to receive(:new).with(message: message)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(message.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
36
spec/jobs/webhooks/telegram_events_job_spec.rb
Normal file
36
spec/jobs/webhooks/telegram_events_job_spec.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Webhooks::TelegramEventsJob, type: :job do
|
||||
subject(:job) { described_class.perform_later(params) }
|
||||
|
||||
let!(:telegram_channel) { create(:channel_telegram) }
|
||||
let!(:params) { { bot_token: telegram_channel.bot_token, 'telegram' => { test: 'test' } } }
|
||||
|
||||
it 'enqueues the job' do
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.with(params)
|
||||
.on_queue('default')
|
||||
end
|
||||
|
||||
context 'when invalid params' do
|
||||
it 'returns nil when no bot_token' do
|
||||
expect(described_class.perform_now({})).to be_nil
|
||||
end
|
||||
|
||||
it 'returns nil when invalid bot_token' do
|
||||
expect(described_class.perform_now({ bot_token: 'invalid' })).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when valid params' do
|
||||
it 'calls Telegram::IncomingMessageService' do
|
||||
process_service = double
|
||||
allow(Telegram::IncomingMessageService).to receive(:new).and_return(process_service)
|
||||
allow(process_service).to receive(:perform)
|
||||
expect(Telegram::IncomingMessageService).to receive(:new).with(inbox: telegram_channel.inbox,
|
||||
params: params['telegram'].with_indifferent_access)
|
||||
expect(process_service).to receive(:perform)
|
||||
described_class.perform_now(params)
|
||||
end
|
||||
end
|
||||
end
|
||||
27
spec/services/telegram/incoming_message_service_spec.rb
Normal file
27
spec/services/telegram/incoming_message_service_spec.rb
Normal file
@@ -0,0 +1,27 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Telegram::IncomingMessageService do
|
||||
let!(:telegram_channel) { create(:channel_telegram) }
|
||||
|
||||
describe '#perform' do
|
||||
context 'when valid text message params' do
|
||||
it 'creates appropriate conversations, message and contacts' do
|
||||
params = {
|
||||
'update_id' => 2_342_342_343_242,
|
||||
'message' => {
|
||||
'message_id' => 1,
|
||||
'from' => {
|
||||
'id' => 23, 'is_bot' => false, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'language_code' => 'en'
|
||||
},
|
||||
'chat' => { 'id' => 23, 'first_name' => 'Sojan', 'last_name' => 'Jose', 'username' => 'sojan', 'type' => 'private' },
|
||||
'date' => 1_631_132_077, 'text' => 'test'
|
||||
}
|
||||
}.with_indifferent_access
|
||||
described_class.new(inbox: telegram_channel.inbox, params: params).perform
|
||||
expect(telegram_channel.inbox.conversations).not_to eq(0)
|
||||
expect(Contact.all.first.name).to eq('Sojan Jose')
|
||||
expect(telegram_channel.inbox.messages.first.content).to eq('test')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
19
spec/services/telegram/send_on_telegram_service_spec.rb
Normal file
19
spec/services/telegram/send_on_telegram_service_spec.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
require 'rails_helper'
|
||||
|
||||
describe Telegram::SendOnTelegramService do
|
||||
describe '#perform' do
|
||||
context 'when a valid message' do
|
||||
it 'calls channel.send_message_on_telegram' do
|
||||
telegram_request = double
|
||||
telegram_channel = create(:channel_telegram)
|
||||
message = create(:message, message_type: :outgoing, content: 'test',
|
||||
conversation: create(:conversation, inbox: telegram_channel.inbox, additional_attributes: { 'chat_id' => '123' }))
|
||||
allow(HTTParty).to receive(:post).and_return(telegram_request)
|
||||
allow(telegram_request).to receive(:success?).and_return(true)
|
||||
allow(telegram_request).to receive(:parsed_response).and_return({ 'result' => { 'message_id' => 'telegram_123' } })
|
||||
described_class.new(message: message).perform
|
||||
expect(message.source_id).to eq('telegram_123')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user