A, step by step to teach you to use wechaty+ Baidu cloud host to create a wechat robot with you across the stars
- Wechaty is a fantastic middleware that can communicate with all kinds of chat tools, including but not limited to wechat.
- Baidu cloud is to use it to get the host, very fast
- Go you!
Two, get the server
1. Active address
Baidu cloud address cloud.baidu.com/campaign/bc…
Find new people free cloud activities, then start!!
2. Registered
Got it!!
3. Change the management password
Console to change the password will not say, change after SSH connection
3. Wechaty configuration
1. The tocken application
Apply for a free token at pad-local.com. (Note: A free token is valid for 7 days. To use a longer token, visit wechaty’s official website wechaty.js.org/)
2. Docker download and configuration
Enter the following command in the terminal
apt update apt install docker.io docker pull wechaty/wechaty:latest export WECHATY_LOG="verbose" export WECHATY_PUPPET="wechaty-puppet-wechat" export WECHATY_PUPPET_SERVER_PORT="8080" export Docker run-ti --name wechaty_puppet_service_token_gateway --rm -e WECHATY_LOG -e WECHATY_PUPPET -e WECHATY_TOKEN -e WECHATY_PUPPET_SERVER_PORT -p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT" wechaty/wechaty:latestCopy the code
3. The following output is displayed after the docker is started
You can log in by scanning the QR code on wechat, and there will be a prompt for successful login
4. Precautions
Getting a Docker image is slow, and there are two ways
- 1. Switch domestic Docker mirror points
- Docker pull wechaty/wechaty:latest& background download
4. Local configuration
1. Wechaty environment configuration
! pip install wechaty==0.7dev17
Copy the code
2. The demo sample
- Pay attention to the operating system
- Note that tokens do not use double quotation marks
3 Platform configuration
The 3.1 Linux
export WECHATY_PUPPET=wechaty-puppet-service
export WECHATY_PUPPET_SERVICE_TOKEN=puppet_padlocal_*************
Copy the code
3.2 win
set WECHATY_PUPPET=wechaty-puppet-service
set WECHATY_PUPPET_SERVICE_TOKEN=puppet_padlocal_*************
Copy the code
import os import asyncio import paddlehub as hub import cv2 import time from wechaty import ( Contact, FileBox, Message, Wechaty, ScanStatus, ) model = hub.Module(name="humanseg_lite") def img_koutu(): Img_new_path = os.path.join('humanseg_output', 'dongman' + '.png') print(img_new_path) if os.path.exists('humanseg_output/dongman.png'): os.remove('humanseg_output/dongman.png') res = model.segment( paths=[os.path.join(img_path)], visualization=True, Output_dir ='humanseg_output') # Return path of new image while not os.path.exists(img_new_path): time.sleep(1) return img_new_path def merge (img_path): # import cv2 # cv2.namedWindow("person") # import cv2 # cv2.namedwindow ("person") # import cv2 # cv2.namedwindow ("person") Frame = cv2.imread(back_img_path, cv2.imread_color) # capture image 1 frame = cv2.resize(frame, (1000600)), print (' background shape: ',frame.shape) # IMREAD_UNCHANGED If set, return the loaded image as is (with alpha channel, Otherwise it gets cropped). # Therefore Png must be the 4-channel IMREAD_UNCHANGED person = cv2.imread(img_path, cv2.IMREAD_UNCHANGED) # person=cv2.resize(person, (300, 600)) rows, cols, channels = person.shape print('person shape: ',rows, cols, channels ) dx, dy = 120, 150 roi = frame[dx:dx + rows, dy:dy + cols] print(roi.shape) for i in range(rows): for j in range(cols): if not (person[i, j][3] == 0): # transparent means ROI [I, j] [0] = person [I, j] [0] ROI [I, j] [1] = person [I, j] [1] ROI [I, j] [2] = person [I, j][2] frame[dx:dx + rows, dy:dy + cols] = roi cv2.imwrite(new_img_path, frame) return new_img_path def dongman(img_path, img_name): Img_new_path = r'dongman/dongman.jpg' print(img_new_path) model = hub.Module(name='animegan_v2_shinkai_33') result = model.style_transfer(images=[cv2.imread(img_path)], visualization=True, output_dir='dongman') cv2.imwrite(img_new_path, result[0]) return img_new_path async def on_message(msg: Message): If msg.text() == 'ding': await msg.say(' dong dong dong') if msg.text() == 'hi' or msg.text() == 'hello ': \n- receive "ding", reply "dong dong dong"\n- receive "picture ", reply" picture ") if msg.text() == 'picture ': Url = 'https://ai.bdstatic.com/file/403BC03612CC4AF1B05FB26A19D99BAF' # building a FileBox file_box_1 = Filebox.from_file ('xx.jpg', name=' Today? ') await msg.say(file_box_1) # if msg.type() == message.type.MESSAGE_TYPE_IMAGE: Img_name = file_box_2.name # Imgg_path = img_path './image/' + img_name # Save image as local file await file_box_2.to_file(file_path= img_PATH) # call image style conversion function img_new_path = dongman(img_path, img_name) print(img_new_path) img_new_path = img_koutu() print(img_new_path) img_new_path = merge(img_new_path) File_box_3 = filebox. from_file(img_new_path) await MSG. Say (file_box_3) async def on_scan( qrcode: str, status: ScanStatus, _data, ): print('Status: ' + str(status)) print('View QR Code Online: https://wechaty.js.org/qrcode/' + qrcode) async def on_login(user: Contact): print(user) async def main(): WECHATY_PUPPET_SERVICE_TOKEN if 'WECHATY_PUPPET_SERVICE_TOKEN' not in os.environ: print(" "Error: WECHATY_PUPPET_SERVICE_TOKEN") WECHATY_PUPPET_SERVICE_TOKEN is not found in the environment variables You need a TOKEN to run the Python Wechaty. Please goto our README for details https://github.com/wechaty/python-wechaty-getting-started/#wechaty_puppet_service_token ''') bot = Wechaty() bot.on('scan', on_scan) bot.on('login', on_login) bot.on('message', on_message) await bot.start() print('[Python Wechaty] Ding Dong Bot started.') asyncio.run(main())Copy the code
4. Local development runs
python main.py
5. The process of interstellar travel
Plan to develop on this machine to enable, after the completion of the program test OK, and then on the server, please look forward to 2 days
1. Ken
2. The cutout
3. Science fiction poster synthesis
3.1 background
3.2 the results
6. Linux server deployment
1. Install paddlepaddle
pip install paddlepaddle
Copy the code
2. Install paddlehub
pip install paddlehub
Copy the code
3. Set environment variables
Export WECHATY_PUPPET=wechaty-puppet-service export WECHATY_PUPPET_SERVICE_TOKEN = "Puppet_padLOCAL_ for your key"Copy the code
4. Start the docker
docker run -ti --name wechaty_puppet_service_token_gateway --rm -e WECHATY_LOG -e WECHATY_PUPPET -e WECHATY_TOKEN -e WECHATY_PUPPET_SERVER_PORT -p "$WECHATY_PUPPET_SERVER_PORT:$WECHATY_PUPPET_SERVER_PORT" wechaty/wechaty:latest
Copy the code
5. Run programs in the background
python main.py &
Copy the code