)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » SmsController (Ia.Wa.Controllers)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using Microsoft.AspNetCore.Mvc;
    2: using System.Diagnostics;
    3: using System.Net;
    4: using System.Text.RegularExpressions;
    5:  
    6: namespace Ia.Wa.Controllers
    7: {
    8:     ////////////////////////////////////////////////////////////////////////////
    9:  
   10:     /// <summary publish="true">
   11:     ///
   12:     /// </summary>
   13:     /// 
   14:     /// <remarks> 
   15:     /// Copyright � 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   16:     ///
   17:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   18:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   19:     ///
   20:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   21:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   22:     /// 
   23:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   24:     /// 
   25:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   26:     /// </remarks> 
   27:     public class SmsController : Controller
   28:     {
   29:         /////////////////////////////////////////////////////////////////////////////////
   30:  
   31:         /// <summary>
   32:         ///
   33:         /// </summary>
   34:         [Route("/service/sms")]
   35:         public IActionResult Sms()
   36:         {
   37:             return View("/Views/Service/Sms.cshtml");
   38:         }
   39:  
   40:         /////////////////////////////////////////////////////////////////////////////////
   41:  
   42:         /// <summary>
   43:         ///
   44:         /// </summary>
   45:         [HttpPost]
   46:         [Route("/service/sms")]
   47:         public IActionResult Sms(string phoneTextInput, string messageTextArea)
   48:         {
   49:             var result = ProcessSms(phoneTextInput, messageTextArea);
   50:  
   51:             if (result.IsSuccessful)
   52:             {
   53:                 ViewBag.ResultMessage = result.Message;
   54:                 ViewBag.ResultClass = "success";
   55:             }
   56:             else
   57:             {
   58:                 ViewBag.ResultMessage = result.Message;
   59:                 ViewBag.ResultClass = "error";
   60:             }
   61:  
   62:             return View("/Views/Service/Sms.cshtml");
   63:         }
   64:  
   65:         /////////////////////////////////////////////////////////////////////////////////
   66:  
   67:         /// <summary>
   68:         ///
   69:         /// </summary>
   70:         protected Ia.Cl.Models.Result ProcessSms(string phoneTextInput, string messageTextArea)
   71:         {
   72:             var result = new Ia.Cl.Models.Result();
   73:  
   74:             var phone = WebUtility.HtmlEncode(phoneTextInput);
   75:             var message = WebUtility.HtmlEncode(messageTextArea);
   76:  
   77:             var senderName = "IntApp"; // senderName can not exceed 11 chars Ia.Cl.Models.ApplicationConfiguration.GetSetting("AppSettings:SiteName");
   78:  
   79:             if (!string.IsNullOrEmpty(phone))
   80:             {
   81:                 if (Regex.IsMatch(phone, @"\d{8,}"))
   82:                 {
   83:                     if (!string.IsNullOrEmpty(message))
   84:                     {
   85:                         if (message.Length < 140)
   86:                         {
   87:                             //text = "This is a test SMS is sent by a visitor to https://ia.com.kw: " + text_tb.Text;
   88:  
   89:                             if (phone.IndexOf("!") == 0)
   90:                             {
   91:                                 phone = phone.Replace("!", string.Empty);
   92:  
   93:                                 result = Ia.Cl.Models.Sms.Send(phone, senderName, message);
   94:                             }
   95:                             else
   96:                             {
   97:                                 result.AddError("Service is temporarily restricted.");
   98:                             }
   99:                         }
  100:                         else
  101:                         {
  102:                             result.AddError("Message text field is too long. Must contain at most 140 (or 70 utf8) characters only.");
  103:                         }
  104:                     }
  105:                     else
  106:                     {
  107:                         result.AddError("Message text field is empty.");
  108:                     }
  109:                 }
  110:                 else
  111:                 {
  112:                     result.AddError("Phone number is invalid.");
  113:                 }
  114:             }
  115:             else
  116:             {
  117:                 result.AddError("Phone number field is empty.");
  118:             }
  119:  
  120:             return result;
  121:         }
  122:  
  123:         /////////////////////////////////////////////////////////////////////////////////
  124:         /////////////////////////////////////////////////////////////////////////////////
  125:  
  126:         /// <summary>
  127:         ///
  128:         /// </summary>
  129:         [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
  130:         public IActionResult Error()
  131:         {
  132:             return View(new Ia.Wa.Models.ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
  133:         }
  134:  
  135:         /////////////////////////////////////////////////////////////////////////////////
  136:         /////////////////////////////////////////////////////////////////////////////////
  137:     }
  138:  
  139:     /////////////////////////////////////////////////////////////////////////////////
  140:     /////////////////////////////////////////////////////////////////////////////////
  141: }