Construction and use of WeChat robots

An unknown college student, known as Caigou in the world of martial arts
original author: jacky Li
Email: [email protected]

Time of completion:2022.12.15
Last edited: 2022.12.17

2505b2adf0bf451db0f53642f0a93f49.jpeg

Table of contents

Instructions for using the "WeChat Robot" program

WeChat robot usage instructions

1. project instruction

2. System environment

3. Preparation conditions

4. Operation steps

Part of the code:


Instructions for using the "WeChat Robot" program

First start the Flask project , then start the Xiaomi ball intranet penetration tool , and finally configure the WeChat public platform . When ready, enter the WeChat public platform.

Enter "joke" on the WeChat public platform, and a joke message will be obtained. The operation effect is shown in Figure 1. Enter "city weather" and the city weather information will be obtained. The running results are shown in Figure 2. Enter other text and the string will be reversed. The running effect is shown in Figure 3.  5ea297e173744ed286a36ef78661eca4.png

                         Figure 1 Joke function            

 d05ec07fdf044505a239d5e78c2fa84b.png

                      Figure 2 Weather check function

WeChat robot usage instructions

Note: For this configuration description, you need to ensure that others can follow the steps to complete the operation and run it! It is not limited to the following 4 steps, and needs to be deleted according to the actual situation of language and program! ! !

1 . project instruction

  • Project name: WeChat robot
  • Author: Jacky Li
  • Project version: V 1.0
  • Version changes: None
  • Completion date: 2022.12.15

2.  System environment

Windows 7 and above/Linux/MacOS

MySQL5.5 or higher

Python3.6 or above

Other Python libraries:

requests==2.18.4
lxml==4.2.5
flask==0.12.2

3. Preparation conditions

       Running this project requires the following prerequisites:

◆ WeChat public platform subscription account

◆ Xiaomi ngrok intranet penetration tool (see text for download and installation steps)

4. Operation steps

1 ) Start virtualization, the steps are as follows:

a. To install the virtual environment, use the following command:

pip install virtualenv

b. Create a virtual environment

In the wechat_rebot directory, create a venv virtual environment with the following command:

virtualenv  venv

c. Start the venv virtual environment with the following command:

venv\script\activate

Part of the code:

def get_weather(keyword):
    url = 'https://www.tianqi.com/tianqi/search?keyword=' + keyword
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    response = requests.get(url,headers=headers)
    tree = etree.HTML(response.text)
    # 检测城市天气是否存在
    try:
        city_name = tree.xpath('//dd[@class="name"]/h2/text()')[0]
    except:
        content = '没有该城市天气信息,请确认查询格式'
        return content
    week = tree.xpath('//dd[@class="week"]/text()')[0]
    now = tree.xpath('//p[@class="now"]')[0].xpath('string(.)')
    temp = tree.xpath('//dd[@class="weather"]/span')[0].xpath('string(.)')
    shidu = tree.xpath('//dd[@class="shidu"]/b/text()')
    kongqi = tree.xpath('//dd[@class="kongqi"]/h5/text()')[0]
    pm = tree.xpath('//dd[@class="kongqi"]/h6/text()')[0]
    content = "【{0}】{1}天气\n当前温度:{2}\n今日天气:{3}\n{4}\n{5}\n{6}".format(city_name, week.split('\u3000')[0], now, temp, '\n'.join(shidu),kongqi,pm)
    return content

Get the weather in the area you want to query through the API.

def get_joke():
    url="http://www.qiushibaike.com/text/page/"+ str(randint(1,5))
    r = requests.get(url)
    tree = etree.HTML(r.text)
    contentlist = tree.xpath('//div[@class="content"]/span')
    jokes = []
    for content in contentlist:
        content = content.xpath('string(.)') # string() 函数将所有子文本串联起来,# 必须传递单个节点,而不是节点集。
        if '查看全文' in  content:  # 忽略包含“查看原文”笑话
            continue
        jokes.append(content)
    joke = jokes[randint(1, len(jokes))].strip()
    return joke

In the same way, jokes are obtained through the API and displayed to users through the WeChat public account.

The author has something to say

If you feel that what the blogger said is useful to you, please click "Follow" to support it. We will continue to update such issues...

If you need the source code, please chat with the blogger privately.

Guess you like

Origin blog.csdn.net/weixin_62075168/article/details/128333675