短信未使用Twilio和Rails发送

时间:2019-03-07 20:57:27

标签: ruby-on-rails ruby-on-rails-4 sms twilio twilio-api

我试图集成Twilio和我的rails应用程序,以根据选择的选项发送不同的文本消息并将其保存到表单中。但是,在研究了文档并查看了他们提供的示例应用程序之后(发送ETA通知),保存了完整的表格,没有发送任何短信,而且我不知道为什么。我希望有一些建议

job_status是文本消息正文需要更改的选项:

 JOB_STATUSES = ["Wildey Que", "In Service-Bay", "Awaiting Approval", 
 "Awaiting Parts", "Jackson Collection Que", "Wildey Collection Que", 
 "Completed"]

message_sender.rb

class MessageSender

    require 'twilio-ruby'

  def self.send_message(job_id, host, to, message)
    new.send_message(job_id, host, to, message)
  end

  def initialize
    # To find TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN visit
    # https://www.twilio.com/console
    account_sid = ENV['---'] (These are entered)
    auth_token  = ENV['---']
    @client = Twilio::REST::Client.new(account_sid, auth_token)
  end


  def send_message(job_id, host, to, message)
    @client.messages.create(
      from:  twilio_number,
      to:    to,
      body:  message,
      status_callback: "http://#{host}/jobs/#{job_id}"
    )
  end

  private

  def twilio_number
    # A Twilio number you control - choose one from:
    # https://www.twilio.com/console/phone-numbers/incoming
    # Specify in E.164 format, e.g. "+16519998877"
    ENV['+17652957305']
  end
end

jobs_controller.rb

 class JobsController < ApplicationController
      before_action :set_job, only: [:show, :edit, :update, :destroy]
      before_action :authenticate_user!



      # GET /jobs
      # GET /jobs.json
      def index
        if(params.has_key? (:job_status))
          @jobs = Job.where(job_status: params[:job_status]).order("created_at desc")
        else
          @jobs = Job.all.order("created_at desc")
        end
      end

      # GET /jobs/1
      # GET /jobs/1.json
      def show
      end

      # GET /jobs/new
      def new
        @job = current_user.jobs.build
      end

      # GET /jobs/1/edit
      def edit
      end

      #TWILLIO INITILIZATION 

     def send_initial_notification
        @job.job_status = :Wildey_Que
        if @job.save
          message = 'Equip4you: Thanks for dropping your machine off, we will keep you updated here every step of the way'
          notify(message)
        else
          redirect_with_error
        end
      end

      def send_delivery_notification
        @job.job_status = :Completed
        if @job.save
          message = 'Equip4you: Thank you for allowing us to take care of your machine for you, if you have any further questions or concerns feel free to contact 425-9999'
          notify(message)
        else
          redirect_with_error
        end
      end

      #END TWILLIO INIT

      # POST /jobs
      # POST /jobs.json
      def create
        @job = current_user.jobs.build(job_params)

        respond_to do |format|
          if @job.save
            format.html { redirect_to @job, notice: 'Job was successfully created.' }
            format.json { render :show, status: :created, location: @job }
          else
            format.html { render :new }
            format.json { render json: @job.errors, status: :unprocessable_entity }
          end
        end
      end

      # PATCH/PUT /jobs/1
      # PATCH/PUT /jobs/1.json
      def update
        respond_to do |format|
          if @job.update(job_params)
            format.html { redirect_to @job, notice: 'Job was successfully updated.' }
            format.json { render :show, status: :ok, location: @job }
          else
            format.html { render :edit }
            format.json { render json: @job.errors, status: :unprocessable_entity }
          end
        end
      end

      # DELETE /jobs/1
      # DELETE /jobs/1.json
      def destroy
        @job.destroy
        respond_to do |format|
          format.html { redirect_to jobs_url, notice: 'Job was successfully destroyed.' }
          format.json { head :no_content }
        end
      end

      private

      # TWILLIO INNIT

      def notify(message)
        MessageSender.send_message(
          @job.id, request.host, @job.cell_number, message)
        redirect_to jobs_url, notice: 'Message was delivered'
      end

      def redirect_with_error
        message = "An error has occurred updating the ticket status"
        redirect_to orders_url, flash: { error: message }
      end

      #TWILLIO INIT END


        # Use callbacks to share common setup or constraints between actions.
        def set_job
          @job = Job.find(params[:id])
        end

        # Never trust parameters from the scary internet, only allow the white list through.
        def job_params
          params.require(:job).permit(:job_status, :purchase_name, :contact_name, :cell_number, :home_number, :other_number, :other_number, :address, :machine_mod, :item_number, :serial_number, :concern, :accessories, :pickup_location, :paid, :invoice_number, :outcome, :avatar)
        end
    end

Routes.rb

require 'sidekiq/web'

Rails.application.routes.draw do
  resources :jobs
  devise_for :users
  root to: 'jobs#index'


  post '/jobs/new', to: 'jobs#new', as: 'initial_notifications'
  post '/jobs/new', to: 'jobs#new', as: 'delivery_notifications'

routes.rb

require 'sidekiq/web'

Rails.application.routes.draw do
  resources :jobs
  devise_for :users
  root to: 'jobs#index'


  post '/jobs/new', to: 'jobs#new', as: 'initial_notifications'
  post '/jobs/new', to: 'jobs#new', as: 'delivery_notifications'

1 个答案:

答案 0 :(得分:0)

请检查此定义。看起来很奇怪。

  def twilio_number
    # A Twilio number you control - choose one from:
    # https://www.twilio.com/console/phone-numbers/incoming
    # Specify in E.164 format, e.g. "+16519998877"
    ENV['+17652957305']
  end