微信機器人--定時給朋友傳送訊息and 自動回覆訊息
阿新 • • 發佈:2019-02-14
定時給朋友傳送訊息:
from __future__ import unicode_literals from threading import Timer from wxpy import * import requests # bot=Bot() # 從快取中獲取登入資訊,剛登陸過,無需一直登陸 bot = Bot(cache_path=True) def get_news(): """獲取金山詞霸每日一句,英文和翻譯""" url = "http://open.iciba.com/dsapi/" r = requests.get(url) content = r.json()['content'] note = r.json()['note'] return content, note def send_news(): try: contents = get_news() # 你朋友的微信名稱,不是備註,也不是微信帳號。 my_friend = bot.friends().search(u'Quincy.Coder')[0] my_friend.send(contents[0]) my_friend.send(contents[1]) my_friend.send(u"HELLO !") # 每86400秒(1天),傳送1次 t = Timer(86400, send_news) t.start() except: # 你的微信名稱,不是微信帳號。 my_friend = bot.friends().search('庸人莫自擾')[0] my_friend.send(u"今天訊息傳送失敗了") if __name__ == "__main__": send_news()
自動回覆訊息:
# from wxpy import * # # bot = Bot(cache_path=True) # # girl_friend = bot.search('劉劉劉')[0] # print(girl_friend) # # # @bot.register() # 接收從指定好友發來的訊息,傳送者即recv_msg.sender為指定好友girl_friend # def recv_send_msg(recv_msg): # print('收到的訊息:', recv_msg.text) # recv_msg.text取得文字 # if recv_msg.sender == girl_friend: # recv_msg.forward(bot.file_helper, prefix='老婆留言: ') # 在檔案傳輸助手裡留一份,方便自己忙完了回頭檢視 # ms = '老婆最美麗,我對老婆的愛如滔滔江水,連綿不絕' # print('>>>給老婆回覆的:', ms) # return ms # 給老婆回一份 # # # embed() from wxpy import * # bot = Bot() bot = Bot(cache_path=True) gf = bot.friends().search('Quincy.Coder')[0] @bot.register() def recv_send_msg(msg): print('收到的訊息:', msg.text) # msg.reply_file() if msg.sender == gf: msg.forward(bot.file_helper, prefix='女同學留言:') ms = '【測試】好好學習,天天向上!' return ms embed()
參考官方文件:https://wxpy.readthedocs.io/zh/latest/chats.html#wxpy.Chat.send_file