Mobile Post/Get package removed in 00.07.14

We are using HTTP requests to send SMS messages using a TRB500.

I just found out the hard way that this feature/package was removed in release 7.14, so I had to downgrade to 7.13.4 to bring the feature back.

Is there another alternative to this feature? Is there an API that provides the same functionality?

Hello,

Starting from firmware 00.07.14.0, the POST/GET services were completely withdrawn. You can find more details on this change here:

As an alternative to sending an SMS message, you can utilize the API requests, as you’ve mentioned yourself. More information specifically on sending SMS messages can be found here: https://developers.teltonika-networks.com/reference/trb500/7.14.3/v1.6.3/messages#post-messages-actions-send

If you have any additional questions or need assistance, feel free to reach out.

Best regards,
M.

We’ve made the switch to the API. Here is the Python script that we made to utilize the API. Hopefully it can help someone else to get started:

#!/usr/bin/python

import sys
import requests
import json
#from unidecode import unidecode

# Disable SSL warnings
#import urllib3
#from urllib3.exceptions import InsecureRequestWarning
#urllib3.disable_warnings(InsecureRequestWarning)

debugging=0

# Teltonika router modem
modem="3-1"

def get_token(ip, username, password):
    """Authenticate and retrieve the token."""
    url = f"https://{ip}/api/login"
    headers = {"Content-Type": "application/json"}
    payload = {"username": username, "password": password}

    response = make_http_request(url, headers, payload)

    token = response.json().get("data", {}).get("token")
    if token:
        if debugging:
            print(f"Token obtained successfully: {token}")
    else:
        print("Error: Token not found in the response.")
        sys.exit(1)

    return token

def send_sms(ip, token, phone_number, message):
    """Send SMS using the Teltonika API."""
    url = f"https://{ip}/api/messages/actions/send"
    headers = {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }

    # Convert the literal \n string into actual newlines
    message = message.replace(r"\n", "\n")

    # Transliterate the message to remove accented characters (GSM 7-bit encoding)
    #message = unidecode(message)

    # Check the length of the message
    if len(message) > 440:
        print("Message length exceeded...truncating")
        message = message[:440]  

    payload = { "data": {
        "number": phone_number,
        "message": message,
        "modem": modem
    }}

    if debugging:
        print(f"Payload: {json.dumps(payload, indent=2)}")

    response = make_http_request(url, headers, payload)

    if debugging:
        print(f"SMS sent successfully: {response.json()}")
    else:
        print(f"SMS sent successfully")


def make_http_request(url, headers, payload, timeout=10):
    try:
        response = requests.post(url, headers=headers, json=payload, verify=False, timeout=timeout)
        response.raise_for_status()  # Raise an error for bad status codes
        return response
    except requests.exceptions.HTTPError as e:
        if response.status_code == 422:
            print(f"Unprocessable Entity: {response.text}")
        elif response.status_code == 401:
            print(f"Unauthorized: {response.text}")
        else:
            print(f"HTTP error occurred: {e}")
        sys.exit(1)
    except requests.exceptions.Timeout as e:
        print(f"Request timed out: {e}")
        sys.exit(1)
    except requests.exceptions.RequestException as e:
        print(f"Error making request: {e}")
        sys.exit(1)

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("Usage: "+ sys.argv[0] +" <GW_IP> <USERNAME> <PASSWORD> \"<PHONE_NUMBER>\" \"<MESSAGE>\"")
        sys.exit(1)

    gw_ip = sys.argv[1]
    username = sys.argv[2]
    password = sys.argv[3]
    phone_number = sys.argv[4]
    message_text = sys.argv[5]

    token = get_token(gw_ip, username, password)

    if not token or token == "":  # Check if token is empty or None
        print("Error: Invalid or empty token.")
        sys.exit(1)

    send_sms(gw_ip, token, phone_number, message_text)
2 Likes

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.