Happy Chinese New Year 2017

Chinese New Year Greeting

Happy Chinese Sprint Festival and wish you a good fortune in new Rooster Year! Here is antithetical couplet(对联) for this new Rooster Year, just inspired by dynamic programming language and serverless view points this morning.

Following up, there is the sample code to play Chat Robot in WeChat, the most popular online chat service of Chinese. It is a sample to demo how to use chat lib and how to use substitution in UTF-8 cprint format strings.

Enjoy!

道可道,非常道, Lambda   里外有乾坤 
型可依,函亦纯, __iadd__ happy, 1

横批: Hello Cloud!

This sample Chat Robot will return the new year greeting if there is "年"(year) character in arriving message text part, or, the incoming message is a picture, video as people used to throw for new year greeting.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'maxwu'


import sys
import itchat, re
from itchat.content import *

reload(sys)
sys.setdefaultencoding('utf-8')


@itchat.msg_register(TEXT)
def text_reply(msg):
global count
print "Msg Received from %s: %s" %(msg['FromUserName'], msg['Text'])

match = re.search(u'\u5e74', msg['Text'])
if match:
count += 1
print "matched!"
itchat.send("新春快乐,鸡年大吉,-%d-" % count, msg['FromUserName'])


@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
def other_reply(msg):
global count
count += 1
itchat.send("发啥都是发,鸡年大吉,-%d-" % count, msg['FromUserName'])


if __name__ == "__main__":
count = 0
itchat.auto_login()
itchat.run(debug=True)

//EOF