forked from TheExplainthis/ChatGPT-Tinder-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (60 loc) · 2.37 KB
/
main.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
70
71
72
73
74
75
import datetime
import os
from src.chatgpt import ChatGPT, DALLE
from src.models import OpenAIModel
from src.tinder import TinderAPI
from src.dialog import Dialog
from src.logger import logger
from opencc import OpenCC
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
from fastapi import FastAPI
import uvicorn
load_dotenv('.env')
models = OpenAIModel(api_key=os.getenv('OPENAI_API'), model_engine=os.getenv('OPENAI_MODEL_ENGINE'))
chatgpt = ChatGPT(models)
dalle = DALLE(models)
dialog = Dialog()
app = FastAPI()
scheduler = AsyncIOScheduler()
cc = OpenCC('s2t')
TINDER_TOKEN = os.getenv('TINDER_TOKEN')
@scheduler.scheduled_job("cron", minute='*/5', second=0, id='reply_messages')
def reply_messages():
tinder_api = TinderAPI(TINDER_TOKEN)
profile = tinder_api.profile()
user_id = profile.id
for match in tinder_api.matches(limit=50):
chatroom = tinder_api.get_messages(match.match_id)
lastest_message = chatroom.get_lastest_message()
if lastest_message:
if lastest_message.from_id == user_id:
from_user_id = lastest_message.from_id
to_user_id = lastest_message.to_id
last_message = 'me'
else:
from_user_id = lastest_message.to_id
to_user_id = lastest_message.from_id
last_message = 'other'
sent_date = lastest_message.sent_date
if last_message == 'other' or (sent_date + datetime.timedelta(days=1)) < datetime.datetime.now():
content = dialog.generate_input(from_user_id, to_user_id, chatroom.messages[::-1])
response = chatgpt.get_response(content)
if response:
response = cc.convert(response)
if response.startswith('[Sender]'):
chatroom.send(response[8:], from_user_id, to_user_id)
else:
chatroom.send(response, from_user_id, to_user_id)
logger.info(f'Content: {content}, Reply: {response}')
@app.on_event("startup")
async def startup():
scheduler.start()
@app.on_event("shutdown")
async def shutdown():
scheduler.remove_job('reply_messages')
@app.get("/")
async def root():
return {"message": "Hello World"}
if __name__ == "__main__":
uvicorn.run('main:app', host='0.0.0.0', port=8080)