Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Wechat automatic reply function by using Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use Python to realize WeChat automatic reply function. Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope you can gain something according to this article.

The code is as follows:

import time, logging, random, requests

from queue import Queue

from WechatPCAPI import WechatPCAPI

logging.basicConfig(level=logging.INFO)

queue_recved_event = Queue()

def on_message(msg):

queue_recved_event.put(msg)

#Robot returns message

def reply_msg(receive_msg):

apikey = 'own apikey'

apiurl = 'http://www.tuling123.com/openapi/api? key=%s&info=%s' % (apikey, receive_msg)

result = requests.get(apiurl)

result.encoding = 'utf-8'

data = result.json()

return data['text']

def login():

pre_msg = ''

#Initialize WeChat instance

wx_inst = WechatPCAPI(on_message=on_message, log=logging)

#Start WeChat

wx_inst.start_wechat(block=True)

#Wait for login success. At this time, you need to manually scan the code to log in to WeChat.

while not wx_inst.get_myself():

time.sleep(5)

print ('login successful')

while True:

msg = queue_recved_event.get()

if 'msg::single' in msg.get('type'):

data = msg.get('data')

if data.get('is_recv', False):

msgfrominfo = data.get('msgfrominfo')

if msgfrominfo is not None:

wx_id = msgfrominfo.get('wx_id')

if wx_id != 'weixin':

receive_msg =str(data.get('msgcontent'))

reply = reply_msg(receive_msg)

wx_inst.send_text(to_user=wx_id, msg=reply)

After reading the above, do you have any further understanding of how to use Python to implement WeChat automatic reply function? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report