-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
183 lines (154 loc) Β· 5.49 KB
/
example.py
File metadata and controls
183 lines (154 loc) Β· 5.49 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#How to use cortex
from flask import Flask, request, render_template
import os
import requests
import random
from dotenv import load_dotenv
from cortex import Client
load_dotenv()
app = Flask(__name__, template_folder=".")
ACCESS_TOKEN = os.getenv("PAGE_ACCESS_TOKEN")
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN")
if not ACCESS_TOKEN or not VERIFY_TOKEN:
raise ValueError("Missing PAGE_ACCESS_TOKEN or VERIFY_TOKEN")
fb = Client(ACCESS_TOKEN)
CAT_IMAGES = [
"https://placekitten.com/800/600",
"https://placekitten.com/801/600",
"https://placekitten.com/802/600",
"https://placekitten.com/803/600",
"https://cataas.com/cat",
"https://cataas.com/cat/cute",
"https://cataas.com/cat/sleepy",
"https://cataas.com/cat/playful",
]
CAT_VIDEOS = [
"https://samplelib.com/lib/preview/mp4/sample-5s.mp4",
"https://samplelib.com/lib/preview/mp4/sample-10s.mp4",
"https://filesamples.com/samples/video/mp4/sample_640x360.mp4",
"https://filesamples.com/samples/video/mp4/sample_960x400_ocean_with_audio.mp4",
]
AUDIOS = [
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3",
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3",
"https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3",
]
@app.route("/")
def home():
return render_template("index.html")
@app.route("/webhook", methods=["GET", "POST"])
def webhook():
if request.method == "GET":
return fb.challenge(VERIFY_TOKEN)
if request.method == "POST":
def post(text):
try:
fb.postFeed(media_type="text", text=text.strip())
return True, None
except Exception as e:
print(f"Post failed: {e}")
return False, str(e)
def echo(sender_id, msg_type, content, message_id):
if msg_type != "text":
fb.sendMsg(
psid=sender_id,
text=f"{msg_type} received π₯",
msg_type="text"
)
return
content = content.strip().lower()
if content == "help":
help_text = """ββββ¦Ώγ β‘ BOT COMMANDS γ
β
β π€ User : Suleiman
β π§ Email : cortexinvader@gmail.com
β π¬ Social : facebook.com/cortexinvader
β
β π Available actions :
β Send text β’ image β’ video β’ audio
β to receive cool replies π
β
β π Special commands :
β - /post your message β post to page
β - image β random cat pic
β - video β relaxing cat clip
β - audio β purr or chill music
β
β°βββββββββ¦Ώ"""
fb.sendMsg(
psid=sender_id,
text=help_text,
msg_type="text"
)
return
if content == "image":
fb.sendMsg(
psid=sender_id,
text=None,
msg_type="image",
media_url=random.choice(CAT_IMAGES)
)
return
if content == "video":
fb.sendMsg(
psid=sender_id,
text=None,
msg_type="video",
media_url=random.choice(CAT_VIDEOS)
)
return
if content == "audio":
fb.sendMsg(
psid=sender_id,
text=None,
msg_type="audio",
media_url=random.choice(AUDIOS)
)
return
if content.startswith("/post"):
post_text = content[5:].strip()
if not post_text:
fb.sendMsg(
psid=sender_id,
text="Use: /post your message",
msg_type="text"
)
return
success, error_msg = post(post_text)
if success:
return
else:
msg = "Couldn't post right now π"
if error_msg:
msg += f"\n({error_msg})"
fb.sendMsg(
psid=sender_id,
text=msg,
msg_type="text"
)
return
try:
ai_text = requests.get(f"https://text.pollinations.ai/{content}", timeout=60).text
fb.sendMsg(psid=sender_id, text=ai_text, msg_type="text")
except Exception as e:
print(f"AI request failed: {e}")
fb.sendMsg(
psid=sender_id,
text=f"AI is sleeping right now π΄ ({str(e)})",
msg_type="text"
)
return fb.event(handler=echo)
if __name__ == "__main__":
print("""
ββββββββββββββββββββββββββββββββββββββ
β CORTEX FACEBOOK BOT β
β Lightweight Graph API wrapper β
ββββββββββββββββββββββββββββββββββββββ
Made with β₯ by cortexinvader
facebook.com/cortexinvader
""")
app.run(
host="0.0.0.0",
port=3000,
debug=True
)