Skip to content
A wechat python SDK
Branch: master
Clone or download
Pull request Compare This branch is 1 commit ahead, 82 commits behind jeffkit:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Wechat SDK

An UnOfficial WeChat python SDK. For the primal version, the SDK support the Official Account only.

Author: @jeff_kit

Getting start

Before getting start, you should have learnt what wechat official account is, and have registed acccount. if you don't, click here to learn more.

Now, we are going to create an echo robot, the robot will send back what you sent to the official account. ps. I use Django web framework for the example.

1. install Wechat sdk

git clone
cd wechat
python install

2. setup your web server

You can use any webframework you like for the response server, just set it up and have it run.

Your offical account should turn into develop mode, and config the interface url and token.

For example, start a Django project and config a view to interactive with Official account. startproject demo
cd demo
python startapp echo

modify demo/, append echo to INSTALLED_APPS:


add view for official account interface. edit echo/

from django.http import HttpResponse

def wechat(request):
	return HttpResponse('hello wechat')


urlpatterns = patterns('',
	url(r'^wechat/', 'echo.views.wechat'),

3.Warm up

Let's get familar with Official account objects.


A WxRequest instance represent an incoming wechat message. One message is piece of xml. WxRequest map elements of xml to it's own attributes, so you can access the message directly. for more information, see this link.

from wechat.official import WxRequest
wxreq = WxRequest(req.body)  # init with a xml string. The req is a django HttpRequest instance
wxreq.MsgType  # text, image, location, event, link
wxreq.Content  # if it's a text message


A WxResponse instance represent and outgoing wechat message. One message is piece of xml too. there are now three types of Outgoing message: text, link, news. so, there are three WxResponse subclasses for you.


from wechat.official import WxTextResponse
resp = WxTextResponse("hello world", wxreq).as_xml()


from wechat.official import WxMusicResponse, WxMusic
resp = WxMusicResponse(WxMusic(Title="hey jude", Description="2012 London", 
								HQMusicUrl=""), wxreq).as_xml()


from wechat.official import WxNewsResponse, WxArticle
resp = WxNewsResponse([WxArticle(Title="iPhone 6 is here!",
						Description="It is not a joke",
						PicUrl="")], wxreq).as_xml()


You should process the WxRequest and return WxResponse in WxApplication. Every time you make a wechat official account application, you should write a subclass of WxApplication, overwrite the following method, every method start with 'on_' should return a WxResponse instance.

on_text(self, text)

Call when recive a text type message.

on_image(self, image)

Call when recive a image type message.

on_link(self, link)

call when recive a link type message.

on_location(self, location)

Call when recive a location type message.

on_subscribe(self, sub)

Call when recive a subscribe event.

on_unsubscribe(self, unsub)

Call when recive an unsubscribe event.

on_click(self, click)

Call when recive an custome event.

Here is the echo robt's sample code:

from wechat.official import WxApplication, WxTextResponse

class EchoApp(WxApplication):
	def on_text(self, text):
		return WxTextResponse(text.Content, text)
	def on_image(self, image):
		return WxTextResponse(image.PicUrl, image)

We only overwrite to 'on_' style method in the sample. if recive an message which type is neither text nor image, the echo robot will reply an UNSUPPORT_TXT message. you can define you own UNSUPPORT_TXT in you WxApplication subclass.

4.Run the Application

The robot's code is simple. edit the echo/

from django.http import HttpResponse

class EchoApp(WxApplication) ………

def wechat(request):
	app = EchoApp()
	result = app.process(request.GET, request.body, token='your token')
	return HttpResponse(result)

It's easy!


That's all. deploy you web application, and have fun!

You can’t perform that action at this time.