Home > OS >  WeChat robot
WeChat robot

Time:02-26

 
# - * - coding: utf-8 - * -

The import wechat
The import json
The import time
The from wechat import WeChatManager, MessageType
The import getweather
The import requests
The import urllib

Wechat_manager=WeChatManager (libs_path='AI/WechatBot wechat_pc_api/libs')


# test function callback here
@ wechat. CONNECT_CALLBACK (in_class=False)
Def on_connect (client_id) :
Print (' [on_connect] client_id: {0} '. The format (client_id))


@ wechat. RECV_CALLBACK (in_class=False)
Def on_recv (client_id, message_type message_data) :
Try:
Def qingyunke (MSG) :
Url="http://api.qingyunke.com/api.php? Key=free& Appid=0 & amp; MSG={} 'format (urllib. Parse. Quote (MSG))
HTML=requests. Get (url)
Return HTML, json () (" content ")

MSG=message_data [' MSG ']
To_wxid=message_data [' to_wxid]
From_wxid=message_data [' from_wxid]
Print (' [on_recv] client_id: {0}, message_type: {1}, message: {2}, user name: - {3} '. The format (client_id,
Message_type, MSG, to_wxid))
If the MSG [0]=='"' :
Print ((MSG. Find (' today ')!=1) and (MSG), find (' weather ')!=1) and (MSG), find (' Beijing ')==1))
If (MSG. Find (' today ')!=1) and (MSG), find (' weather ')!=1) and (MSG), find (' Beijing ')==1) :
Msg_list=list (MSG)
Msg_list. Insert (3, 'Beijing')
True_msg="'. Join (msg_list)
Print (true_msg)
Print (from_wxid)
If to_wxid 'chatroom in:
Wechat_manager. Send_text (client_id, to_wxid qingyunke (MSG) replace (' & gt; ', ' ', 1)). The replace (' {br} ', '\ n'))
The else:
Wechat_manager. Send_text (client_id, from_wxid qingyunke (MSG) replace (' & gt; ', ' ', 1)). The replace (' {br} ', '\ n'))
The else:
Wechat_manager. Send_text (client_id, from_wxid qingyunke (MSG) replace (' & gt; ', ' ', 1)). The replace (' {br} ', '\ n'))
Except the Exception as e:
Print (e)


@ wechat. CLOSE_CALLBACK (in_class=False)
Def on_close (client_id) :
Print (' [on_close] client_id: {0} '. The format (client_id))

The class LoginTipBot (wechat. CallbackHandler) :

@ wechat. RECV_CALLBACK (in_class=True)
Def on_message (self, client_id, message_type message_data) :
# judgment after a successful login, assistant to file a clockwork news
If message_type==MessageType. MT_USER_LOGIN:
Time. Sleep (2)
Wechat_manager. Send_text (client_id 'filehelper', 'test message')

Wechat_manager. Send_link (client_id,
'filehelper'
'wechat_pc_api projects'
'WeChatPc robot project',
'https://github.com/smallevilbeast/wechat_pc_api',
'https://www.showdoc.com.cn/server/api/attachment/visitfile/sign/0203e82433363e5ff9c6aa88aa9f1bbe? Showdoc=. JPG) ')



If __name__=="__main__" :
Bot=LoginTipBot ()

# to add a callback instance objects
Wechat_manager. Add_callback_handler (bot)
Wechat_manager. Manager_wechat (smart=True)

# blocking the main thread
While True:
Time. Sleep (0.5)

CodePudding user response:

  • Related