forked from chatwoot/chatwoot
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Baileys-like option for setting up a WhatsApp inbox (#2)
* draft: scaffold evo channel as api channel * feat: add evolution to whatsapp channel
- Loading branch information
1 parent
d349a2a
commit 59fd3d9
Showing
10 changed files
with
304 additions
and
2 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
app/controllers/api/v1/accounts/channels/evolution_channels_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
class Api::V1::Accounts::Channels::EvolutionChannelsController < Api::V1::Accounts::BaseController | ||
include Api::V1::InboxesHelper | ||
before_action :authorize_request | ||
before_action :set_user | ||
|
||
def create | ||
ActiveRecord::Base.transaction do | ||
channel = create_channel | ||
@inbox = Current.account.inboxes.build( | ||
{ | ||
name: inbox_name(channel), | ||
channel: channel | ||
}.merge( | ||
permitted_params.except(:channel) | ||
) | ||
) | ||
|
||
params = permitted_params(channel_type_from_params::EDITABLE_ATTRS)[:channel].except(:type) | ||
Evolution::ManagerService.new.create(@inbox.account_id, permitted_params[:name], params[:webhook_url], | ||
params[:api_key], @user.access_token.token) | ||
@inbox.save! | ||
end | ||
|
||
render json: @inbox, status: :created | ||
rescue StandardError => e | ||
render json: { error: e.message }, status: :unprocessable_entity | ||
end | ||
|
||
private | ||
|
||
def authorize_request | ||
authorize ::Inbox | ||
end | ||
|
||
def set_user | ||
@user = current_user | ||
end | ||
|
||
def create_channel | ||
return unless %w[api whatsapp].include?(permitted_params[:channel][:type]) | ||
|
||
params = permitted_params(channel_type_from_params::EDITABLE_ATTRS)[:channel].except(:type, :api_key) | ||
params[:webhook_url] = "#{params[:webhook_url]}/chatwoot/webhook/#{permitted_params[:name]}" | ||
account_channels_method.create!(params) | ||
end | ||
|
||
def inbox_attributes | ||
[:name] | ||
end | ||
|
||
def permitted_params(channel_attributes = []) | ||
# We will remove this line after fixing https://linear.app/chatwoot/issue/CW-1567/null-value-passed-as-null-string-to-backend | ||
params.each { |k, v| params[k] = params[k] == 'null' ? nil : v } | ||
|
||
params.permit( | ||
*inbox_attributes, | ||
channel: [:type, :api_key, *channel_attributes] | ||
) | ||
end | ||
|
||
def channel_type_from_params | ||
{ | ||
'api' => Channel::Api, | ||
'whatsapp' => Channel::Whatsapp | ||
}[permitted_params[:channel][:type]] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ApiClient from '../ApiClient'; | ||
|
||
class EvolutionChannel extends ApiClient { | ||
constructor() { | ||
super('channels/evolution_channel', { accountScoped: true }); | ||
} | ||
} | ||
|
||
export default new EvolutionChannel(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
app/javascript/dashboard/routes/dashboard/settings/inbox/channels/Evolution.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<template> | ||
<form class="mx-0 flex flex-wrap" @submit.prevent="createChannel()"> | ||
<div class="w-[65%] flex-shrink-0 flex-grow-0 max-w-[65%]"> | ||
<label :class="{ error: $v.channelName.$error }"> | ||
{{ $t('INBOX_MGMT.ADD.WHATSAPP.INBOX_NAME.LABEL') }} | ||
<input | ||
v-model.trim="channelName" | ||
type="text" | ||
:placeholder="$t('INBOX_MGMT.ADD.WHATSAPP.INBOX_NAME.PLACEHOLDER')" | ||
@blur="$v.channelName.$touch" | ||
/> | ||
<span v-if="$v.channelName.$error" class="message">{{ | ||
$t('INBOX_MGMT.ADD.WHATSAPP.INBOX_NAME.ERROR') | ||
}}</span> | ||
</label> | ||
</div> | ||
|
||
<div class="w-[65%] flex-shrink-0 flex-grow-0 max-w-[65%]"> | ||
<label :class="{ error: $v.webhookUrl.$error }"> | ||
{{ $t('INBOX_MGMT.ADD.EVOLUTION.WEBHOOK_URL.LABEL') }} | ||
<input | ||
v-model.trim="webhookUrl" | ||
type="text" | ||
:placeholder="$t('INBOX_MGMT.ADD.EVOLUTION.WEBHOOK_URL.PLACEHOLDER')" | ||
@blur="$v.webhookUrl.$touch" | ||
/> | ||
<span v-if="$v.webhookUrl.$error" class="message">{{ | ||
$t('INBOX_MGMT.ADD.EVOLUTION.WEBHOOK_URL.ERROR') | ||
}}</span> | ||
</label> | ||
<p class="help-text"> | ||
{{ $t('INBOX_MGMT.ADD.EVOLUTION.WEBHOOK_URL.SUBTITLE') }} | ||
</p> | ||
</div> | ||
|
||
<div class="w-[65%] flex-shrink-0 flex-grow-0 max-w-[65%]"> | ||
<label :class="{ error: $v.apiKey.$error }"> | ||
{{ $t('INBOX_MGMT.ADD.EVOLUTION.API_KEY.LABEL') }} | ||
<input | ||
v-model.trim="apiKey" | ||
type="text" | ||
:placeholder="$t('INBOX_MGMT.ADD.EVOLUTION.API_KEY.PLACEHOLDER')" | ||
@blur="$v.apiKey.$touch" | ||
/> | ||
<span v-if="$v.apiKey.$error" class="message">{{ | ||
$t('INBOX_MGMT.ADD.WHATSAPP.API_KEY.ERROR') | ||
}}</span> | ||
</label> | ||
<p class="help-text"> | ||
{{ $t('INBOX_MGMT.ADD.EVOLUTION.API_KEY.SUBTITLE') }} | ||
</p> | ||
</div> | ||
|
||
<div class="w-full"> | ||
<woot-submit-button | ||
:loading="uiFlags.isCreating" | ||
:button-text="$t('INBOX_MGMT.ADD.WHATSAPP.SUBMIT_BUTTON')" | ||
/> | ||
</div> | ||
</form> | ||
</template> | ||
|
||
<script> | ||
import { mapGetters } from 'vuex'; | ||
import alertMixin from 'shared/mixins/alertMixin'; | ||
import { required } from 'vuelidate/lib/validators'; | ||
import router from '../../../../index'; | ||
const shouldBeWebhookUrl = (value = '') => | ||
value ? value.startsWith('http') : true; | ||
const shouldContainOnlyValidChars = (value = '') => | ||
value ? /^[a-zA-Z0-9_]+$/.test(value) : true; | ||
export default { | ||
mixins: [alertMixin], | ||
data() { | ||
return { | ||
channelName: '', | ||
webhookUrl: '', | ||
apiKey: '', | ||
}; | ||
}, | ||
computed: { | ||
...mapGetters({ | ||
uiFlags: 'inboxes/getUIFlags', | ||
}), | ||
}, | ||
validations: { | ||
channelName: { shouldContainOnlyValidChars }, | ||
webhookUrl: { shouldBeWebhookUrl }, | ||
apiKey: { required }, | ||
}, | ||
methods: { | ||
async createChannel() { | ||
this.$v.$touch(); | ||
if (this.$v.$invalid) { | ||
return; | ||
} | ||
try { | ||
const apiChannel = await this.$store.dispatch( | ||
'inboxes/createEvolutionChannel', | ||
{ | ||
name: this.channelName, | ||
channel: { | ||
type: 'api', | ||
webhook_url: this.webhookUrl, | ||
api_key: this.apiKey, | ||
}, | ||
}); | ||
router.replace({ | ||
name: 'settings_inboxes_add_agents', | ||
params: { | ||
page: 'new', | ||
inbox_id: apiChannel.id, | ||
}, | ||
}); | ||
} catch (error) { | ||
this.showAlert(this.$t('INBOX_MGMT.ADD.WHATSAPP.API.ERROR_MESSAGE')); | ||
} | ||
}, | ||
}, | ||
}; | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Evolution::ManagerService | ||
def api_headers(api_key) | ||
{ 'apikey' => api_key, 'Content-Type' => 'application/json' } | ||
end | ||
|
||
def create(account_id, name, webhook_url, api_key, access_token) | ||
response = HTTParty.post( | ||
"#{webhook_url}/instance/create", | ||
headers: api_headers(api_key), | ||
body: { | ||
instanceName: name, | ||
token: SecureRandom.uuid, | ||
qrcode: true, | ||
# number: phone_number, | ||
auto_create: false, | ||
chatwoot_name_inbox: name, | ||
chatwoot_account_id: account_id, | ||
chatwoot_token: access_token, | ||
chatwoot_url: ENV.fetch('FRONTEND_URL', 'http://localhost:3000'), | ||
chatwoot_sign_msg: true, | ||
chatwoot_reopen_conversation: true, | ||
chatwoot_conversation_pending: false, | ||
chatwoot_import_messages: false, | ||
chatwoot_import_contacts: false, | ||
chatwoot_merge_brazil_contacts: true, | ||
chatwoot_days_limit_import_messages: 0 | ||
}.to_json | ||
) | ||
|
||
process_response(response) | ||
end | ||
|
||
def process_response(response) | ||
if response.success? | ||
Rails.logger.info("HTTP Request Successful: #{response}") | ||
else | ||
Rails.logger.error response.body | ||
nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters