1+ import time
2+ import random
3+
4+ class AutoCaller :
5+ def __init__ (self , target_number ):
6+ self .target = target_number
7+ self .call_count = 0
8+ self .is_running = True
9+
10+ def make_call (self ):
11+ """模拟拨打电话(实际需对接真实电话API)"""
12+ self .call_count += 1
13+ print (f"\n { '=' * 50 } " )
14+ print (f"📞 第 { self .call_count } 次拨打: { self .target } " )
15+ print (f"⏰ 时间: { time .strftime ('%Y-%m-%d %H:%M:%S' )} " )
16+
17+ # 拨号中
18+ print ("📞 拨号中..." )
19+ time .sleep (1 )
20+
21+ # 振铃中
22+ print ("🔔 振铃中..." )
23+ time .sleep (2 )
24+
25+ # 随机模拟对方行为(实际使用时替换为真实通话状态检测)
26+ # 这里用随机模拟,真实场景需要对接电话API
27+ actions = ['answer' , 'hangup' , 'busy' , 'no_answer' ]
28+ weights = [0.2 , 0.5 , 0.2 , 0.1 ] # 20%接听, 50%挂断, 20%忙线, 10%无人接听
29+ result = random .choices (actions , weights = weights )[0 ]
30+
31+ if result == 'answer' :
32+ print ("💬 对方接听!通话中..." )
33+ duration = random .randint (5 , 30 )
34+ time .sleep (duration )
35+ print (f"✅ 通话结束 (通话时长: { duration } 秒)" )
36+ print ("🎉 通话成功完成,停止重拨" )
37+ self .is_running = False
38+ return 'answered'
39+
40+ elif result == 'hangup' :
41+ print ("❌ 对方挂断" )
42+ return 'hangup'
43+
44+ elif result == 'busy' :
45+ print ("📵 对方忙线" )
46+ return 'busy'
47+
48+ else :
49+ print ("⏰ 无人接听" )
50+ return 'no_answer'
51+
52+ def start (self , interval = 30 , max_attempts = None ):
53+ """开始自动重拨"""
54+ print ("=" * 50 )
55+ print ("🚀 自动重拨已启动" )
56+ print (f"📱 目标号码: { self .target } " )
57+ print (f"⏱️ 重拨间隔: { interval } 秒" )
58+ print (f"🔁 最大次数: { '无限' if max_attempts is None else max_attempts } " )
59+ print ("=" * 50 )
60+
61+ while self .is_running :
62+ if max_attempts and self .call_count >= max_attempts :
63+ print (f"\n ⚠️ 已达到最大重拨次数 ({ max_attempts } ),停止重拨" )
64+ break
65+
66+ result = self .make_call ()
67+
68+ if result == 'answered' :
69+ break
70+
71+ if self .is_running :
72+ print (f"\n ⏳ 等待 { interval } 秒后自动重拨..." )
73+ for i in range (interval , 0 , - 1 ):
74+ print (f" 剩余 { i } 秒" , end = '\r ' )
75+ time .sleep (1 )
76+ print ("\n " + " " * 20 + "\n " )
77+
78+ if __name__ == "__main__" :
79+ print ("=" * 40 )
80+ print ("📞 自动重拨系统" )
81+ print ("=" * 40 )
82+
83+ # 直接在这里填写要拨打的手机号
84+ phone = "17751377766" # 👈 改成你要拨打的号码
85+
86+ interval = 10 # 挂断后等待10秒重拨
87+ max_attempts = 5 # 最多重拨5次,None表示无限
88+
89+ print (f"目标号码: { phone } " )
90+ print (f"重拨间隔: { interval } 秒" )
91+ print (f"最大重拨: { max_attempts } 次" )
92+ print ("-" * 40 )
93+
94+ caller = AutoCaller (phone )
95+ try :
96+ caller .start (interval = interval , max_attempts = max_attempts )
97+ except KeyboardInterrupt :
98+ print ("\n \n ⚠️ 用户手动停止" )
0 commit comments