Advertisement
shaggycat

Untitled

Jun 16th, 2018
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import socket
  3. import threading
  4. from time import gmtime, strftime
  5. import pickle
  6. import telegram
  7. #import telegram.ext
  8. #import telegram.error
  9. import logging
  10. import sys
  11. import configparser
  12.  
  13.  
  14. def ConfigSectionMap(section):
  15.     dict1 = {}
  16.     options = config.options(section)
  17.     for option in options:
  18.         try:
  19.             dict1[option] = config.get(section, option)
  20.             if dict1[option] == -1:
  21.                 DebugPrint("skip: %s" % option)
  22.         except:
  23.             print("exception on %s!" % option)
  24.             dict1[option] = None
  25.     return dict1
  26.  
  27.  
  28. #def bot_start(tokenid,chatid):
  29. #    global update_id
  30. #    global bot
  31. #    bot = telegram.Bot(tokenid)
  32.     # get the first pending update_id, this is so we can skip over it in case
  33.     # we get an "Unauthorized" exception.
  34. #    try:
  35. #        update_id = bot.get_updates()[0].update_id
  36. #    except IndexError:
  37. #        update_id = None
  38. #    bot_message("FreePBX bot starts!",chatid)
  39. #    bot_message("second message!",chatid)
  40.  
  41.  
  42. def bot_message(MessageText):
  43.     bot = telegram.Bot(telegram_settings["token"])
  44.     try:
  45.         update_id = bot.get_updates()[0].update_id
  46.     except IndexError:
  47.         update_id = None
  48.  
  49.     bot.send_message(chat_id=telegram_settings["chat_id"], text=MessageText)
  50.     print("debug test message=",MessageText)
  51.  
  52. def bot_voice_message(VoiceMessage,ChatID):
  53.     bot.send_voice(chat_id=ChatID, voice=VoiceMessage)
  54.  
  55.  
  56. def connection_worker(conn,addr):
  57.     returndata = b''
  58.     while True:
  59.         data = conn.recv(1024)
  60.         if not data:
  61.             break
  62.         returndata = returndata+data
  63.     filename=strftime("%Y_%m_%d____%H_%M_%S", gmtime()) + '.wav'
  64.     wavfile = open(filename,'wb')
  65.     #try to un-pickle touple, and use their elements
  66.     try:
  67.         touple_pickled=pickle.loads(returndata)
  68.         wavfile.write(touple_pickled[0])
  69.         print(touple_pickled[1])
  70.         bot_message(touple_pickled[1])
  71.         bot_message(filename)
  72.     except:
  73.         pass
  74.  
  75.  
  76.  
  77. def workers_create(sock):
  78.     while True:
  79.         conn,addr = sock.accept()
  80.         print('connected:', addr)
  81.         worker = threading.Thread(target=connection_worker,args=(conn,addr))
  82.         worker.start()
  83.  
  84.  
  85.  
  86.  
  87. def main():
  88.     #bind to socket
  89.     sock = socket.socket()
  90.  
  91.     try:
  92.         sock.bind((network_settings["bind_ip"],int(network_settings["bind_port"])))
  93.         sock.listen(10)
  94.     except:
  95.         print("cant bind to socket!")
  96.         sys.exit(1)
  97.     #using dedicated thread, run worker creator
  98.     #bot_start(telegram_settings['token'],telegram_settings['chat_id'])
  99.     bot_message("testmessage from main!")
  100.     worker_creator_thread = threading.Thread(target=workers_create(sock))
  101.     worker_creator_thread.start()
  102.  
  103.  
  104.  
  105. #use ini config to determinate Network settings
  106. config = configparser.ConfigParser()
  107.  
  108. try:
  109.     config.read('freepbxbot.ini')
  110.     network_settings = ConfigSectionMap('Network')
  111.     telegram_settings = ConfigSectionMap('Telegram')
  112.  
  113. except:
  114.     print('cant read config!')
  115.     sys.exit(1)
  116.  
  117. if __name__ == "__main__":
  118.     main()
  119.  
  120. ======
  121.  
  122. Running a server, and I receive a test message in telegram:
  123.  
  124. python3 test_server.py
  125. debug test message= testmessage from main!
  126.  
  127. ======
  128.  
  129. running a client, server's output:
  130.  
  131. connected: ('127.0.0.1', 46832)
  132. Content-Type: text/plain; charset=UTF-8
  133. Content-Transfer-Encoding: 8bit
  134.  
  135. ***,
  136.  
  137. There is a new voicemail in mailbox 301:
  138.  
  139.        From:   "***" <***>
  140.        Length: 0:03 seconds
  141.        Date:   Thursday, June 14, 2018 at 05:39:02 PM
  142.  
  143. Dial *98 to access your voicemail by phone.
  144.  
  145. ====
  146.  
  147. And nothing in telegram
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement