Introduction to Python development and combat 22-Simple message reply

Source: Internet
Author: User
Tags cdata

22. Simple message Reply

In this section we will implement an inventory query function using the portal to implement the Inventory query service for the text message class with our previous Biz business Logic Layer example.

22.1. Add processing logic for handling text pushes in the RESPONSEMSG function
defresponsemsg (postcontent): Poststr=smart_str (postcontent)#poststr = postcontent    ifpoststr:msg=xmlcontent2dic (POSTSTR)ifmsg['Msgtype']:            ifmsg['Msgtype'] =='Event': ResultStr= Handleevent (msg)#handling Event Type pushes            elifmsg['Msgtype'] =='text': ResultStr=handletext (msg)#handling Text type push        Else: ResultStr='Input something ...'        returnResultStr
22.2. Add Handletext function to handle push text information logic
defHandletext (msg): ResultStr="<xml><tousername><! [cdata[%s]]></tousername><fromusername><! [cdata[%s]]></fromusername><createtime>%s</createtime><msgtype><! [cdata[%s]]></msgtype><content><! [cdata[%s]]></content></xml>"    ifmsg['Content'] =='1': ResultStr= resultstr% (msg['Fromusername'],msg['Tousername'],STR (int (time.time ())),'text', u'Please enter a query for the item name, the format "K nut". ')       elifString.upper (msg['Content'][0]) = ='K': ItemName= msg['Content'][1:len (msg['Content'])] STRs="'Biz=inventorybiz () Inventorys=biz.getinventorybyitemname (itemname) forInventoryinchInventorys:strs= STRs + inventory. Item.itemname + u'Stock Quantity:'+ STR (inventory. Amount) +'\ n'            ifSTRs = ="': STRs= u'Current item: "'+ ItemName +u'"No inventory data was queried'ResultStr= resultstr% (msg['Fromusername'],msg['Tousername'],STR (int (time.time ())),'text', STRs)Else: ResultStr= resultstr% (msg['Fromusername'],msg['Tousername'],STR (int (time.time ())),'text', u'Thank you for your attention to "micro-everyone public" \ n current functions as follows: "1" Inventory inquiry;')      returnResultStr

Note: We still get inventory information through the query function call of the business logic layer, and then return to the query user for "". The code simply calls the Biz layer interface, and as a developer of the "" interface no longer needs to consider the logical implementation of the business logic layer. In this way we can also think of "as an expression pattern in MVC view."

As the code is updated to the cloud, we can make inventory inquiries at the end. Such as:

22.3. Summary

In this chapter we demonstrate how the inventory query is quickly extended to support queries, because the business logic layer of code is shared, which means that the business logic of the real inventory query is a black box for the developer of the interface, and the developer calls the Biz Layer interface to complete the extension of the function from the Web query to the query.

Further if the development team internally defined its own information encapsulation format, the end of the inventory query and ExtJS Web-side inventory query can be unified call a service interface. For example: The Getinventorybyitemname service we released earlier, the period returns the JSON data format. This allows the client developer to not know how the server is implemented, just invoke the service. This kind of programming pattern can realize the complete separation of interface presentation layer and service layer development, and further reduce the system coupling. Unified service layer to achieve the consistency of different terminal access results, improve the overall development efficiency of the system.

Introduction to Python development and combat 22-Simple message reply

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.