首页 文章

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

提问于
浏览
1

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

我按照这里的说明知道了我的情况:https://www.sitepoint.com/adding-sms-capabilities-to-your-rails-app/这些超级伟大,但也写于4年前 . 我注意到Twilio使用了不同的资源URI(消息与短信/消息),而我仍然遗漏了一些东西,因为我使用的是'm not receiving any text messages. And yes, the phone number I' m已经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.sms.messages.create() 更改 client.account.messages.create() ,但无济于事 . 我也尝试将其更改为 client.account.sms.messages.sendMessage() ,但我收到一个错误,告诉我该方法未定义 .

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

提前致谢 . :)

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

4 回答

  • 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
    

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

  • 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

  • 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
    

    看一下教程中的完整代码,让我知道它是否有用 .

  • 0

    我只是想跟进这个 - 我写了这个问题 . 事实证明,你可以't test in this manner with Twilio'的测试凭证 . 如Twilio文档中所述,https://support.twilio.com/hc/en-us/articles/223183808-Why-aren-t-my-test-credentials-working-

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

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

相关问题