Receive regular SMS to Viber and Telegram instant messengers (using GoIP gateways)

In many companies using IP-telephony and using GSM-gateways for corporate communications, the question often arises of receiving and sending SMS from their numbers.



For Chinese GOIP models, there is a good GOIP-SMS Server solution, which has been repeatedly mentioned on the Internet and in our Habr community as well. This is a simple, free and convenient product, but SMS via WEB, especially from mobile is not very convenient.



There was an idea of ​​receiving and sending SMS using a messenger - and was implemented in an hour using a simple Python3.6 script and Telegram bot (and 2 months later it was transferred to Viber). I ask under the cat:



The logic is as follows: We process and parse the request, retrieve the body of SMS, send it to bots and send it to personal chats - we get SMS, the topic is especially interesting for iPhone owners with 1 sim, when you quickly need to receive messages on a business trip or vacation, from banks or registration services or find out about operator replenishment:



So, we will consider the first part, namely, how to receive full-fledged SMS from a GSM network directly in Telegram, we need:





We’ll analyze an example script, but first add the recipient’s email address to the SMS server (several email addresses can be separated by commas:



image



Consider the script:



import imaplib import email import telebot #      pip install PyTelegramBotAPI bot = telebot.TeleBot('YOUTOKENBOT') #   mail = imaplib.IMAP4_SSL('imap.gmail.com') mail.login('yougmail@gmail.com', 'gmailpassword') #   @gmail mail.list() #      mail.select('INBOX') #     result, data = mail.uid('search', None, 'UNSEEN') #   i = len(data[0].split()) for x in range(i): latest_email_uid = data[0].split()[x] result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)') raw_email = email_data[0][1] mail.store(latest_email_uid, '+FLAGS', '\Deleted') #       raw_email_string = raw_email.decode('utf-8') email_message = email.message_from_string(raw_email_string) #       for part in email_message.walk(): if part.get_content_type() == "text/html" or part.get_content_type() == "text/plain": body = part.get_payload(decode=True) s = (email_message['Subject']) numb = s[5:16] #    11    7XXXXXXXXXX   ,  12      s[5:17] mss = str(body.decode('unicode-escape')) #      Python3  unicode,  2      abon = mss[54:67] #    smss = mss[77:] #   ,      soobsh=( numb + 'n'+ 'n' + smss + 'n' + ':' + abon) #    bot.send_message(chat_id, soobsh) #   chat_id       else: continue
      
      





Now we run the script, send SMS to our Sim and wait for processing - about 3 seconds it takes to transfer SMS to the messenger, if everything is correct, then we see our messages in our TelegramBot

(real numbers erased in order to avoid "Spring" conflicts):



image






image






image






You can wrap the script in a “def function” and call it every 3 or 20, or for how many seconds you need! Depends on the frequency of receiving SMS ok!



If someone doesn’t manage to repeat, write - we will analyze each case ...



See you in the next article!

73!



PS: the article lasted for about a year in the sandbox (approved by UFO on 08/20/2018) has undergone several changes, apparently the old articles do not automatically come, even if the new article allowed to become a full member. I publish the original original.



Now it can be discussed.



All Articles