Postgresql functions as a backend (p.3: Send SMS from the function)

pip3 install requests
pip3 install requests-oauthlib
create or replace function sm_send_sms(phone text, msg text)
returns bool as $W$
from requests_oauthlib import OAuth1Session
import sys
phone=args[0]
msg=args[1]
key = '**<YOUR_KEY>**'
secret = '**<YOUR_SECRET>**'
gwapi = OAuth1Session(key, client_secret=secret)
req = {
'sender': 'MYAPP',
'message': msg,
'recipients': [{'msisdn': phone}],
}
res = gwapi.post('https://gatewayapi.com/rest/mtsms', json=req)
res.raise_for_status()
return res
$W$ language plpython3u parallel safe;

/* EXECUTE
select sm_send_sms('357666666','Buy cool stuff!');*/

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store