📜 ⬆️ ⬇️

SMS chat on my knees

image

It took us somehow SMS chat for a small group of users. The main requirements were reliability and ease of implementation. Available was a mediocre office computer with Windows XP on board, a USB modem Huawei E1550, a SIM card with a positive balance and an average enikeyschik.

We wanted the following: engineers from among the operating personnel, taking up duty, are connected to the chat group and can exchange short text messages among themselves. This is useful for solving problems related to several departments (relay workers and power engineers, for example). When the presence in the group is not required - you can exit and do not receive messages.
')
Theoretically, everything was simple. We take a GSM modem, write a program that checks incoming messages on it, pulls out the necessary information from them, processes it and sends it to the list of connected subscribers.

In practice, everything turned out differently. Active google led us to several projects, including Habr. But it turned out to be quite complicated. For Delphi of us, no one really understands, and projects for Linux, we did not consider in principle - we have all Windows machines.

The latest find was a program from the St. Petersburg company Headwind Solutions. It is called “Personal SMS Server”. The program is paid, but there are 30 days of demo mode. We started picking it.

The program was very convenient and reliable. Its main feature is that it eliminates the need to work with the modem directly. She herself works with the GSM modem, checks incoming messages and sends outgoing ones. If a new script arrives, it sends two parameters to it: the sender's number and the message text. Everything else is decided by the script. Already from the script, you can call the procedure for sending messages to the program. There may be a lot of messages, the program itself will put them in a queue for sending and correctly transmit. The script is written in VBS.

The program, by the way, can work as a Windows service. We first ignored this opportunity and in vain the service is more stable.

We will not dwell on the program itself. It has many customizable parameters, is simple and well described in the documentation on the developer’s website.

More details on the chat script. In addition to the script itself, in the same directory you will need to create three files: hlr.txt, vlr.txt and vlr_back.txt.
The first file is filled with the numbers of subscribers with access to the chat. The format is as follows: mobile number without a plus <space> subscriber ID.

79111234567 Ivanov (power engineer)
79111234568 Petrov (WDM)
79111234569 Sidorov (RRL)

The second and third files are left blank. The second stores records of subscribers in the group, and the third file is used by the script to add / remove a subscriber from the group.

The script supports three commands. They begin with the sign # (lattice). To enter the chat group you need to send an SMS to the number of the SIM card of the modem with the text # 1 to exit # 0. To query the current list of users need to send #?
Requests from subscribers who are not in the group are ignored.

Incoming messages are processed as follows. The subscriber's identifier is placed at the beginning of the original message (we have the employee's last name and his department), and then this message is transmitted to all subscribers from the vlr.txt file. The sender receives a receipt for sending the message. It duplicates its message and the number of subscribers to whom it has been sent. A transfer receipt was added as a control measure. If the receipt is received, it means that the chat is operational, there is money on the SIM card and the message has flown away to all the chat participants.

I bring our program settings. Parameters were selected for several years by trial and error:

image

And the actual script:

