-
Notifications
You must be signed in to change notification settings - Fork 8
/
faceSwapBot.py
67 lines (51 loc) · 2.25 KB
/
faceSwapBot.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
import sys
import time
import telepot
from telepot.loop import MessageLoop
import pprint
from PIL import Image
import requests
class State:
#isAnImageSet
#settingMode
def __init__(self, bool1, bool2):
self.isAnImageSet = bool1
self.settingMode = bool2
#state = State(False, False)
idList = {}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(content_type, chat_type, chat_id)
if content_type == 'text':
if msg['text'] == "/start":
idList[chat_id] = State(False, False)
elif msg['text'] == "/set":
idList[chat_id].settingMode = True
elif msg['text'] == "/stop":
idList[chat_id].isAnImageSet = False
#Wipe the history.
elif msg['text'] == "/help":
SwapBot.sendMessage(chat_id, "1. Use set to set a mask that will be used on all subsequent images \n2. Send any photo you want after that to have the faces replaced by the masked. \n3. Profit???")
else:
SwapBot.sendMessage(chat_id, "I'd love to talk, but I'm a busy bot")
elif content_type == 'photo' and idList[chat_id].settingMode == True:
idList[chat_id].settingMode = False
dloadID = (msg['photo'][0]['file_id'])
fileObject = SwapBot.getFile(dloadID)
filePath = fileObject['file_path']
req = requests.get("https://api.telegram.org/file/bot<insert-bot_key>/" + filePath)
if req.status_code == 200:
with open("./mask" + str(chat_id) + ".jpg", 'wb') as f:
f.write(req.content)
idList[chat_id].isAnImageSet = True
elif content_type == 'photo' and idList[chat_id].isAnImageSet == True:
print("whatever")
#Business as usual
elif content_type == 'photo' and not idList[chat_id].isAnImageSet == True:
SwapBot.sendMessage(chat_id, "Great photo, but there is nothing I can swap onto it. Use the /set command first")
TOKEN = <insert-bot-key>
SwapBot = telepot.Bot(TOKEN)
MessageLoop(SwapBot, handle).run_as_thread()
print ('Listening ...')
while 1:
time.sleep(10)