Integration of WeChat public platform development functions

Source: Internet
Author: User
In the previous functional development documents, each function is independent and can only provide one function, which does not meet the needs of developers and customers. Therefore, in this article, we will integrate the previously developed functions for your reference. I. INTRODUCTION

In the previous functional development documents, each function is independent and can only provide one function, which does not meet the needs of developers and customers. Therefore, in this article, we will integrate the previously developed functions for your reference.

II. train of thought analysis

A simple method is to intercept keywords, make judgments, and execute the corresponding functional code. This method is suitable for scenarios with few features and is simple. Another method is to number each feature and record the status of the selected feature. each time a user queries, the user first checks its status, then execute the corresponding function code. This method is suitable for functions that are more integrated and complex. developers can choose based on their own needs. In this article, we will explain the integration of weather and translation functions. the integration of more functions is similar.

3. keyword truncation

3.1 keyword truncation

We define that the format of the message sent by the user is fixed. the format of the query weather is "region + weather", such as "Suzhou weather" and "Beijing weather". Therefore, the last two words are intercepted first, determine whether the keyword is "weather", and then extract the city name to query. Similarly, translation also intercepts the first two words, determines whether it is a "translation" keyword, and then intercepts the subsequent text for query operations.

// Intercept the keyword $ weather_key = mb_substr ($ keyword,-, "UTF-8"); $ city_key = mb_substr ($ keyword, 0,-2, "UTF-8 "); $ translate_key = mb_substr ($ keyword, 2,200, "UTF-8"); $ word_key = mb_substr ($ keyword, "UTF-8 ");

3.2 function integration

If ($ weather_key = 'weather '&&! Empty ($ city_key) & $ translate_key! = 'Translation') {$ contentStr = _ weather ($ city_key);} elseif ($ translate_key = 'translation '&&! Empty ($ word_key) {$ contentStr = _ baiduDic ($ word_key);} else {$ contentStr = "thank you for your attention [zhuojin Suzhou] \ n: zhuojinsz ";}

Note:Here, we have encapsulated query weather and translation into functions _ weather () and _ baiduDic (), and then introduced these files, which can be called directly here, which is very convenient.

In this way, we have completed the integration of the weather and translation functions.

3.3 test

The test is successful.

IV. status record practices

4.1 Description

First, we need to number each function, for example:

Reply sequence number:

1. weather query

2. translation query

Then, the database is used to record the user's query status. each time the user inputs a message, the system first queries the user's status from the database and then performs corresponding operations.

4.2 create a user status table user_flags in the database.

---- TABLE structure 'user _ flags '-- create table if not exists 'user _ flags' ('from _ user' varchar (50) not null, 'flag _ id' int (4) not null) ENGINE = MyISAM default charset = utf8;

4.3 introduce database function files

To operate the database, you need to introduce the data operation file. here we use the MySQL cloud database provided by BAE.

// Introduce the database file require_once ('regiondes/mysql_bae.func.php ');

4.4 determine the user status

// Determine the user status $ SQL = "SELECT flag_id FROM user_flags WHERE from_user = '$ fromUsername 'limit 0, 1"; $ result = _ select_data ($ SQL); while (!! $ Rows = mysql_fetch_array ($ result) {$ user_flag = $ rows [flag_id];}

Note:Obtain flag_id from the user_flags table and assign it to $ user_flag for the following judgment operations.

4.5 determine the user's existing status and new input status

if(trim($keyword) <> $user_flag && is_numeric($keyword)){    $user_flag = '';    $sql = "DELETE FROM user_flags WHERE from_user = '$fromUsername'";    _delete_data($sql);}

Note:Determine the user's existing status and the new input status. if the status is different and the input keyword is a number, set $ user_flag to null and clear the status in the database, as if the first query process

4.6 User status judgment

A.Status is empty, that is, the first query

If (empty ($ user_flag) {switch ($ keyword) {case 1: // query weather conditions $ SQL = "insert into user_flags (from_user, flag_id) values ('$ fromusername', '1') "; $ contentStr =" enter the city to query the weather: Beijing, Shanghai, and Suzhou "; break; case 2: // translate $ SQL = "insert into user_flags (from_user, flag_id) values ('$ fromusername', '2')"; $ contentStr = "Enter the content to translate: for example: good morning, good morning, zookeeper Zookeeper "; break; default: // others $ SQL = ""; $ contentStr = "thank you for your attention [zhuojin Suzhou] \ n: zhuojinsz \ N please reply No.: \ n1. weather query \ n2. translation query \ n enter [help] to view the prompt \ n For more information. please wait... "; break;} // judge and execute the preceding insert statement if (! Empty ($ SQL) {_insert_data ($ SQL );}}

Note:The user status is blank, that is, the first query. if the keyword entered by the user is the function serial number (1 or 2), the user status is written to the database and a prompt is displayed; if the keyword entered by the user is not a function serial number, a help message is provided, prompting the user to enter the keyword.

B.The user status is not empty.

Else {if ($ user_flag = '1') {$ contentStr = _ weather ($ keyword); // query weather} elseif ($ user_flag = '2 ') {$ contentStr = _ baiduDic ($ keyword); // translation }}

Note:The user status is not empty. the user has a query operation. as long as the user does not switch over, the user remains under the existing function and runs the corresponding code.

4.7 test

For more articles on integration of public platform development functions, refer to PHP Chinese network!

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.