百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
I

intercom-rails

> 编程语言
开源

在 Rails 应用中安装 Intercom 的最简单方法。

289 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

在 Rails 应用中安装 Intercom 的最简单方法。

IntercomRails

The easiest way to install Intercom in a rails app.

For interacting with the Intercom REST API, use the intercom gem (https://github.com/intercom/intercom-ruby)

Requires Ruby 2.0 or higher.

Installation

Add this to your Gemfile:

ruby
gem "intercom-rails"

Then run:

bundle install

Take note of your app_id from here and generate a config file:

rails generate intercom:config YOUR-APP-ID

To make installing Intercom easy, where possible a tag **will be automatically inserted before the closing tag**. For most Rails apps, you won't need to do any extra config. Having trouble? Check out troubleshooting below.

Live Chat

With the Intercom Messenger you can chat with users and visitors to your web site. Include the Intercom Messenger on every page by setting:

ruby
  config.include_for_logged_out_users = true

Disabling automatic insertion

To disable automatic insertion for a particular controller or action you can:

ruby
  skip_after_action :intercom_rails_auto_include

Troubleshooting

If things are not working make sure that:

  • You've generated a config file with your app_id as detailed above.
  • Your user object responds to an id or email method.
  • Your current user is accessible in your controllers as current_user or @user, if not in config/initializers/intercom.rb:
ruby
  config.user.current = Proc.new { current_user_object }

If your users can be defined in different ways in your app you can also pass an array as follows:

ruby
  config.user.current = [Proc.new { current_user_object }, Proc.new { @user_object }]
  • If you want the Intercom Messenger to be available when there is no current user, set config.include_for_logged_out_users = true in your config.

Feel free to mail us: [email protected], if you're still having trouble and we'll work with you to get you sorted.

Configuration

API Secret

It is possible to enable Identity Verification for the Intercom Messenger and you can find the documentation in how to do it here. We strongly encourage doing this as it makes your installation more secure! If you want to use this feature, ensure you set your Identity Verification Secret as the API secret in config/initializers/intercom.rb:

ruby
  config.api_secret = '123456'

Note: This example is just for the sake of simplicity, you should never include this secret in source control. Instead, you should use the Rails secret config feature.

JWT Authentication

You can enable JWT authentication for enhanced security with the Intercom Messenger. This feature uses JSON Web Tokens (JWTs) to authenticate users instead of the traditional user_hash method. To enable JWT authentication, add the following to your config/initializers/intercom.rb:

ruby
  config.jwt.enabled = true

JWT Expiry

You can set an expiry time for JWTs. This determines how long the token remains valid:

ruby
  config.jwt.expiry = 12.hours  # Token expires after 12 hours

If no expiry is set, the JWT will not include an expiration claim.

Signed User Fields

You can specify which user fields should be included in the JWT payload and removed from the client-side settings for enhanced security:

ruby
  config.jwt.signed_user_fields = [:email, :name, :plan, :team_id]

With this configuration, these fields will be:

  • Included in the signed JWT payload
  • Removed from the client-side intercomSettings object
  • Still available to Intercom through the secure JWT

Per-Request JWT Configuration

You can also configure JWT settings on a per-request basis using the intercom_script_tag helper:

erb
 current_user.id,
  :email => current_user.email
}, {
  :jwt_enabled => true,
  :jwt_expiry => 1.hour
}) %>

Important Notes:

  • JWT authentication requires an api_secret to be configured
  • JWT is only generated when a user_id is present
  • When JWT is enabled, the user_id is removed from client-side settings and only included in the secure JWT
  • Other configured signed fields are also removed from client-side settings when JWT is used

Shutdown

We make use of first-party cookies so that we can identify your users the next time they open your messenger. When people share devices with someone else, they might be able to see the most recently logged in user’s conversation history until the cookie expires. Because of this, it’s very important to properly shutdown Intercom when a user’s session on your app ends (either manually or due to an automated logout).

Using Devise

If you use devise, you can override (if not already done) the session_controller by replacing in your config/routes.rb file:

ruby
devise_for :users

with

ruby
devise_for :users, controllers: { sessions: "sessions" }

Then you can use the following code to prepare Intercom Shutdown on log out in your app/session_controller.rb

