RoR Twilio短信应用程序 - 无法让Twilio发送短信。没有错误消息

时间:2016-06-26 21:13:40

标签: ruby-on-rails web-applications sms twilio twilio-api

更新:感谢大家的建议。我尝试了所有方法,但在我将凭据(我的API,令牌和电话号码)更新为生产凭据之前,没有任何工作。现在我可以收到短信,即使短信令人困惑地说他们是从试用帐户发送的。我很高兴应用程序正在运行,但是当测试凭据没有时,生产凭证的工作原理令我感到困惑。如果有人有想法,请告诉我。

我正在使用RoR创建一个使用Twilio API向手机号码发送短信的网络应用。因为我不知道我在做什么,所以我按照这里的说明操作: https://www.sitepoint.com/adding-sms-capabilities-to-your-rails-app/ 这是超级伟大的,但也写于4年前。我注意到Twilio使用了不同的资源URI(消息与短信/消息),并且我试图调整代码以反映此更新,但我认为我仍然遗漏了一些东西,因为我'我没有收到任何短信。是的,我使用的电话号码是100%经过验证的。 更令人困惑的是,我的控制台和Twilio SMS日志都没有给我任何错误消息。控制台输出看起来像这样:

(42.6ms)提交事务   渲染文本模板(0.0ms) 在710ms完成200 OK(浏览次数:0.4ms | ActiveRecord:42.9ms)

对我来说看起来很冷淡。并且在日志中甚至没有条目。这是我的控制器的样子:

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      render text: "Thank you! You will receive an SMS shortly with verification instructions."

      # Instantiate a Twilio client
      client = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])

      # Create and send an SMS message
      client.account.messages.create(
        from: TWILIO_CONFIG['from'],
        to: @user.phone,
        body: "Thanks for signing up. To verify your account, please reply HELLO to this message."
      )
    else
      render :new
    end
  end
end

具体来说,我尝试将client.account.messages.create()client.account.sms.messages.create()更改为无效。我也尝试将其更改为client.account.sms.messages.sendMessage(),但我收到错误,告诉我方法未定义。

无论如何,任何人都可以给我一个关于如何排除故障的提示吗?这是我改变的唯一一段代码,所以也许我必须改变别的东西?

提前致谢。 :)

P.S。为什么代码末尾的end看起来如此棘手?他们在Sublime看起来并不像那样。

4 个答案:

答案 0 :(得分:1)

如果您执行以下操作会发生什么:

pip install git+https://github.com/user/anotherrepo.git
--process-dependency-links --allow-all-external

然后,您可以删除控制器中定义的那个。我相信,你应该能够发送消息或看到返回的错误

答案 1 :(得分:1)

试试这个:

将其粘贴到您的控制器中

def twilio_client
    Twilio::REST::Client.new(ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'])
    end


    def send_otp
      twilio_client.messages.create(to: 'phone_number', from: 'twilio_phone_no', body: "temporary identification code when prompted." )
    end

根据您的twilio帐户从字段设置并设置为fild到客户端电话号码“+4563217892” 并调用控制器send_otp方法。

有关更多信息,请查看此链接:

http://rubyonrailsdocs.blogspot.com/2016/05/twilio-application-step-by-step-process.html

答案 2 :(得分:1)

基拉,

请查看有关您的更新的Why aren't my test credentials working?

使用正确的凭据,我建议您尝试更新的教程SMS notifications in Ruby and Rails,其中send_message函数如下所示:

def send_message(phone_number, alert_message, image_url)

  @twilio_number = ENV['TWILIO_NUMBER']
  @client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']

  message = @client.account.messages.create(
    :from => @twilio_number,
    :to => phone_number,
    :body => alert_message,
    # US phone numbers can make use of an image as well.
    # :media_url => image_url 
  )
  puts message.to
end

查看教程中的完整代码,让我知道它是否有帮助。

答案 3 :(得分:0)

我只是想跟进这个 - 我撰写了这个问题。事实证明,您无法使用Twilio的测试凭证以这种方式进行测试。如Twilio文档中所述,https://support.twilio.com/hc/en-us/articles/223183808-Why-aren-t-my-test-credentials-working-

虽然Twilio将为用户提供测试凭据以执行Twilio REST API的部分,但这些不能用于发送消息或拨打电话。这就是我使用生产凭证时我的应用程序工作的原因。我没有被要求使用这些生产凭证(我认为你必须得到几个免费赠品)。

对于记录,我在该教程中遵循的代码是正确的,除了原始问题中提到的弃用的.sms端点。这确实需要改变。