【问题标题】:RoR Twilio Texting App - Cannot get Twilio to send SMS. No error messageRoR Twilio 短信应用程序 - 无法让 Twilio 发送短信。没有错误信息
【发布时间】:2016-06-26 21:13:40
【问题描述】:

更新:感谢大家的建议。我尝试了所有方法,但在我将我的凭据(我的 API、令牌和电话号码)更新为生产凭据之前,没有任何效果。现在我可以接收短信,即使短信令人困惑地表明它们是从试用帐户发送的。我很高兴该应用程序可以正常工作,但对为什么生产凭据有效而测试凭据不起作用感到困惑。如果有人有想法,请告诉我。

我正在使用 RoR 创建一个使用 Twilio API 将文本消息发送到电话号码的 Web 应用程序。因为我不知道自己在做什么,所以我按照这里的说明进行操作: https://www.sitepoint.com/adding-sms-capabilities-to-your-rails-app/ 超级棒,但也是4年前写的。我注意到 Twilio 使用了不同的资源 URI(消息与 SMS/消息),并且我尝试调整代码以反映此更新,但我认为我仍然缺少一些东西,因为我没有收到任何短信.是的,我使用的电话号码是 100% 验证的。 更令人困惑的是,我的控制台和 Twilio SMS 日志都没有给我任何错误消息。控制台输出如下所示:

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

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

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(),但我收到错误消息,告诉我方法未定义。

Anywayyyy,任何人都可以给我一个关于如何排除故障的提示吗?这是我更改的唯一一段代码,所以也许我必须更改其他内容?

提前致谢。 :)

附:为什么我的代码末尾的ends 看起来很奇怪?它们在 Sublime 中看起来不像。

【问题讨论】:

  • 删除.sms 是正确的,这是已弃用的端点,现在使用client.account.messages 是正确的。你能具体记录下创建消息的结果并告诉我它说了什么吗?

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


【解决方案1】:

如果你这样做会发生什么:

#config/initializers/twilio.rb
  #move your TWILIO_CONFIG initialization here
  TwilioClient = Twilio::REST::Client.new(TWILIO_CONFIG['sid'], TWILIO_CONFIG['token'])

#models/user.rb
class User
  after_create :send_verification_message #best done with a background worker

  def send_verification_message
    TwilioClient.account.messages.create(
        from: TWILIO_CONFIG['from'],
        to: phone,
        body: "Thanks for signing up. To verify your account, please reply HELLO to this message."
        )
    end
  end
end

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

【讨论】:

  • 感谢您的回答!不幸的是,我仍然没有看到任何错误或文本。我注意到您的评论“最好由后台工作人员完成”。这是必要的吗?我不太确定如何设置。
【解决方案2】:

试试这个:

将其粘贴到您的控制器中

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 帐户从字段设置,并设置为客户端电话号码“+4563217892” 并调用控制器 send_otp 方法。

欲了解更多信息,请查看此链接:

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

【讨论】:

    【解决方案3】:

    基拉,

    查看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
    

    查看教程中的完整代码,如果有帮助,请告诉我。

    【讨论】:

    • 嗨梅根!感谢您的输入!它没有用,但我最终确实让应用程序正常工作。将用详细信息更新我最初的问题。
    【解决方案4】:

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

    虽然 Twilio 将为用户提供测试凭据以使用 Twilio REST API 的某些部分,但这些凭据不能用于发送消息或拨打电话。这就是为什么当我使用生产凭据时我的应用程序可以正常工作的原因。我没有因使用这些生产凭证而被收费(我认为您必须获得一些免费赠品)。

    作为记录,我从该教程中遵循的代码是正确的,但原始问题中提到的已弃用的 .sms 端点除外。那确实必须改变。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多