require_relative '../whatsapp' module Gupshup module REST class OutboundMessage < Gupshup::REST::WhatsApp attr_accessor :phone, :app, :base_uri, :apikey, :version, :content_type, :path, :headers def initialize(app, apikey, phone, version = '2') super @channel = 'whatsapp' @source = phone @headers = { 'Content-type' => @content_type, 'apikey' => @apikey } end def send(destination, message_params) # Exception handling for parameters to be added case message_params[:type] when 'text' if message_params[:isHSM] send_text_message(destination, message_params[:text], isHSM = true) else send_text_message(destination, message_params[:text], isHSM = false) end when 'sticker' send_sticker(destination, message_params[:url]) when 'image' send_image(destination, message_params[:originalUrl], message_params[:previewUrl], message_params[:caption]) when 'video' send_video(destination, message_params[:url], message_params[:caption]) when 'audio' send_audio(destination, message_params[:url]) when 'file' send_file(destination, message_params[:url], message_params[:filename]) when 'location' send_location(destination, message_params[:longitude], message_params[:latitude], message_params[:name], message_params[:address]) when 'list' send_list(destination, message_params[:title], message_params[:body], message_params[:globalButtons], message_params[:items]) when 'quick_reply' send_button(destination, message_params[:content], message_params[:options]) else puts 'Done' end end # @param [Object] destination def send_text_message(destination, message, isHSM=false) payload = {'isHSM' => isHSM, 'type' => 'text', 'text' => message }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_image(destination, imageUrl, previewUrl, caption) payload = {'type' => 'image', 'originalUrl' => imageUrl, 'previewUrl' => previewUrl, 'caption' => caption }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } puts payload r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_sticker(destination, url) payload = { 'type': 'sticker', 'url': url }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_video(destination, url, caption) payload = {'url' => url, 'type' => 'video', 'caption' => caption }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_file(destination, url, filename) payload = {'url' => url, 'type' => 'file', 'filename' => filename }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_audio(destination, url) payload = {'url' => url, 'type' => 'audio' }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_location(destination, longitude, latitude, name, address) payload = { "type": 'location', "longitude": longitude, "latitude": latitude, "name": name, "address": address }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_list(destination, title, body, globalButtons, items) uuid = UUID.new payload = { "type": 'list', "title": title, "body": body, "msgid": uuid.generate , "globalButtons": [{ "type": globalButtons[0][:type], "title": globalButtons[0][:title] }], "items": items }.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end def send_button(destination, content, options) uuid = UUID.new payload = { "type": 'quick_reply', "content": content, "msgid": uuid.generate, "options": options}.to_json data = { 'channel' => @channel, 'destination' => destination.to_s, 'source' => @source, 'src.name' => @app, 'message' => payload } r = Gupshup::HTTP::Client.new r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers) end end end end