網頁版微信網址:https://wx.qq.com/
一、定時發送指定消息:
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){
var localTime = new Date();
if(localTime.getDay() < 6){ //非週末
var localTimeString = localTime.toLocaleTimeString();
if(localTimeString.indexOf('上午10:00:00') === 0){
$scope.editAreaCtn = "10.00了,你該喝水了!";
$scope.sendTextMessage();
}
else if(localTimeString.indexOf('上午10:01:00') === 0){
$scope.editAreaCtn = "今天又是新的一天,祝你好運!";
$scope.sendTextMessage();
}
else if(localTimeString.indexOf('下午5:30:00') === 0){
$scope.editAreaCtn = "時間快到5:30了,你該回去了!";
$scope.sendTextMessage();
}
}
},1000);
二、準分報時:
var appElement = document.querySelector('[ng-controller=chatSenderController]');
var $scope = angular.element(appElement).scope();
setInterval(function(){
var now= new Date();
var date = now.getDate();
var day = now.getDay();
var hour = now.getHours();
var min = now.getMinutes();
var second = now.getSeconds();
if(second == 0 ){
var output = "【準分報時】現在是北京時間:"+hour+"時"+min +"分" + second +"秒"
$scope.editAreaCtn = output;
$scope.sendTextMessage();
}
},1000);
方法二:
腳本內容:
setInterval(function(){
if(new Date().toLocaleString().indexOf('2017/11/13 下午2:25:00')===0)
{
$('.edit_area').html('test script');
$(".edit_area").trigger($.Event("keydown", { keyCode: 13,ctrlKey: true}));
$('.btn_send').click();
}
},1000);
其中test script
處爲想要發送的消息內容,時間格式使用new Date().toLocaleString();
來查看
方法三:python
提前說明,先提前安裝好itchat模塊。在命令提示符窗口寫入:pip install itchat 回車就行了。其次就是我用的是python3,我不確定python2能不能正常運行不報錯。
這個程序會把金山詞霸上的每日一句自動發送給指定的微信好友。
效果圖,你們就參考我引用的第一篇博文吧。我就不再獻醜了。
# -*- coding: utf-8 -*-
"""
Created on Thu May 17 19:08:26 2018
Auto send message to one of your friend of Wechat everyday.
@author: XiuCai
"""
from __future__ import unicode_literals
import requests
import itchat
from threading import Timer
def get_news():
url = "http://open.iciba.com/dsapi"
r = requests.get(url)
contents = r.json()['content']
translation = r.json()['translation']
return contents, translation
def send_news():
'''主要功能就在這裏'''
try:#登陸你的微信賬號,會彈出網頁二維碼,掃描即可
itchat.auto_login(hotReload=True)
# 獲取你對應的好友備註,這裏的XXX我只是舉個例子
my_friend = itchat.search_friends(name=u'XXX')
# 獲取對應名稱的一串數字
The_One = my_friend[0]["UserName"]
# 獲取金山字典的內容
message1 = str(get_news()[0])
content = str(get_news()[1][17:])
message2 = str(content)
message3 = "Love you foever"#也可以寫一些酸掉牙的話,煽情的話,告白的話
# 發送消息
itchat.send(message1, toUserName=The_One)
itchat.send(message2, toUserName=The_One)
itchat.send(message3, toUserName=The_One)
# 每86400秒(1天),發送1次,
# 不用linux的定時任務是因爲每次登陸都需要掃描二維碼登陸,
# 很麻煩的一件事,就讓他一直掛着吧
t = Timer(86400, send_news)
t.start()
except:#異常時的處理
message4 = u"今天出現了bug /(ㄒoㄒ)/~~"
itchat.send(message4, toUserName=The_One)
if __name__ == '__main__':
send_news()
python代碼二:
但凡有些事情重複時,我就在想怎麼可以用程序來自動化。這裏想分享如何每天給女友定時微信發送”晚安“,如果只是晚安,就略顯單調,於是爬取金山詞霸每日一句,英文和翻譯,藉此設定定時器進行發送。
準備:
pip install wxpy
pip install requests
實現代碼:
from __future__ import unicode_literals
from threading import Timer
from wxpy import *
import requests
import random
bot = Bot()
# linux執行登陸請調用下面的這句
#bot = Bot(console_qr=2,cache_path="botoo.pkl")
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('fairy')[0]
my_friend.send(contents[0])
my_friend.send(contents[1])
my_friend.send(u"晚安")
# 每86400秒(1天),發送1次
t = Timer(86400, send_news)
# 爲了防止時間太固定,於是決定對其加上隨機數
ran_int = random.randint(0,100)
t = Timer(86400+ran_int,send_news)
t.start()
except:
# 你的微信名稱,不是微信帳號。
my_friend = bot.friends().search('威風大俠')[0]
my_friend.send(u"今天消息發送失敗了")
if __name__ == "__main__":
send_news()
效果截圖:
————————————————
版權聲明:本文爲CSDN博主「精神抖擻王大鵬」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_29027865/article/details/81488654