[RESUELTO] Conexion http API envio SMS

Desarrollo de Macros y programación en UNO, usar las API, llamar programas externos...
Responder
Avatar de Usuario
JoePublic
Mensajes: 36
Registrado: Dom Sep 06, 2020 5:34 pm

[RESUELTO] Conexion http API envio SMS

Mensaje por JoePublic »

Hola,
RESUMEN: Traigo un tema super interesante y me gustaria saber como establecer conexión desde Calc para el envio de sms a traves de un proveedor de servicios de SMS.

Me gustaria conectar con LibreOffice CALC a una http API para poder enviar sms con Calc.
Tengo un servicio contratado con un proveedor xxx.com para el envio de sms y con su api puedo enviar sms a traves de Excel.
Pero ahora necesito hacerlo con Calc de libre Office y como este software libre usa OOoBasic (otro Basic) no se muy bien como son las funciones, objetos o metodos a los que he de llamar.

Adjunto mi codigo en Excel, alguien sabe como conectar con esta API via http, URL ?

Las lineas que ME INTERESA SABER SON las siguientes:

Código: Seleccionar todo

'**** Conect to the API via Htpp ****************
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0") 'Create Object
oXMLHTTP.Open "GET", RouteHtml
oXMLHTTP.Send

Send_SMS = oXMLHTTP.responseText 'The sms provider give us a feedback to know if all went fine.

'** Clean the object *********
Set oXMLHTTP = Nothing
Con EXCEL conectamos con una librería para crear este objeto, pero... y con CALC?, como referenciamos esta librería ?
Agradecería un ejemplo del codigo descrito arriba. El codigo completo PARA EXCEL lo pongo aqui abajo. GRACIAS !!

Código: Seleccionar todo

Function Send_SMS(sMobileNumbers As String)
'Send sms via Excel. For this you will have to activate Reference -> Microsoft XML v6.0 in menu VBA Tools/References/ (Activate library VBA Microsoft XML v6.0)
'You can send sms with excel. Send one o several sms. If you want to send same text to several telephones numbers, you must separate each telephone with a coma.
'For instance: sMobileNumbers = "49123456789,32123456789,34123456789"

'*** Este macro es para Excel ******************
Dim SmsOperator_Provider As String
Dim oXMLHTTP As Object
Dim Result As String
Dim RouteHtml As String

Dim sUrl As String
Dim sAPI_ID As String
Dim sPassword As String
Dim sUsername As String
Dim sTelefonFrom As String
Dim sSenderId As String
Dim Text_Sms As String
Dim sreq_feat As String

'***** Get tHe data from our EXCEL Sheet **************
sTelefonFrom = Worksheets("Sms").Range("B4").Value
Text_Sms = Worksheets("Sms").Range("C4").Value

SmsOperator_Provider = Worksheets("Sms").Range("C2").Value 'NAME from our Sms operator.


sUrl = Worksheets("Sms").Range("B10").Value
sUsername = Worksheets("Sms").Range("B11").Value
sPassword = Worksheets("Sms").Range("B12").Value
sAPI_ID = Worksheets("Sms").Range("B13").Value 'id de registro API en proveedor de sms
sSenderId = Worksheets("Sms").Range("B14").Value 'id del que envia el sms.
sreq_feat = Worksheets("Sms").Range("B15").Value 'Es el requisito que se pone al enlace para que salga el id sender en el sms.
	
'***** Concatenate the http API pattern  '*********
'https://www.smsdiscount.com/myaccount/sendsms.php?username=xxxxxxxxxx&password=xxxxxxxxxx&from=xxxxxxxxxx&to=xxxxxxxxxx&text=xxxxxxxxxx
'For several clients the variable must be like this: sMobileNumbers = "+34123456789,+34123456789,+34123456789"
RouteHtml = sUrl
RouteHtml = RouteHtml & "username=" & sUsername
RouteHtml = RouteHtml & "&password=" & sPassword
RouteHtml = RouteHtml & "&from=" & sTelefonFrom
RouteHtml = RouteHtml & "&to=" & sMobileNumbers
RouteHtml = RouteHtml & "&text=" & Text_Sms

'**** Conect to the API via Htpp ****************
Set oXMLHTTP = CreateObject("MSXML2.XMLHTTP.6.0") 'Create Object
oXMLHTTP.Open "GET", RouteHtml
oXMLHTTP.Send

'*** Get the feedback from SMS_Provider **************
'Usually they provide some information to know whether sms was send successfully or not.

Send_SMS = oXMLHTTP.responseText 'The sms provider give us a feedback to know if all went fine.

'** Clean the object *********
Set oXMLHTTP = Nothing
End Function
Última edición por JoePublic el Vie Feb 11, 2022 6:56 pm, editado 2 veces en total.
Usuario apasionado de las hojas de cálculo. Novato aprendiz de macros con OOo Basic y VBA.
LibreOffice Version: 6.4.3.2 (x64) y Sistema Operativo Windows 10 Home.
mriosv
Mensajes: 2334
Registrado: Sab Dic 27, 2008 1:12 am
Ubicación: Galiza (España)

Re: Conexion http API envio SMS

Mensaje por mriosv »

