处理通过 Twilio 在用户之间发送短信

handle sending sms between users over Twilio

我已阅读 THIS Twilio post 了解如何在用户之间启用短信。我在 Rails.

上使用 Ruby 构建

目前,这是我拥有的:

这很棒,因为我为用户提供了多种彼此之间的通信方式(在应用程序或电子邮件中)。

现在我想要一个类似的但不是使用 Twilio API.

我特别想启用这种类型的通信: 根据 Twilio help center

我无法理解拥有多个号码供不同用户使用的概念,但不知何故你知道消息是给谁的...(哇,我什至无法描述我所拥有的麻烦了!)

我正在使用 Ruby wrapper for the Twilio API

向用户发送消息非常简单:

# set up a client to talk to the Twilio REST API
@client = Twilio::REST::Client.new account_sid, auth_token

# send sms
@client.messages.create(
  from: 'twilio_number',
  to: 'a user number',
  body: 'Hey there!'
)

接收消息:

首先配置您的 Twilio phone 号码请求 url 以指向应用程序中的端点。在我的例子中是 'yourappurl.com/twilio/create'.

class TwilioController < ApplicationController

  include Webhookable

  after_filter :set_header
  skip_before_action :verify_authenticity_token

  def create
    # inbound messages arrive here. Need to do something with the message, identify who's it for
    # the receiving user and where its coming from ( the user who sent the text message)
    render_twiml response
  end

end

然而,这是我的应用程序向用户的 phone 号码发送消息。我如何处理两个用户之间的通信,其中用户 X 可以通过短信与用户 Y 通信(全部通过我的应用程序)

这里是 Twilio 开发人员布道者。

sending messages between users 上的帮助中心文章对如何在理论上实现这一点进行了很好的解释,但是您可能希望查看一些代码。

你最好的选择是完成 Masked numbers with Twilio 上的本教程,它实际上向你展示了如何为一个用户购买一个号码,然后通过该号码连接两个用户。

简而言之,您需要获得一个代表两个用户之间关系的数字。当您收到该号码的消息并从 Twilio 获取 webhook 时,您可以根据消息发送到的 Twilio 号码和发件人号码来查找关系。然后您可以连接并将消息发送到关系另一端的号码。

但是,正如我所说,我建议您通过 tutorial 真正深入地学习。

如果这有帮助,请告诉我。