Skip to content

Commit 3c4a1e9

Browse files
committed
wechat
1 parent ff9b4fe commit 3c4a1e9

File tree

1 file changed

+168
-0
lines changed

1 file changed

+168
-0
lines changed

_posts/2019-04-19-wechat.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
layout: post
3+
title: "微信发博文""
4+
author: iosdevlog
5+
date: 2019-04-04 21:18:56 +0800
6+
description: ""
7+
category: 微信
8+
tags: []
9+
---
10+
11+
![kevin-bhagat-343433-unsplash.jpg](https://upload-images.jianshu.io/upload_images/910914-8907c633ebd732e9.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
12+
13+
Photo by [Kevin Bhagat](https://unsplash.com/photos/zNRITe8NPqY?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/search/photos/macbook?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
14+
15+
在小武 2 岁的时候我创建了 《小武成长记》 网站: <http://jiaxianhua.com>
16+
17+
今年还没有更新一次博文。
18+
19+
[http://jiaxianhua.com/jekyll/update/2017/06/06/welcome-to-jekyll.html](http://jiaxianhua.com/jekyll/update/2017/06/06/welcome-to-jekyll.html)
20+
21+
记录了网站的创建过程。
22+
23+
当时是用 [jekyll](https://jekyllrb.com/) 创建的,发 `post` 的话运行 `rake` 命令,交互式的创建标题,子标题,分类等,最后生成 `post` 模板,再写内容。
24+
25+
如果当天没有打开电脑,就发不了文章。
26+
27+
后来发现官方提供 [https://github.com/jekyll/jekyll-admin](https://github.com/jekyll/jekyll-admin),可以网页发文章。
28+
29+
## 微信发博客
30+
31+
如果有人想要添加小武成长的故事,通过微信把内容发给我,我还要自己更新 `github` 仓库。
32+
33+
微信收到内容后可以自动发博文就太好了。
34+
35+
**如何实现呢?**
36+
37+
简单的三步:
38+
39+
1. 收到微信通知
40+
1. 生成博文
41+
1. 更新博文
42+
43+
搞定!
44+
45+
### 1. 收到信息通知
46+
47+
这个可以能过 `github` 上面开源的聊天机器人实现。
48+
49+
* <https://github.com/littlecodersh/ItChat>
50+
* <https://github.com/Urinx/WeixinBot>
51+
52+
我之前用过 WeixinBot,这里使用 ItChat。
53+
54+
#### 首先创建一个后台运行的环境。
55+
56+
```sh
57+
$ screen -S growth15
58+
```
59+
60+
#### 安装 itchat
61+
62+
```sh
63+
$ pip3 install itchat
64+
```
65+
66+
#### 测试一下
67+
68+
有了 itchat,如果你想要给文件传输助手发一条信息,只需要这样:
69+
70+
```sh
71+
import itchat
72+
73+
itchat.auto_login(True, enableCmdQR=2)
74+
75+
itchat.send('Hello, filehelper', toUserName='filehelper')
76+
```
77+
78+
#### 文本消息
79+
80+
如果你想要回复发给自己的文本消息,只需要这样:
81+
82+
```python
83+
import itchat
84+
85+
@itchat.msg_register(itchat.content.TEXT)
86+
def text_reply(msg):
87+
return msg.text
88+
89+
itchat.auto_login(enableCmdQR=2)
90+
itchat.run()
91+
```
92+
93+
#### 客服
94+
95+
```python
96+
import itchat
97+
import re
98+
99+
@itchat.msg_register(itchat.content.TEXT)
100+
def text_reply(msg):
101+
reply = {
102+
"发货|包装": "已经包装好,正在发货!",
103+
"功效|使用方法": "你应该这么使用。。。有这些作用。。。"
104+
}
105+
text = msg.text
106+
print(text)
107+
for key, value in reply.items():
108+
if (re.search(key, text)):
109+
return value
110+
111+
itchat.auto_login(enableCmdQR=2)
112+
itchat.run()
113+
```
114+
115+
### 2. 生成博文
116+
117+
```python
118+
import itchat
119+
import re
120+
import time
121+
import os
122+
123+
def post(text):
124+
post_head = """---
125+
layout: post
126+
title: {title}
127+
subtitle:
128+
author: 小武
129+
date: {date}
130+
categories:
131+
tag:
132+
---
133+
"""
134+
now = time.localtime()
135+
title = time.strftime("%Y 年 %m 月 %d 日")
136+
date = time.strftime("%Y-%m-%d %H:%M:%S +0800")
137+
138+
content = post_head.format(title=title, date=date)
139+
content += text
140+
filename = time.strftime("_posts/%Y-%m-%d-%H-%M-%S-growth15.md")
141+
with open(filename, 'w') as f:
142+
f.write(content)
143+
```
144+
145+
### 3. 更新博文
146+
147+
主要是 `git push`。
148+
149+
```python
150+
commit = "git commit -m '{}'".format(title)
151+
os.system("git add -A")
152+
os.system(commit)
153+
os.system("git push")
154+
return '发送成功'
155+
```
156+
157+
## 测试一下
158+
159+
![growth15.png](https://upload-images.jianshu.io/upload_images/910914-f59adccd27961dee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
160+
161+
[http://jiaxianhua.com/2019/04/18/04-41-30-growth15.html](http://jiaxianhua.com/2019/04/18/04-41-30-growth15.html)
162+
163+
![growth15.com.png](https://upload-images.jianshu.io/upload_images/910914-66700b6447ad7720.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
164+
165+
**完美!**
166+
167+
> PS. 这里只是举了一个简单的例子,我并没有处理图片,视频等内容,有需要的赶紧行动起来。
168+

0 commit comments

Comments
 (0)