# -*- coding: utf-8 -*-
import wechat
import json
import time
from wechat import WeChatManager, MessageType
import getweather
import requests
import urllib
wechat_manager = WeChatManager(libs_path='AI/WechatBot/wechat_pc_api/libs')
# 這里測驗函式回呼
@wechat.CONNECT_CALLBACK(in_class=False)
def on_connect(client_id):
print('[on_connect] client_id: {0}'.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&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}'.format(client_id,
message_type, msg, to_wxid))
if msg[0] == '》':
print((msg.find('今天') != -1) and (msg.find('天氣') != -1) and (msg.find('北京') == -1))
if (msg.find('今天') != -1) and (msg.find('天氣') != -1) and (msg.find('北京') == -1):
msg_list = list(msg)
msg_list.insert(3, '北京')
true_msg = ''.join(msg_list)
print(true_msg)
print(from_wxid)
if to_wxid in 'chatroom':
wechat_manager.send_text(client_id, to_wxid, qingyunke(msg.replace('>', '', 1)).replace('{br}', '\n'))
else:
wechat_manager.send_text(client_id, from_wxid, qingyunke(msg.replace('>', '', 1)).replace('{br}', '\n'))
else:
wechat_manager.send_text(client_id, from_wxid, qingyunke(msg.replace('>', '', 1)).replace('{br}', '\n'))
except Exception as e:
print(e)
@wechat.CLOSE_CALLBACK(in_class=False)
def on_close(client_id):
print('[on_close] client_id: {0}'.format(client_id))
class LoginTipBot(wechat.CallbackHandler):
@wechat.RECV_CALLBACK(in_class=True)
def on_message(self, client_id, message_type, message_data):
# 判斷登錄成功后,就向檔案助手發條訊息
if message_type == MessageType.MT_USER_LOGIN:
time.sleep(2)
wechat_manager.send_text(client_id, 'filehelper', '測驗訊息')
wechat_manager.send_link(client_id,
'filehelper',
'wechat_pc_api專案',
'WeChatPc機器人專案',
'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()
# 添加回呼實體物件
wechat_manager.add_callback_handler(bot)
wechat_manager.manager_wechat(smart=True)
# 阻塞主執行緒
while True:
time.sleep(0.5)
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/263535.html
標籤:Windows客戶端使用
下一篇:從服務器上無法連通所有埠
