-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
69 lines (54 loc) · 1.91 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: UTF-8 -*-
import itchat
import logging
from flask import Flask, jsonify, send_file, request, render_template, url_for, redirect
app = Flask(__name__)
logger = logging.getLogger('app')
mychat = mychat = itchat.Core()
@app.route("/", methods=['GET'])
def index():
return redirect('static/index.html')
@app.route('/qr', methods=['GET'])
def get_QR():
mychat.get_QRuuid()
logger.info("----", mychat.uuid)
qr_io = mychat.get_QR(enableCmdQR=2)
qr_io.seek(0)
return send_file(qr_io, mimetype='image/png')
@app.route('/login', methods=['GET'])
def get_isLogging():
logger.info("----", mychat.uuid)
if not mychat.uuid:
return jsonify({'error': 'please request /qr and scan QR'})
status = mychat.check_login()
if status == '200':
isLoggedIn = True
elif status == '201':
return jsonify({'error': 'Please press confirm on your phone.'})
elif status == '408':
return jsonify({'error': 'please request /qr and scan QR', 'status': status})
mychat.web_init()
mychat.show_mobile_login()
mychat.get_contact(True)
mychat.start_receiving()
return jsonify({'isLoggin': mychat.alive})
@app.route('/send', methods=['POST'])
def sendMsg():
if not mychat.alive:
return jsonify({'error': '请先登录'})
json = request.json
return jsonify(mychat.send(json[u'message'], findUserByName(mychat, json[u'name'])))
@app.route('/send-group', methods=['POST'])
def sendGroupMsg():
if not mychat.alive:
return jsonify({'error': '请先登录'})
json = request.json
return jsonify(mychat.send(json[u'message'], findGroupUserByName(mychat,json[u'name'])))
def findGroupUserByName(mychat, name):
users = mychat.search_chatrooms(name=name)
return users[0]['UserName']
def findUserByName(mychat, name):
users = mychat.search_friends(name=name)
return users[0]['UserName']
if __name__ == '__main__':
app.run()