VBS Chat Code
'   ,       Number = WScript.Arguments(0) Message = WScript.Arguments(1) Message = Trim(Message) '  ,    FlagVlr = 0 FlagHlr = 0 FlagSymbol=0 Users = "" Count = 0 '        vlr = "vlr.txt" hlr = "hlr.txt" vlr_back = "vlr_back.txt" '      HLR Set objFSO = CreateObject("Scripting.FileSystemObject") Set filehlr = objFSO.OpenTextFile(hlr, 1) Do Until filehlr.AtEndOfStream sLine = filehlr.ReadLine() nSpace = InStr(sLine, " ") If nSpace > 0 Then Number_hlr = Left(sLine, nSpace - 1) Name_hlr = Trim(Right(sLine, Len(sLine) - nSpace)) End If If (Number_hlr = Number) Then FlagHlr = 1 Name = Name_hlr End If If (Len(Users) = 2) Then Users = "" End If Loop filehlr.Close '      VLR Set objFSO = CreateObject("Scripting.FileSystemObject") Set filevlr = objFSO.OpenTextFile(vlr, 1) Do Until filevlr.AtEndOfStream sLine = filevlr.ReadLine() nSpace = InStr(sLine, " ") If nSpace > 0 Then Users = Users & ", " Number_vlr = Left(sLine, nSpace - 1) Name_vlr = Trim(Right(sLine, Len(sLine) - nSpace)) End If Count = Count + 1 If (Number_vlr = Number) Then FlagVlr = 1 Name = Name_vlr End If If (Len(Users) = 2) Then Users = "" End If Users = Users & Name_vlr Loop filevlr.Close '           HLR  VLR If (FlagHlr = 0) Then Flaguser = 0 End If If (FlagHlr = 1) And (FlagVlr = 0) Then Flaguser = 1 End If If (FlagHlr = 1) And (FlagVlr = 1) Then Flaguser = 2 End If '     MessageAlreadyEnter = "    .   : " & Users & "." MessageNowInGroup = "  : " & Users & "." MessageEmpty = "   ." '        If (Count = 0) Then MessageNowInGroup = "   ." End If If (Count = 1) Then MessageAlreadyEnter = "    .    ." End If '    dlina = Len(Message) symbol = Trim(Message) symbol = Left(symbol, 1) If (symbol = "#") Then FlagSymbol = 1 End If '       If (Message = "#1") And (Flaguser = 1) Then FlagVlr = 0 FlagSymbol = 0 Set filevlr = objFSO.OpenTextFile(vlr, 1) Set filevlrback = objFSO.OpenTextFile(vlr_back, 2) rec = Number + " " + Name filevlrback.WriteLine(rec) Do Until filevlr.AtEndOfStream sw = filevlr.ReadLine() filevlrback.WriteLine(sw) Loop filevlr.Close filevlrback.Close Set filevlr = objFSO.OpenTextFile(vlr, 2) Set filevlrback = objFSO.OpenTextFile(vlr_back, 1) Do Until filevlrback.AtEndOfStream sw = filevlrback.ReadLine() filevlr.WriteLine(sw) Loop filevlr.Close filevlrback.Close ' =======        ======= Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = "    . " + MessageNowInGroup objMsg.Send() End If '      ,       If (Message = "#1") And (Flaguser = 2) Then FlagVlr = 0 FlagSymbol = 0 Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = "    . " + MessageNowInGroup objMsg.Send() End If '        If (Message = "#0") And (Flaguser = 1) Then FlagSymbol = 0 End If '       If (Message = "#0") And (FlagVlr = 1) Then FlagVlr = 0 FlagSymbol = 0 Set filevlr = objFSO.OpenTextFile(vlr, 1) Set filevlrback = objFSO.OpenTextFile(vlr_back, 2) Do Until filevlr.AtEndOfStream sLine = filevlr.ReadLine() nSpace = InStr(sLine, " ") If nSpace > 0 Then Number_vlr = Left(sLine, nSpace - 1) Name_vlr = Trim(Right(sLine, Len(sLine) - nSpace)) rec = Number_vlr + " " + Name_vlr End If If (Number_vlr <> Number) Then filevlrback.WriteLine(rec) End If Loop filevlr.Close filevlrback.Close Set filevlr = objFSO.OpenTextFile(vlr, 2) Set filevlrback = objFSO.OpenTextFile(vlr_back, 1) Do Until filevlrback.AtEndOfStream rec = filevlrback.ReadLine() filevlr.WriteLine(rec) Loop filevlr.Close filevlrback.Close ' =======        ======= Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = "   ." objMsg.Send() End If '       If (Message = "#?") And (FlagVlr = 1) Then FlagVlr = 0 FlagSymbol = 0 Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = MessageNowInGroup objMsg.Send() End If '      If (FlagSymbol = 1) And (FlagHlr = 1) Then FlagVlr = 0 FlagSymbol = 0 Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = "  . : #1 - , #0 - , #? -  ." objMsg.Send() End If '    If (Message = "") And (FlagVlr = 1) Then FlagVlr = 0 Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = MessageEmpty objMsg.Send() End If '    If (FlagVlr = 1) Then Message = Trim(Message) Set objFSO = CreateObject("Scripting.FileSystemObject") Set filevlr = objFSO.OpenTextFile(vlr, 1) Set objSMSDriver = CreateObject("HeadwindGSM.SMSDriver") objSMSDriver.Connect() Do Until filevlr.AtEndOfStream sLine = filevlr.ReadLine nSpace = InStr(sLine, " ") If nSpace > 0 Then Number_buffer = Left(sLine, nSpace - 1) Name_buffer = Trim(Right(sLine, Len(sLine) - nSpace)) If (Number_buffer <> Number) Then Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number_buffer objMsg.Body = Name & ": " & Message objMsg.Send() End If End If Loop filevlr.Close Count_receipt = Count - 1 ' =======   ======= Set objMsg = CreateObject("HeadwindGSM.SMSMessage") objMsg.To = Number objMsg.Body = "Message send to " & Count_receipt & " users. Text: '" & Message & "'" objMsg.Send() ' ======= /  ======= End If ' =======   ======= WScript.Quit 




So we got a simple SMS chat, which is now working for the benefit of domestic engineers. We hope that it will be useful to you.

Source: https://habr.com/ru/post/259057/


All Articles