21

云拜年攻略!用Python自动回复拜年消息,还能“抗”七姑八姨的灵魂问题

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MjM5MTQzNzU2NA%3D%3D&%3Bmid=2651676957&%3Bidx=1&%3Bsn=6f1afa047db5c86e9288720b773b51d7
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

3iAVvai.jpg!web

大数据文摘出品

作者: 曹培信

为了响应国家号召,今年过年不出去拜年,不出去走亲戚,开启了 “云拜年” 新模式。

从年三十到今天,手机上的拜年消息就没停过 ,大多还是群发,不回复显得很没 礼貌,一一回复又累心劳神。

甚至因为出不了门,七大姑八大姨就更闲了, “云催婚”“云催娃” 也应运而生, 如果你也在为此而烦恼,那么你就需要一款能够自动回复拜年信息的小程序,最好还能自动回复各种微信消息,应对七大姑八大姨的花式催婚和催娃。

这么小小的要求,Python当然可以做到,itchat包帮你解决一切,没有安装的同学需要先安装,用Python操作微信真是好玩到停不下来!

具体思路就是现在图灵机器人网站上注册一个账号,记下自己的key码,代码中要用到。 然后在Python中用itchat登录微信,当接受到消息后(可以简单用个if函数设置只对特定好友有效),提交到图灵机器人网站并得到回复,而后将此回复再发送给好友。

#引入itchat包

import requests

import itchat

#自动登陆(括号内参数可以保持一段时间登录状态)

itchat.auto_login()


#name = itchat.search_friends(name=u'曹培信')

#XiaoMing = name[0]["UserName"]

#message_concent = 'Hey,dude'

#itchat.send(message_concent,XiaoMing)

KEY = '440a48c5f559402ea4a0ce9a5dda7fa3'


def get_response(msg):

apiUrl = 'http://www.tuling123.com/openapi/api'

data = {

'key' : KEY,

'info' : msg,

'userid' : 'wechat-robot',

}

try:

r = requests.post(apiUrl, data=data).json()

return r.get('text')

except:

return


@itchat.msg_register(itchat.content.TEXT)

def tuling_reply(msg):

defaultReply = 'I received: ' + msg['Text']

name=itchat.search_friends(name=u'***')

# ***表示你想进行自动回复的人的微信昵称(备注名)

xiaoming=name[0]["UserName"]

if msg['FromUserName']==xiaoming:

reply = get_response(msg['Text'])

if '拜年' in msg['Text'] or '新年好' in msg['Text']:

itchat.send("愿新春以后,吉吉利利,百事都如意。", msg['FromUserName'])

else:

return reply or defaultReply


#itchat.auto_login(hotReload=True)

itchat.run()

itchat.logout()

然后,就是设置关键词,一般的拜年信息会包含“拜年”、“新年好”,我们用 if '拜年' in msg[ 'Text' ] 来进行关键词判断,如果包含这样的拜年信息,则自动回复一条拜年的问候语。

6fq6Rnz.png!web

这样,你就得到了一个既能自动回复拜年信息,也可以针对七大姑八大姨扩展功能的自动回复机器人,简直是春节必备之神器,赶紧用起来吧!

iMRB7nA.jpg!web

实习/全职编辑记者招聘ing

加入我们,亲身体验一家专业科技媒体采写的每个细节,在最有前景的行业,和一群遍布全球最优秀的人一起成长。坐标北京·清华东门,在大数据文摘主页对话页回复 “招聘” 了解详情。简历请直接发送至[email protected]

UziiMrJ.jpg!web

点「在看」的人都变好看了哦!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK