From 962cf3ede602751040dd87c3b6fa6cd5099630a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacques=20von=20H=C3=A4msterviel?= <59958130+JacquesVonHamsterviel@users.noreply.github.com> Date: Thu, 10 Nov 2022 07:28:59 +0000 Subject: [PATCH] 20221110a 20221110a --- config/{id.ini => default.ini} | 110 +++++++++++------------ main.py | 156 +++++++++++++++++++++++++++------ telegram.py | 30 +++++++ 3 files changed, 216 insertions(+), 80 deletions(-) rename config/{id.ini => default.ini} (95%) create mode 100644 telegram.py diff --git a/config/id.ini b/config/default.ini similarity index 95% rename from config/id.ini rename to config/default.ini index 970d5a4..29c2bb0 100644 --- a/config/id.ini +++ b/config/default.ini @@ -1,55 +1,55 @@ -; 配置文件 -; pip install pyserial -; pip install telepot -; pip install phone #归属地查询 -; https://api.telegram.org/bot+id/getupdates - -[main] -;手机号,不填不影响,在多开的情况下,填了方便区分。 -current_phonenum=13512345678 -;错误次数上限 -max_error_count=100 - -[port] -;串口号 -serialPort=COM7 -;波特率 -baudRate=115200 -;X秒未刷新,强制重新连接串口 -schedule_reconnect_max=20.0 - -[sms] -;允许发送短信吗 -sms_send_allow=True -;开启11位来电自动回复 -sms_auto_send=False -;自动回复短信的数量上限 -sms_limit=1 -;自动回复的内容 -sms_auto_send_content=你好,有事情请联系。 - - -[log] -;是否在本地消息中显示本机号码 -current_phonenum_log=True - -[telegram] -;启用Telegram发送 -tg_send_enable=True -;启用Telegram接收 (新建线程)不建议开启,国内网络及其不稳定,除了专线。 -tg_receive_enable=False -;bot参数 -bot=xxxxxx:xxxxxxxxxx -;TG的群组 -tg_chat_id=-xxx -;是否在Telegram消息中显示本机号码 -current_phonenum_tg=True -;Telegram消息识别频率,单位为秒,不能小于5秒,否则默认为5秒 -tg_read_period=60 -;最新读取的信息ID -tg_last_read_id=997629083 - -[common_input] -;通用输入功能,通过运行目录下的common_input.txt 实现,仅读取第一行的命令,超出部分会删除,流程完毕会返回free在第一行,或者error和第二行错误信息。 -common_input_function=True - +; 配置文件 +; pip install pyserial +; pip install telepot +; pip install phone #归属地查询 +; https://api.telegram.org/bot+id/getupdates + +[main] +;手机号,不填不影响,在多开的情况下,填了方便区分。 +current_phonenum=13512345678 +;错误次数上限 +max_error_count=100 + +[port] +;串口号 +serialPort=COM7 +;波特率 +baudRate=115200 +;X秒未刷新,强制重新连接串口 +schedule_reconnect_max=20.0 + +[sms] +;允许发送短信吗 +sms_send_allow=True +;开启11位来电自动回复 +sms_auto_send=False +;自动回复短信的数量上限 +sms_limit=1 +;自动回复的内容 +sms_auto_send_content=你好,有事情请联系。 + + +[log] +;是否在本地消息中显示本机号码 +current_phonenum_log=True + +[telegram] +;启用Telegram发送 +tg_send_enable=True +;启用Telegram接收 (新建线程)不建议开启,国内网络及其不稳定,除了专线。 +tg_receive_enable=False +;bot参数 +bot=xxxxxx:xxxxxxxxxx +;TG的群组 +tg_chat_id=-xxx +;是否在Telegram消息中显示本机号码 +current_phonenum_tg=True +;Telegram消息识别频率,单位为秒,不能小于5秒,否则默认为5秒 +tg_read_period=60 +;最新读取的信息ID +tg_last_read_id=997629083 + +[common_input] +;通用输入功能,通过运行目录下的common_input.txt 实现,仅读取第一行的命令,超出部分会删除,流程完毕会返回free在第一行,或者error和第二行错误信息。 +common_input_function=True + diff --git a/main.py b/main.py index b9c76d5..3e153e3 100644 --- a/main.py +++ b/main.py @@ -86,7 +86,7 @@ def tg_send(tg_type,tg_str): global tg_send_enable if tg_send_enable==True: try: - bot.sendMessage(chat_id=tg_chat_id, text="銆恵}銆憑} {}".format(str(tg_type),standard_time(),tg_str)) + bot.sendMessage(chat_id=tg_chat_id,text="銆恵}銆憑} {}".format(str(tg_type),standard_time(),tg_str)) log("LOG_TG","銆恵}銆戙恵}銆憑}".format(tg_type,standard_time(),tg_str)) return True except Exception as ex: @@ -274,6 +274,15 @@ def common_input(strx): finally: time.sleep(3) +def call(num): + ser.write('atd{};'.format(str(num)).encode('utf-8') + b'\r\n') + +def call10086(): + log("CALL","Start To Call 10086") + ser.write('atd10086;'.encode('utf-8') + b'\r\n') + time.timeout(20) + log("CALL","Stop Communication with 10086") + #鍐呯疆甯搁噺 en_uni=(" ","!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~") @@ -284,39 +293,131 @@ def common_input(strx): schedule_reconnect=time.time() #鍒濆鍖栨椂闂 error_count=1 #鍒濆閿欒娆℃暟鍊 tg_last_read_id=0 +flag_return=False +version="2022-06-27" #璇诲彇閰嶇疆鏂囦欢 -try: +try: if len(sys.argv)<=1: - print("缂哄皯鍙傛暟锛屽弬鏁版槸閰嶇疆鏂囦欢锛堝繀濉級") + print("Argument Required! Use 'help' to get help") + flag_return=True sys.exit() + #elif len(sys.argv)>2: + # print("Too Many Arguments! Use 'help' to get help") + # flag_return=True + # sys.exit() cf=configparser.ConfigParser() cf.read(sys.argv[1]) - current_phonenum=cf.get('main','current_phonenum') - max_error_count=cf.getint('main','max_error_count') + if sys.argv[1]=='debug-serial': + print("mode: debug-serial") + serialPort=input("Port: ") + baudRate=115200 + ser=serial.Serial(serialPort,baudRate,timeout=0.5) + print("鍙傛暟璁剧疆锛氫覆鍙=%s 锛屾尝鐗圭巼=%d, Use ## to send exit signal"%(serialPort,baudRate))#杈撳嚭涓插彛鍙峰拰娉㈢壒鐜 + while True: + try: + res=ser.readline() + if res==b'': + time.sleep(0.1) + i_input=input("Command: ") + if i_input=="##": + command_variable = chr(26) + ser.write(command_variable.encode('utf-8')) + else: + ser.write(i_input.encode('utf-8') + b'\r\n') + else: + print(res) + i_input=input("Command: ") + if i_input=="##": + command_variable = chr(26) + ser.write(command_variable.encode('utf-8')) + else: + ser.write(i_input.encode('utf-8') + b'\r\n') + + except KeyboardInterrupt: + break + + #except Exception as ex: + # print(ex) + # pass + + except: + print("銆怢og銆慡omething else went wrong") + + ser.close() + flag_return=True + sys.exit() + elif sys.argv[1]=='phoneinfo': + print("mode: phoneinfo") + findphone = Phone() + print(phoneinfo(input("PhoneNum: "))) + flag_return=True + sys.exit() + elif sys.argv[1]=='help': + print("Help\n1. debug-serial\n2. phoneinfo\n3. ver\nElse: Seen as path of config file") + flag_return=True + sys.exit() + elif sys.argv[1]=='ver': + print("Py_AT\nVersion: {}".format(version)) + flag_return=True + sys.exit() + else: + current_phonenum=cf.get('main','current_phonenum') + max_error_count=cf.getint('main','max_error_count') - serialPort=cf.get('port','serialPort') - baudRate=cf.getint('port','baudRate') - schedule_reconnect_max=cf.getfloat('port','schedule_reconnect_max') + serialPort=cf.get('port','serialPort') + baudRate=cf.getint('port','baudRate') + schedule_reconnect_max=cf.getfloat('port','schedule_reconnect_max') - sms_send_allow=cf.getboolean('sms','sms_send_allow') - sms_auto_send=cf.getboolean('sms','sms_auto_send') - sms_limit=cf.getint('sms','sms_limit') - sms_auto_send_content=cf.get('sms','sms_auto_send_content') + sms_send_allow=cf.getboolean('sms','sms_send_allow') + sms_auto_send=cf.getboolean('sms','sms_auto_send') + sms_limit=cf.getint('sms','sms_limit') + sms_auto_send_content=cf.get('sms','sms_auto_send_content') - current_phonenum_log=cf.getboolean('log','current_phonenum_log') + current_phonenum_log=cf.getboolean('log','current_phonenum_log') - tg_send_enable=cf.getboolean('telegram','tg_send_enable') - tg_receive_enable=cf.getboolean('telegram','tg_receive_enable') - bot = telepot.Bot(cf.get('telegram','bot')) - tg_chat_id=cf.get('telegram','tg_chat_id') - current_phonenum_tg=cf.getboolean('telegram','current_phonenum_tg') - tg_read_period=cf.getint('telegram','tg_read_period') - tg_last_read_id=cf.getint('telegram','tg_last_read_id') - common_input_function=cf.getboolean('common_input','common_input_function') + tg_send_enable=cf.getboolean('telegram','tg_send_enable') + tg_receive_enable=cf.getboolean('telegram','tg_receive_enable') + bot = telepot.Bot(cf.get('telegram','bot')) + if cf.getboolean('proxy','enable_proxy')==True: + if cf.getboolean('proxy','auth')==True: + telepot.api.set_proxy(cf.get('proxy','url'), basic_auth=(cf.get('proxy','username'),cf.get('proxy','password'))) + else: + telepot.api.set_proxy(cf.get('proxy','url'), basic_auth=None) + tg_chat_id=cf.get('telegram','tg_chat_id') + current_phonenum_tg=cf.getboolean('telegram','current_phonenum_tg') + tg_read_period=cf.getint('telegram','tg_read_period') + tg_last_read_id=cf.getint('telegram','tg_last_read_id') + common_input_function=cf.getboolean('common_input','common_input_function') except: - print('璇诲彇閰嶇疆鏂囦欢鍑洪敊锛') - sys.exit() + if flag_return==True: + sys.exit() + elif sys.argv[1]=='telegram': + if len(sys.argv)<3: + print("Config File Required!") + sys.exit() + else: + cf=configparser.ConfigParser() + cf.read(sys.argv[2]) + tg_send_enable=cf.getboolean('telegram','tg_send_enable') + tg_receive_enable=cf.getboolean('telegram','tg_receive_enable') + bot = telepot.Bot(cf.get('telegram','bot')) + if cf.getboolean('proxy','enable_proxy')==True: + if cf.getboolean('proxy','auth')==True: + telepot.api.set_proxy(cf.get('proxy','url'), basic_auth=(cf.get('proxy','username'),cf.get('proxy','password'))) + else: + telepot.api.set_proxy(cf.get('proxy','url'), basic_auth=None) + tg_chat_id=cf.get('telegram','tg_chat_id') + current_phonenum_tg=cf.getboolean('telegram','current_phonenum_tg') + tg_read_period=cf.getint('telegram','tg_read_period') + tg_last_read_id=cf.getint('telegram','tg_last_read_id') + while True: + tg_send("绯荤粺",input("鍙戦佹秷鎭細 ")) + sys.exit() + else: + print('璇诲彇閰嶇疆鏂囦欢鍑洪敊锛') + sys.exit() + ser=serial.Serial(serialPort,baudRate,timeout=0.5) log("LOG","Program Starts") @@ -357,7 +458,12 @@ def common_input(strx): #寮濮嬭鍙栫煭淇 re_phonenum=re.compile(r'\d{5,}') re_time=re.compile(r'\d{1,2}/\d{1,2}/\d{1,2},\d{1,2}:\d{1,2}:\d{1,2}') - msg_phonenum=re_phonenum.search(str(res))[0] + + if len(re_phonenum.search(str(res))[0])>24: + msg_phonenum=DecodeUnicode(re_phonenum.search(str(res))[0]) + else: + msg_phonenum=re_phonenum.search(str(res))[0] + msg_time=re_time.search(str(res))[0] msg_in_receiving=True continue @@ -434,4 +540,4 @@ def common_input(strx): break else: error_count+=1 -ser.close() +ser.close() \ No newline at end of file diff --git a/telegram.py b/telegram.py new file mode 100644 index 0000000..783d1d2 --- /dev/null +++ b/telegram.py @@ -0,0 +1,30 @@ +import requests + +class tg_bot(object): + def __init__(self, tg_api_base_link,bot_id): + self.tg_api_base_link = tg_api_base_link + self.bot_id=bot_id + def getUpdates(self): + error=0 + while True: + if error>5: + return(False,"Too Many Errors") + try: + r=requests.get("{}/bot{}/getUpdates".format(self.tg_api_base_link,self.bot_id),timeout=5) + return (True,r.content.decode("utf-8")) + except Exception as ex: + error+=1 + print(ex) + time.sleep(0.5) + def sendMessage(self,chat_id,text): + error=0 + while True: + if error>5: + return(False,"Too Many Errors") + try: + r=requests.get("{}/bot{}/sendMessage?chat_id={}&text={}".format(self.tg_api_base_link,self.bot_id,chat_id,text),timeout=5) + return r.content.decode("utf-8") + except Exception as ex: + error+=1 + print(ex) + time.sleep(0.5)