This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
487 lines (448 loc) · 24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# -*- coding: utf-8 -*-
import sys
import configparser
import re
import time
from _thread import *
from urllib.parse import MAX_CACHE_SIZE
import serial
import idna
import traceback
from functions.telegram import *
from functions.standard_time import *
from functions.unicode import *
from functions.phoneinfo import *
from functions.output import *
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))
log("LOG_TG","【{}】【{}】{}".format(tg_type,standard_time(),tg_str))
return True
except Exception as ex:
log("LOG_TG","【ERROR】{}".format(str(ex)))
return False
else:
log("LOG_TG","【Error】Telegram send not enable")
return False
def at_initialize():
log("LOG","AT_initializing...")
ser.write('AT'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #No Response without this
ser.write('ATE0'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #echo off
ser.write('AT+CPIN?'.encode('utf-8') + b'\r\n') #SIM Card In?
ser.write('AT+CLIP=1'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #显示来电号码,如果没有这条指令,则来电话模块只送出ring,不送出号码
ser.write('AT+CMGF=1'.encode('utf-8') + b'\r\n')
#ser.write('AT+CIMI'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #读取IMSI
#ser.write('AT+CCID'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #读取ICCID号
#ser.write('AT+CPBS="ON"'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #将电话存贮位置选择为本机号码列表
#ser.write('AT+CPBW=1'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #储存本机号码
#ser.write('AT+CNUM'.encode('utf-8') + b'\r\n');time.sleep(1);print(ser.readline()) #读取本机号码
return True
def at_send_en_message(phonenum,text):
global msg_in_sending
if sms_send_allow==True:
if msg_in_sending==False:
for i in text or "\n"in text:
if i in en_uni:
pass
else:
return((False,"NOT_EN"))
msg_in_sending=True
ser.write('AT+CMGF=1'.encode('utf-8') + b'\r\n')
ser.write('AT+CMGS="{}"'.format(phonenum).encode('utf-8') + b'\r\n')
ser.write(b'\r\n')
time.sleep(0.1)
ser.write(text.encode('utf-8') + b'\r\n')
time.sleep(0.2)
command_variable = chr(26)
ser.write(command_variable.encode('utf-8'))
log("SMS","MESSAGE SENT TO {}, Content:{}".format(phonenum,text))
msg_in_sending=False
return((True,""))
else:
return((False,"目前有消息在发送中"))
else:
return((False,"sms_send_allow=False,全局消息发生被禁用!"))
def at_send_cn_message(phonenum,text):
global msg_in_sending
if sms_send_allow==True:
if msg_in_sending==False:
msg_in_sending=True
ser.write('AT+CMGF=1'.encode('utf-8') + b'\r\n')
time.sleep(0.1)
ser.write('AT+CSCS="UCS2"'.encode('utf-8') + b'\r\n')
time.sleep(0.1)
ser.write('AT+CSMP=17,71,0,8'.encode('utf-8') + b'\r\n')
time.sleep(0.1)
ser.write('AT+CMGS="{}"'.format(EncodeUnicode(phonenum)).encode('utf-8') + b'\r\n')
time.sleep(0.1)
ser.write(EncodeUnicode(text).encode('utf-8') + b'\r\n')
time.sleep(0.2)
command_variable = chr(26)
ser.write(command_variable.encode('utf-8'))
log("SMS","MESSAGE SENT TO {}, Content:{}".format(phonenum,text))
msg_in_sending=False
tg_send("SMS","MESSAGE SENT TO {}, Content:{}".format(phonenum,text))
return((True,""))
else:
return((False,"目前有消息在发送中"))
else:
return((False,"sms_send_allow=False,全局消息发生被禁用!"))
def read_tg_message(strx):
global tg_last_read_id
global tg_read_period
global error_count
global max_error_count
if tg_read_period<5:
tg_read_period=5
while True:
try:
response = bot.getUpdates(offset=int(tg_last_read_id)+1)
if response==[]:
pass
else:
tg_last_read_id=int(response[0]['update_id'])
if 'channel_post' in response[0]:
if str(response[0]['channel_post']['chat']['id'])==tg_chat_id:
tg_cmd=response[0]['channel_post']['text']
log("LOG","【Telegram】收到消息:"+tg_cmd)
tg_send("命令执行",cmd_exc(tg_cmd))
cf.set("telegram", "tg_last_read_id",tg_last_read_id)
time.sleep(tg_read_period)
except Exception as ex:
log("LOG","【异常处理】{}\n目前累计错误次数:{},错误次数上限:{}".format(str(ex),str(error_count),str(max_error_count)))
tg_send("异常处理","{}\n目前累计错误次数:{},错误次数上限:{}".format(str(ex),str(error_count),str(max_error_count)))
traceback.print_exc()
if error_count>=max_error_count:
break
else:
error_count+=1
def cmd_exc(strcmd):
if len(strcmd)>3:
if strcmd[:6]=="sendcn" and strcmd.count("###")==1:
cmd_exc_phonenum=strcmd[6:strcmd.find("###")]
cmd_exc_text=strcmd[strcmd.find("###")+3:]
if 4<len(cmd_exc_phonenum)<21 and len(cmd_exc_text)!=0 and len(cmd_exc_text)<60 and cmd_exc_phonenum.isdigit()==True:
if sms_send_allow==True:
at_send_cn_message(cmd_exc_phonenum,cmd_exc_text)
return("\n识别到sendcn指令,已发送到:{},内容:{}".format(cmd_exc_phonenum,cmd_exc_text))
else:
return("\n全局短信功能目前为禁用状态!")
else:
return("命令错误。提示:不能发送空文本,或者电话长度不在6到20之间,或不是整数,或者短信太长了。")
elif strcmd[:6]=="senden" and strcmd.count("###")==1:
cmd_exc_phonenum=strcmd[6:strcmd.find("###")]
cmd_exc_text=strcmd[strcmd.find("###")+3:]
if 4<len(cmd_exc_phonenum)<21 and len(cmd_exc_text)!=0 and len(cmd_exc_text)<60 and cmd_exc_phonenum.isdigit()==True:
if sms_send_allow==True:
at_send_en_message(cmd_exc_phonenum,cmd_exc_text)
return("\n识别到senden指令,已发送到:{},内容:{}".format(cmd_exc_phonenum,cmd_exc_text))
else:
return("\n全局短信功能目前为禁用状态!")
else:
return("命令错误。提示:不能发送空文本,或者电话长度不在6到20之间,或不是整数,或者短信太长了。")
elif strcmd=="help":
return("\n【帮助】请按命令标准格式来\n\n1.发送短信:sendcn电话号码###内容 或者 senden电话号码###纯英文内容 \n\n2.归属地查询:phone电话号码\n\n请勿一次性发送过多命令,过多的命令会被加载到任务队列中等待下一个周期执行。\n\n3.AT###相关AT命令,会通过串口发送\n\n当前Telegram消息刷新周期为{}秒,本地CommonInput文件读写需用UFT-8编码".format(str(tg_read_period)))
elif len(strcmd)>4 and strcmd[:5]=="phone" and strcmd.count("phone")==1:
if strcmd[5:].isdigit()==True:
return(str(phoneinfo(strcmd[5:])))
else:
return("phone命令后需加纯数字手机号")
elif "AT###" in strcmd:
ser.write(strcmd[6:].encode('utf-8') + b'\r\n')
return("已执行AT命令:{}".format(strcmd[6:]))
else:
return("无法识别命令,使用help命令获取帮助")
else:
return("无法识别命令,使用help命令获取帮助")
def check_sms_limit(phonenum):
if sms_limit==0:
return((False,"Up To Limit"))
else:
if len(phonenum)==11:
for i in record_sms_phonenum:
if i[0]==phonenum and i[1]<sms_limit:
record_sms_phonenum[record_sms_phonenum.index(i)]=[phonenum,i[1]+1]
return((True,""))
elif i[0]==phonenum and i[1]>=sms_limit:
return((False,"Up To Limit"))
record_sms_phonenum.append([phonenum,1])
return((True,""))
else:
return((False,"Phonenum Length Not 11"))
def phonenum_self(mode): #mode支持参数:enter输出末尾换行符,space末尾为空格。
if current_phonenum!="" and mode=="enter" and current_phonenum_tg==True:
return("接收号码:"+ current_phonenum+"\n")
elif current_phonenum!="" and mode=="space" and current_phonenum_log==True:
return("接收号码:"+ current_phonenum+" ")
else:
return("")
def common_input(strx):
time.sleep(10) #延迟10秒启动
log("LOG","Common_Input模块已启动!")
cmdres=""
while True:
try:
with open("common_input.txt","r",encoding='utf-8') as CIF:
lines=CIF.readlines()
if lines!=[]:
if lines[0]!="free":
if lines[0]!="free\n":
cmdres=cmd_exc(lines[0])
log("LOG","Common_Input收到命令{},执行结果{}".format(lines[0],cmdres))
with open("common_input.txt","w+",encoding='utf-8') as CIF:
CIF.write("free\n"+cmdres)
elif lines==[]:
with open("common_input.txt","w+",encoding='utf-8') as CIF:
CIF.write("free")
except Exception as ex:
print(ex)
log("LOG","Common Imput File Error: " + str(ex))
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","{","|","}","~")
record_sms_phonenum=[] #存储已发送提示消息的手机号,避免重复发送,浪费话费。
msg_in_receiving=False #短信是否完全接收完毕
msg_in_sending=False #短信是否在发送
schedule_reconnect=time.time() #初始化时间
error_count=1 #初始错误次数值
tg_last_read_id=0
flag_return=False
version="2022-06-27"
#读取配置文件
try:
if len(sys.argv)<=1:
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])
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("【Log】Something 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')
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')
tg_send_enable=cf.getboolean('telegram','tg_send_enable')
tg_receive_enable=cf.getboolean('telegram','tg_receive_enable')
bot = tg_bot(cf.get('telegram','tg_api_base_link'),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:
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")
log("LOG","参数设置:串口=%s ,波特率=%d"%(serialPort,baudRate)) #输出串口号和波特率
try:
if tg_receive_enable==True:
start_new_thread( read_tg_message, ("Telegram_bot", ) )
#thread.start_new_thread( print_time, ("Thread-2", 4, ) )
if common_input_function==True:
start_new_thread( common_input, ("Common_input", ) )
except Exception as ex:
print(ex)
at_initialize()
log("LOG","本机号码:"+ current_phonenum)
#print(schedule_reconnect)
while True:
try:
res=ser.readline()
#读取并解析短消息
if res!=b'' and res!=b'\r\n':
schedule_reconnect=time.time()
log("Terminal",str(res))#可以接收中文
if res==b'\x00':
log("LOG","检测到信号断连,重新连接串口")
ser.close()
time.sleep(3)
ser=serial.Serial(serialPort,baudRate,timeout=0.5)
#at_initialize()
log("LOG","检测到信号断连,重新连接串口完成")
if b'+CMTI:' in res:
log("LOG","有短信来了")
ser.write('AT+CMGL'.encode('utf-8') + b'\r\n') #读取内存中所有短消息
if b'+CMGL:' in res: #短消息第一部分-电话号码和时间
#开始读取短信
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}')
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
if msg_in_receiving==True: #短消息第二部分-文本内容
msg_content=DecodeUnicode(str(res)[2:-5])
msg_in_receiving=False
#读取短信完成
#msg_phonenum=DecodeUnicode(msg_phonenum) #这里可能有gbk解码方面的问题,直接在第一步读取
phone_location=phoneinfo(msg_phonenum)
if phone_location[0]=="Error":
log("LOG","来自:{} {}时间:{} 内容:{}".format(msg_phonenum,phonenum_self("space"),standard_time(),msg_content))
tg_send("收到消息","\n来自:{} \n{}时间:{} \n内容:{}".format(msg_phonenum,phonenum_self("enter"),standard_time(),msg_content))
html("SMS","来自:{} {}内容:{}".format(msg_phonenum,phonenum_self("space"),msg_content))
else:
log("LOG","来自:{} ({}) {}时间:{} 内容:{}".format(msg_phonenum,phone_location[1],phonenum_self("space"),standard_time(),msg_content))
tg_send("收到消息","\n来自:{} ({}) \n{}时间:{} \n内容:{}".format(msg_phonenum,phone_location[1],phonenum_self("enter"),standard_time(),msg_content))
html("SMS","来自:{} ({}) {}内容:{}".format(msg_phonenum,phone_location[1],phonenum_self("space"),msg_content))
msg="" #删除内存中所有短消息
ser.write('AT+CMGD=1,2'.encode('utf-8') + b'\r\n')
# 拒绝所有电话
if b'RING\r\n' in res:
ser.write('AT+CLCC'.encode('utf-8') + b'\r\n') #获取来电号码
#time.sleep(1)
ser.write('AT+CHUP'.encode('utf-8') + b'\r\n') #拒绝电话
if b'CLCC' in res:
re_phonenum=re.compile(r'\d{5,}')
phonenum=re_phonenum.search(str(res))[0]
phone_location=phoneinfo(phonenum)
if phone_location[0]=="Error":
log("LOG","【拒绝来电】 \n来自:{} {}".format(phonenum,phonenum_self("space")))
tg_send("拒绝来电","\n来自:{} {}".format(phonenum,phonenum_self("space")))
else:
log("LOG","【拒绝来电】 \n来自:{} ({}) {}".format(phonenum,phone_location[1],phonenum_self("space")))
tg_send("拒绝来电","\n来自:{} ({}) {}".format(phonenum,phone_location[1],phonenum_self("space")))
#向11位的手机号来电发送短信
if b'CLCC' in res:
re_phonenum=re.compile(r'\d{5,}')
send_msg_phonenum=re_phonenum.search(str(res))[0]
if sms_auto_send==True:
return_check_limit=check_sms_limit(send_msg_phonenum)
log("LOG","临时发生短信次数记录:"+str(record_sms_phonenum))
if return_check_limit[0]==True:
return_send_cn_message=at_send_cn_message(send_msg_phonenum,sms_auto_send_content)
if return_send_cn_message[0]==True:
log("LOG","发送自动回复短信, 号码:{}".format(send_msg_phonenum))
else:
log("LOG","发送自动回复短信失败, 原因{},号码:{}".format(return_send_cn_message[1],send_msg_phonenum))
elif return_check_limit[0]==False:
log("LOG","不发送自动回复短信,原因:{}, 号码:{}".format(return_check_limit[1],send_msg_phonenum))
else:
log("LOG","自动发送短信回复来点功能为关闭状态")
else:
if time.time()-schedule_reconnect>schedule_reconnect_max:
ser.close()
#time.sleep(1)
ser=serial.Serial(serialPort,baudRate,timeout=0.5)
#log("LOG","串口自动重连")
schedule_reconnect=time.time()
except KeyboardInterrupt:
log("LOG","KeyboardInterrupt")
break
except Exception as ex:
if "PermissionError" in str(ex) and "Access is denied" in str(ex):
log("LOG","错误信息:SerialException\n详细信息:错误导致程序退出。\n这个错误一般在初次配置的时候出现,一般性是串口配置错误,修改配置即可。\n但是在生产环境中,这是一个严重的错误,正常情况下是几乎不可能面临此错误。模块很有可能被异常移除,或者受到物理损坏,需要手动检查模块和串口连接状态后再启动。")
tg_send("异常处理","SerialException\n详细信息:错误导致程序退出。\n这个错误一般在初次配置的时候出现,一般性是串口配置错误,修改配置即可。\n但是在生产环境中,这是一个严重的错误,正常情况下是几乎不可能面临此错误。模块很有可能被异常移除,或者受到物理损坏,需要手动检查模块和串口连接状态后再启动。")
break
else:
log("LOG","【异常处理】{}\n目前累计错误次数:{},错误次数上限:{}".format(str(ex),str(error_count),str(max_error_count)))
tg_send("异常处理","{}\n目前累计错误次数:{},错误次数上限:{}".format(str(ex),str(error_count),str(max_error_count)))
traceback.print_exc()
if error_count>=max_error_count:
log("LOG","【异常处理】Too Many Errors, auto exit")
break
else:
error_count+=1
ser.close()