0

In my app's controller once a record is created I want to automatically open the client's whatsapp, sms or email app (depending of the type of the new record).

My create action looks like this:

def create
    session[:return_to] ||= request.referer

    @token = SecureRandom.uuid
    @new_token = Token.new(:token_value => @token, :sender_id => current_user.id, :card_id => params[:card_id], :type => params[:type])    

    respond_to do |format|
      format.html { redirect_to session.delete(:return_to), notice: "Token " + @new_token.token_value + " was successfully created."  }
    end

    if @new_token.type == "sms"
      #open client's sms app and autofill sms body with @new_token.token_value

    elsif @new_token.type == "email"
      #open client's email app and autofill subject + body with @new_token.token_value

    elsif @new_token.type == "whatsapp"
      #open client's whatsapp and autofill body with @new_token.token_value

    end

  end

How can I achieve that?

Oliver
  • 1,181
  • 2
  • 12
  • 30

1 Answers1

1

I gues you should be able to redirect to an app-specific url scheme. As an example of the idea:

<html>
<meta http-equiv="refresh" content="0; url=mailto:info@example.com?body=token&subject=Your new token" />
</html>

You could make a redirect to an email and whatsapp with a predrafted message:

redirect_to 'mailto:info@example.com?body=token&subject=Your new token'
redirect_to 'whatsapp://send?text=token'

Sadly, the sms url scheme only supports a telephone number. Note that the mailto:-url scheme works almost everywhere an e-mail client is available, support for the whatsapp:-url scheme depends on the OS and whether the person has WhatsApp installed. Hence, know what you will be redirecting to!

murb
  • 1,736
  • 21
  • 31
  • It works! However, it only does if I specify an email address. If I leave it blank - like 'mailto:?body..." it does not work. Is there a solution to this (because in my case the address should be blank). – Oliver Jun 14 '17 at 10:02
  • You should be able to do so: https://stackoverflow.com/questions/3540664/how-do-you-create-a-mailto-link-without-the-to-part ... but maybe you can work around it with %20 (a space?) – murb Jun 14 '17 at 10:16
  • I tried both. But the the redirect_to works only if there is an "@" after mailtto: – Oliver Jun 14 '17 at 10:29