def text_reply(msg):
if is_at_me(msg.ToUserName):
if users[msg.FromUserName] == FriendStatus.UNKNOWN:
if check_trigger_words(msg.Content):
users[msg.FromUserName] = FriendStatus.SELF_INTRO
itchat.send_msg(msg=FIRST_REPLY % (msg.User.NickName, GROUP_NAME), toUserName=msg.FromUserName)
elif users[msg.FromUserName] == FriendStatus.SELF_INTRO:
if check_self_intro(msg.Content):
users[msg.FromUserName] = FriendStatus.INVITE_SENT
send_group_invitation(msg)
intro_upcomming_member(msg.User.NickName, msg.Content)
itchat.send_msg(msg=GOODBYE % (msg.User.NickName, GROUP_NAME), toUserName=msg.FromUserName)
else:
itchat.send_msg(msg=INTRO_FAILED, toUserName=msg.FromUserName)
elif users[msg.FromUserName] == FriendStatus.INVITE_SENT and check_trigger_words(msg.Content):
# the user sent trigger words again but the invitation already sent
send_group_invitation(msg)
itchat.send_msg(msg=GOODBYE % (msg.User.NickName, GROUP_NAME), toUserName=msg.FromUserName)
python类send_msg()的实例源码
def mainloop():
global user
global wefriendnc
global weuser
global role
global wait
global z
global userlist
#???[?????--?????--????????]
#??????????????????
#??????/???????????random?sleep
#???????/????????????ending???120?????????????
print(wefriendnc)
for x in friendnc:
print(x)
itchat.send_msg(str(x)+"?"+friendnc[x],toUserName=groupchatmain)
itchat.send_msg(str(x)+'?'+friendnc[x],toUserName=groupchatlangren)
itchat.send_msg('???????????',toUserName=groupchatmain)
itchat.send_msg('????????????????????id?',toUserName=groupchatlangren)
wait='langren'
def start(location=LOCATION, report_time=REPORT_TIME):
if datetime.datetime.now().strftime('%H:%M') == report_time: #?? 03:25
weather = ''
count = 0
while not weather:
weather = get_weather(location)
count += 1
print('??????%d?' % count)
time.sleep(1)
if count >= 10:
break
if not weather:
print('????????')
else:
#???????
for USER_NAME in USER_NAME_LIST:
num = itchat.search_friends(name = USER_NAME)[0]["UserName"]
try:
itchat.send_msg(get_weather(location), num)
except Exception as e:
print(e)
#print(get_weather(location))
def send_msgs():
with sqlite3.connect('events.db') as db:
cur=db.cursor()
cur.execute('select msgid,content from push_msgs')
res=cur.fetchall()
for msgid,content in res:
if itchat.send_msg(content,toUserName=group_name):
cur=db.cursor()
cur.execute('delete from push_msgs where msgid=?',[msgid])
db.commit()
print(' -> sent msg:',content)
else:
if msgid not in logged_errors:
print('!!! send failed:',content)
log('error','?????????#%d??%s'%(msgid,content))
logged_errors.add(msgid)
time.sleep(.5)
def intro_upcomming_member(user: str, intro: str) -> None:
chatroom_id = itchat.search_chatrooms(name=GROUP_NAME)[0].UserName
itchat.send_msg(msg=GROUP_INTRO % (user, intro), toUserName=chatroom_id)
def send_msg(msg='Test Message', to_user_name=None):
''' send plain text message
for options
- msg: should be unicode if there's non-ascii words in msg
- to_user_name: 'user_name' key of friend dict
it is defined in components/messages.py
'''
return itchat.send_msg(msg, to_user_name)
def send_text(text):
#send text msgs to 'filehelper'
#???????????
try:
itchat.send_msg(msg=text,toUserName='filehelper')
return
except (ConnectionError,NotImplementedError,KeyError):
traceback.print_exc()
print('\nConection error,failed to send the message!\n')
return
else:
return
def t_send(self,msg,toUserName='filehelper'):
try:
itchat.send_msg(msg=msg,toUserName=toUserName)
return
except (ConnectionError,NotImplementedError,KeyError):
traceback.print_exc()
print('\nConection error,failed to send the message!\n')
return
else:
return
def add_friend(msg):
if msg['RecommendInfo']['Content'] == 'Algorithm':
msg.user.verify()
itchat.send_msg('?????????????', msg['RecommendInfo']['UserName'])
def ending(winner):
global user
global weuser
global role
global userlist
global groupchatlangren
global groupchatmain
itchat.send_msg('?????????%s\n?????????'%winner,toUserName=groupchatmain)
itchat.send_msg('?????????%s\n?????????'%winner,toUserName=groupchatlangren)
userDict = []
#???
global idnow
global roles
global ifgame
global langrenamout
global cunminamout
global yuyanjia
global cunmin
global langren
global nvwu
idnow=0
ifgame=False
user={}
weuser={}
role={}
userlist=[]
ifchosen=False
groupchatmain=''
groupchatlangren=''
langren=[]
cunmin=[]
nvwu=''
yuyanjia=''
roles=['langren','nvwu','yuyanjia','cunmin']
#??????+?????
def goon():
itchat.send_msg('??????????????????',toUserName=groupchatmain)
if int(nvwu) in dead:
time.sleep(10)
itchat.send_msg('???????????10????',toUserName=user[nvwu])
else:
wait='nvwu'
itchat.send_msg('????%s??????id???????/??',toUserName=user[nvwu])
def nvwugoon():
itchat.send_msg('????????????',toUserName=groupchatmain)
itchat.send_msg('??????',toUserName=groupchatmain)
if int(yuyanjia) in dead:
time.sleep(10)
itchat.send_msg('????????',toUserName=user[yuyanjia])
else:
wait='yuyanjia'
itchat.send_msg('????????id?',toUserName=user[yuyanjia])
def yuyanjiagoon():
wait=''
itchat.send_msg('????????????????',toUserName=groupchatmain)
if save=='':
save='???'
if die=='':
die='???'
itchat.send_msg('??%s????%s??'%die,save)
if save!='???':
itchat.send_msg('??180????????????????????',toUserName=groupchatmain)
wait='toupiao'
time.sleep(180)
wait=''
toupiaoed=[]
x=get_mode(toupiao)
if x==None:
itchat.send_msg('??????????????????',toUserName=groupchatmain)
elif len(x)>1:
y=random.choice(x)
dead.append(y)
itchat.send_msg('%s???'%y,toUserName=groupchatmain)
elif len(x)==1:
itchat.send_msg('%s???'%x[0],toUserName=groupchatmain)
dead.append(x[0])
dlr=0
for lr in langren:
if lr in dead:
dlr=dlr+1
if dlr==len(langren):
ending('??')
for cm in cunmin:
if cm in dead:
dcm=dcm+1
if nvwu in dead:
dcm=dcm+1
if yuyanjia in dead:
dcm=dcm+1
if dcm==len(cunmin)+2:
ending('??')
mainloop()
def sendmsg(msg,name):
itchat.send_msg(msg,toUserName=Name2Wx[name])
def group(people=None):
if people==None:
print('Err')
return False
global groupchatmain
userDict=[]
for userlist in people:
userDict.append({"UserName":userlist})
gctmp=itchat.create_chatroom(userDict, '???Beta')
groupchatmain=gctmp['ChatRoomName']
itchat.send_msg('???????',toUserName=groupchatmain)
time.sleep(0.1)
itchat.send_msg('??????????...',toUserName=groupchatmain)
def langrengroup(people=None):
global groupchatlangren
if people==None:
print('Err')
return False
userDict=[]
for userlist in people:
userDict.append({"UserName":userlist})
gctmp=itchat.create_chatroom(userDict, '???-???Beta')
groupchatlangren=gctmp['ChatRoomName']
itchat.send_msg('????????,???????????\n???????????????????.',toUserName=groupchatmain)
def send2group(msg):
itchat.send_msg(msg,toUserName=groupchatmain)
def send2langren(msg):
itchat.send_msg(msg,toUserName=groupchatlangren)
def add_friend(msg, status=3):
itchat.add_friend(**msg['Text']) # ????????????????????????
time.sleep(5)
itchat.send_msg(u'????????', msg['RecommendInfo']['UserName'])
def fri_reply(msg):
itchat.add_friend(**msg['Text'])
itchat.send_msg('??????????', msg['RecommendInfo']['UserName'])
def task_remind():
for task in task_list:
task_time = task[0]
task_content = task[1]
if datetime.datetime.now().strftime("%H:%M") == task_time:
#name=????
receiver = itchat.search_friends(name=u"Hoder")[0]["UserName"]
itchat.send_msg(task_content, receiver)
def custom_push(msg):
cmd,_,content=msg['Content'].partition(' ')
if cmd.lower()=='send':
return '??' if itchat.send_msg(content,toUserName=group_name) else '??'
def add_friend(msg):
logging.info('add friend {}, message {}'.format(msg['RecommendInfo']['UserName'], msg['Text']))
itchat.add_friend(**msg['Text'])
itchat.send_msg(faq.replay_welcome(), msg['RecommendInfo']['UserName'])
def auto_reply_map(msg):
try:
loc = msg["Content"].split(":")
if loc is not None:
loc = loc[0]
itchat.send_msg(msg="????????%s????" % loc, toUserName=msg['FromUserName'])
resp = api.ask(loc + "???")
if resp is not None:
itchat.send_msg(msg=resp['text'], toUserName=msg['FromUserName'])
else:
return "???????"
except Exception as e:
print(str(e))