SMS Script Configuration

SMS Script Configuration

This script is a ready-to-use template for sending SMS notifications. For the script to function, you must enter the information received from your SMS provider here.

The example script is as follows:

using System;
using System.IO;
using System.Collections;
using System.Windows.Forms;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CSharp;
using Newtonsoft.Json;

public class MainClass
{
    public string Phone;
    public string Message;
    public int Timeout;
    public byte RetryCount;
    public bool Result;
    public string ErrorMessage;

    public void Main()
    {        
        string apiUrl = "https://api.smsprovider.com/send";
        string apiKey = "API_KEY";
        string sender = "SCADA";
        using (HttpClient client = new HttpClient()){
            client.Timeout = TimeSpan.FromSeconds(Timeout / 1000);
            client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
            var postData = new
                from = sender, 
                to = Phone, 
                text = Message 
            };
            string json = JsonConvert.SerializeObject(postData);
            StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpResponseMessage response = null;
            for (int i = 0; i < RetryCount; i++){
                try{
                    Task<HttpResponseMessage> responseTask = client.PostAsync(apiUrl, content);
                    responseTask.Wait();
                    response = responseTask.Result;
                    if (response.IsSuccessStatusCode){
                        Result = true;
                        return;
                    } else {
                        Task<string> errorTask = response.Content.ReadAsStringAsync();
                        errorTask.Wait();
                        ErrorMessage += $"Error {i + 1}: {errorTask.Result}{Environment.NewLine}";
                    }
                } catch (Exception ex) {
                    if (ex is AggregateException aggEx && aggEx.InnerException != null)
                        ErrorMessage += $"Exception {i + 1}: {aggEx.InnerException.Message}{Environment.NewLine}"; 
                    else
                        ErrorMessage += $"Exception {i + 1}: {ex.Message}{Environment.NewLine}";
                }
                if (i < RetryCount - 1){
                    System.Threading.Thread.Sleep(2000);
                }
            }
        }
    }
}

Fields to Configure

  • apiUrl: Enter your SMS provider’s API address here. Example: “https://api.smsprovider.com/send”
  • apiKey: Paste your private API key (token) received from your SMS provider into this field. Example: “API_KEY”
  • sender: Enter the name or number that the SMS will be sent from here. Example: “SCADA” or “5XXXXXXXXX”

How the Script Works?

  1. Data Input: The script automatically retrieves values such as Phone, Message, Timeout, and RetryCount from the notification settings table.
  2. Sending Request: It sends an HTTP request to the apiUrl using this information.
  3. Retry: If an error occurs on the first attempt, the script attempts the operation again based on the RetryCount value.
  4. Result: If the operation is successful, the Result variable returns true. In case of an error, the error message is saved to the ErrorMessage variable. These values are used to update the Success and Fault tags in your notification settings table.

Note: Once you enter the settings, the script is compiled and ready for use. You can use the Try To Send button in the notification table to test the SMS sending process.

Leave a Reply