Smart WeChat public account source code

[Development requirements] Python develops an intelligent public account to achieve the following functions:
1. Perform a simple human-machine dialogue based on the text content entered by the user;
2. Call the Baidu artificial intelligence interface to extract the text of the picture entered by the user.
Note that picocr is a custom-written OCR module for screenshot text recognition , refer to the link to view the source code.

import werobot
import picocr
import requests
import uuid
robot = werobot.WeRoBot(token='doctorwu')

@robot.subscribe
def hello(message):
	return '欢迎来到无尘的小站,发送带文字的图片,我可以帮你转换成可以复制的文本'

@robot.text
def reply(message):
	content = message.content
	if content[0:3]=='###':
		return 'I will check its pseudomeaning.'
	elif content[-2:]=='吗?':
		msg = content[:-2]+'!'
		msg = msg.replace('你','我')
		return msg
	else:
		return '发送带文字的图片,我可以帮你转换成可以复制的文本'
@robot.image
def image_ocr(message):
	req = requests.get(message.img)
	filename = f'{uuid.uuid4()}.jpg'
	print(filename)
	with open('./screenshots/'+filename,'wb') as f:
		f.write(req.content)
	return picocr.get_ocr_result(filename)

robot.config['HOST'] = '0.0.0.0'
robot.config['PORT'] = 80
robot.run()
Published 273 original articles · praised 40 · 30,000+ views

Guess you like

Origin blog.csdn.net/weixin_41855010/article/details/105541063