SMS-сообщения не отправляются с использованием Twilio и Rails - PullRequest
0 голосов
/ 07 марта 2019

Я пытаюсь интегрировать 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 голосов
/ 08 марта 2019

Пожалуйста, проверьте это определение.Это выглядит странно.

  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
...