Creo que está cuestión ya la has planteado en el foro en inglés de LibreOffice.
Puedes usar "Subir adjunto" en Respuesta rápida - Editor Completo - Pestaña Subir adjunto
Apache OpenOffice 4, LibreOffice (Win10x64)
Avatar de Usuario
JoePublic
Mensajes: 36
Registrado: Dom Sep 06, 2020 5:34 pm

Re: Conexion http API envio SMS

Mensaje por JoePublic »

Si la planteé en el foro ingles para abarcar a mas gente que me pueda ayudar.... Afortunadamente alli me contestaron y tengo la respuesta.
Adjunto el archivo con el macro y sus funciones.

Para que funcione, primero tienes que contratar un servicio de sms y que te de el nombre de usuario, el id, y password, etc para poder enviar sms via API.
Solo tienes que sustituir los parametros que encontrarás en la hoja pestaña SETTINGS. Y funciona !!!.
El macro envia sms a todos los que figuran en la lista de la hoja LIST.
Gracias !!.

Código: Seleccionar todo

Sub sendSMS
'This macro send sms to the people who is in the list.

Dim numberRow as Long
Dim oleService As Object
Dim xmlHttpReq As Object
Dim responseAPI as String

Dim sUrl as String
Dim sUsername as String
Dim sPassword as String
Dim sAPI_ID as String
Dim sSenderId as String
Dim sreq_feat as String
Dim sMobileNumbers as String
Dim Text_Sms as String


Dim oSheet as Object
Dim oCell as Object
Dim oRegion as Object

Dim ColName as String
dim indexColumn as Long
Dim finalRow as Long


'******* Get last row in table ***********************************
oCell = ThisComponent.Sheets.getByName("List").getCellRangeByName("A1")
oRegion = getCurrentRegion2(oCell)
finalRow = oRegion.RangeAddress.EndRow
'msgbox finalRow
'**********************************************
'numberRow = getNumberRow() 'if you activate or select a cell of any column, start from this row to the end of table from LIST sheet.
numberRow = 1 'Starts in row 2 until the end.
'**** Get values of sheet *********************
sUrl = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B5").getString()
sUsername = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B6").getString()
sPassword = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B7").getString()
sAPI_ID = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B8").getString()
sSenderId = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B9").getString()
sreq_feat = ThisComponent.getSheets().getByName("Settings").getCellRangeByName("B10").getString()

'******* Get the parameters of the Column where we set a ticket number value *****************
ColName = "F"
oSheet = ThisComponent.Sheets.getByName("List") 'Accede a la hoja por su indice. Hoja numero 1
'indice = GetColumnIndex(oSheet, ColName)
indexColumn = GetColIndex(ColName)
'Msgbox indexColumn


'**** Conect to the API via Htpp ****************
oleService = createUnoService("com.sun.star.bridge.OleObjectFactory")
xmlHttpReq = oleService.createInstance("MSXML2.XMLHTTP.6.0")



'****** We iterate over the whole list *************************
For i=1 to finalRow
numberRow = numberRow + 1
sMobileNumbers = ThisComponent.getSheets().getByName("List").getCellRangeByName("D" & numberRow).getString()
sMobileNumbers = "+" & sMobileNumbers
Text_Sms = ThisComponent.getSheets().getByName("List").getCellRangeByName("E" & numberRow).getString()

'***** Concatenate the http API pattern  '*********
'For several clients the variable must be like this: sMobileNumbers = "+34123456789,+34123456789,+34123456789"

        RouteHtml = sUrl
        RouteHtml = RouteHtml & "user=" & sUsername
        RouteHtml = RouteHtml & "&password=" & sPassword
        RouteHtml = RouteHtml & "&api_id=" & sAPI_ID
        
        RouteHtml = RouteHtml & "&to=" & sMobileNumbers
        RouteHtml = RouteHtml & "&text=" & Text_Sms
        
        RouteHtml = RouteHtml & "&from=" & sSenderId
        RouteHtml = RouteHtml & "&" & sreq_feat  
		
		MsgBox RouteHtml


	'******* Send sms to Provider ******************
	'xmlHttpReq.open("GET", "https://www.google.com", false)
	xmlHttpReq.open("GET", RouteHtml, false)
	xmlHttpReq.send()
	   
	'*** Get the feedback from SMS_Provider **************
	'Usually the sms service provide a ticket code justifying that sms was sent successfully.
	responseAPI = xmlHttpReq.responseText 'The sms provider give us a feedback to know if all went fine.
	'Msgbox responseAPI
	'***** We set the Shipping ticket incurrent Row and F Column of LIST sheet **********************************
	Call setValueStringInCell2("List", indexColumn, numberRow - 1, responseAPI)
	'*******************************************************
next i

'** Clean the object and Empty MEMORY *********
xmlHttpReq = Nothing
End Sub
Adjuntos
Send_SMS_LibreOffice.ods
(17.16 KiB) Descargado 233 veces
Usuario apasionado de las hojas de cálculo. Novato aprendiz de macros con OOo Basic y VBA.
LibreOffice Version: 6.4.3.2 (x64) y Sistema Operativo Windows 10 Home.
Responder