ruby
class SessionsController  Proc.new { |user| user.plan.name },
    :is_paid => Proc.new { |user| user.plan.present? },
    :email_verified => :email_verified?
  }
  # or If User::custom_data method returns a hash
  config.user.custom_data = Proc.new { |user| user.custom_data }

In some situations you'll want to set some custom data attribute specific to a request. You can do this using the intercom_custom_data helper available in your controllers:

ruby
class AppsController  Proc.new { |app| app.messages.count },
  :is_interesting => :is_interesting?
}

In some situations you'll want to set some custom company data attribute specific to a request. You can do this similarly to user data attribute set by using the intercom_custom_data helper available in your controllers:

ruby
class AppsController 

This will behave exactly the same as the default auto-install. If for whatever reason you can't use auto-install, you can also provide a hash of user data as the first argument:

erb

   'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at,
    :custom_data => {
      'plan' => current_user.plan.name
    }
  }) %>

You can also override IntercomRails::Config options such as your api_secret, or widget configuration with a second hash:

erb

   'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at
  }, {
    :secret => 'your-apps-api-secret',
    :widget => {:activator => '#Intercom'}
  }) %>

Content Security Policy Level 2 (CSP) support

As of version 0.2.30 this gem supports CSP, allowing you to whitelist the include code using both nonces and SHA-256 hashes.

Automatic Insertion

CSP support for automatic insertion exposes two namespaces that can be defined by the user via monkey patching:

  • String CoreExtensions::IntercomRails::AutoInclude.csp_nonce_hook(controller)
  • nil CoreExtensions::IntercomRails::AutoInclude.csp_sha256_hook(controller, SHA-256 whitelist entry)

For instance, a CSP nonce can be inserted using the Github Secure Headers gem with the following code:

ruby
module CoreExtensions
  module IntercomRails
    module AutoInclude
      def self.csp_nonce_hook(controller)
        SecureHeaders.content_security_policy_script_nonce(controller.request)
      end
    end
  end
end

or, for whitelisting the SHA-256 hash:

ruby
module CoreExtensions
  module IntercomRails
    module AutoInclude
      def self.csp_sha256_hook(controller, sha256)
        SecureHeaders.append_content_security_policy_directives(controller.request, {script_src: [sha256]})
      end
    end
  end
end

Manual Insertion

CSP is supported in manual insertion as well, the request nonce can be passed as an option:

erb

   'your-app-id',
    :user_id => current_user.id,
    :email => current_user.email,
    :name => current_user.name,
    :created_at => current_user.created_at
  }, {
    :secret => 'your-apps-api-secret',
    :widget => {:activator => '#Intercom'},
    :nonce => get_nonce_from_your_csp_framework
  }) %>

The SHA-256 hash is available using csp_sha256 just after generating the tag itself:

erb

Deleting your users

If you delete a user from your system, you should also delete them from Intercom lest they still receive messages.

You can do this using the intercom-ruby gem. In the example below we're using an ActiveJob to perform the delete in the background.

class User
  after_destroy { DeleteFromIntercomJob.perform_later(self) }
end

class DeleteFromIntercomJob < ApplicationJob
  def perform(user)
    intercom = Intercom::Client.new
    user = intercom.users.find(id: user.id)
    deleted_user = intercom.users.delete(user)
  end
end

Running tests/specs

specs should run on a clean clone of this repo, using the following commands. (developed against ruby 2.1.2 and 1.9.3)

bundle install
bundle exec rake spec
or
bundle exec rspec spec/

Pull Requests

  • Add tests! Your patch won't be accepted if it doesn't have tests.

  • Document any change in behaviour. Make sure the README and any other relevant documentation are kept up-to-date.

  • Create topic branches. Don't ask us to pull from your master branch.

  • One pull request per feature. If you want to do more than one thing, send multiple pull requests.

  • Send coherent history. Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before sending them to us.

Contributors

  • Dr Nic Williams (@drnic) - provided a rails generator for adding the Intercom javascript tag into your layout.
  • Alexander Chaychuk (@sashich) - fixed bug in user detection when users not persisted (e.g. new session view with devise).

License

intercom-rails is released under the MIT License.

Copyright

Copyright (c) 2011-2020 Intercom, Inc. All rights reserved.

Issues· 54 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言