-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtelegraph.py
221 lines (189 loc) · 7.34 KB
/
telegraph.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from bcnadds import TgGraph
from datetime import datetime
import psycopg2
token = "d3b25feccb89e508a9114afb82aa421fe2a9712b963b387cc5ad71e58722" # from https://telegra.ph/api
API_ID = "" #from my.telegram.org
API_HASH = "" #from my.telegram.org
BOT_TOKEN = "" # @botfather
DATABASE_URL = "" # Replace with your PostgreSQL database URL https://www.elephantsql.com/
app = Client("telegraph_bot", api_id=API_ID, api_hash=API_HASH, bot_token=BOT_TOKEN)
tgraph = TgGraph(access_token=token)
def Connect(query, values=None, fetch=False):
connection = psycopg2.connect(DATABASE_URL, sslmode='require')
cursor = connection.cursor()
try:
if values:
cursor.execute(query, values)
else:
cursor.execute(query)
connection.commit()
if fetch:
return cursor.fetchall()
finally:
cursor.close()
connection.close()
create_users = """
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
user_id BIGINT UNIQUE NOT NULL
);
"""
Connect(create_users)
def get_users_count():
query = "SELECT COUNT(user_id) FROM users;"
result = Connect(query, fetch=True)
return result[0][0] if result else 0
def add_stats(user_id):
query = "INSERT INTO users (user_id) VALUES (%s) ON CONFLICT DO NOTHING RETURNING id;"
result = Connect(query, (user_id,), fetch=True)
return result[0][0] if result else None
@app.on_message(filters.command("stats"))
async def stats_callback(client, message):
unique_users = get_users_count()
msg = await message.reply_text("Getting your stats...")
await msg.edit_text(
f"📊 <b><u>Statistics</u></b>\n\n"
f"<b>Users:</b> <code>{unique_users}</code>"
)
@app.on_message(filters.photo | filters.video | filters.animation)
async def handle_messages(client, message):
try:
telegraph_link = None
if message.photo or message.video or message.animation:
# For photos, videos, or GIFs, generate a Telegraph link with text if available
file_path = await message.download()
uploaded_files = await tgraph.file_upload(file_path)
media_source_url = uploaded_files[0].get('src')
# Check if the message contains text
if message.caption:
msg = await message.reply_text("Uploading file...")
await msg.edit_text("Generating your link...")
page_title = "TgGraph"
page_content = message.caption
telegraph_response = await tgraph.create_page(
page_title,
html_content=f'<img src="{media_source_url}" alt="Telegraph Media">{page_content}',
return_content=True,
return_html=True
)
telegraph_link = f'https://telegra.ph/file/{telegraph_response.get("path")}'
await msg.edit_text(f"🔗 Here is your link: {telegraph_link}")
else:
telegraph_link = f'https://graph.org/file/{media_source_url.split("/")[-1]}'
msg = await message.reply_text("Uploading your link...")
await msg.edit_text(f"🔗 Here is your link: {telegraph_link}")
except Exception as e:
await message.reply(f"An error occurred: {str(e)}")
@app.on_message(filters.text & ~filters.command("start") & ~filters.command("help") & ~filters.command("stats"))
async def handle_text_messages(client, message):
try:
if message.text:
msg = await message.reply_text("Creating Telegraph page...")
# If the message contains text, create a Telegraph page with the text
page_title = "TgGraph"
page_content = message.text
telegraph_response = await tgraph.create_page(
page_title,
html_content=page_content,
return_content=True,
return_html=True
)
telegraph_link = f'https://graph.org/{telegraph_response.get("path")}'
await msg.edit_text(f"Here is your link: {telegraph_link}")
except Exception as e:
await message.reply(f"An error occurred: {str(e)}")
@app.on_callback_query(filters.regex("about"))
async def about_callback(client, callback_query):
about_text = (
f"🤖 **Bot Information**\n\n"
f"**Developer:** [Developer](t.me/my_name_is_nobitha)\n"
f"**Library:** [Pyrogram](pyrogram.org)\n"
f"**Programming Language:** [python](python.org)\n"
f"**Telegraph API:** [Bcnadds](https://github.com/bcncalling/bcnadds)\n"
)
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Back", callback_data="start"),
]
]
)
await callback_query.edit_message_text(
about_text,
reply_markup=keyboard,
disable_web_page_preview=True
)
@app.on_callback_query(filters.regex("help"))
async def help_callback(client, callback_query):
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Back", callback_data="start"),
]
]
)
await callback_query.edit_message_text(
"ℹ️ **Help**\n\n"
"This bot can create Telegraph pages for your text, photos, videos, and GIFs.\n\n"
"To create a Telegraph page, send a photo, video, GIF, or text to the bot with optional text.",
reply_markup=keyboard
)
@app.on_message(filters.command("help"))
async def help_(client, message):
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Back", callback_data="start"),
]
]
)
await message.reply_text(
"ℹ️ **Help**\n\n"
"This bot can create Telegraph pages for your text, photos, videos, and GIFs.\n\n"
"To create a Telegraph page, send a photo, video, GIF, or text to the bot with optional text.",
reply_markup=keyboard
)
@app.on_callback_query(filters.regex("start"))
async def start_callback(client, callback_query):
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Updates", url="t.me/TgBotsNetwork"),
],
[
InlineKeyboardButton("About", callback_data="about")
],
[
InlineKeyboardButton("Help", callback_data="help"),
]
]
)
await callback_query.edit_message_text(
"Hello! I am a Telegraph bot. I can help you create Telegraph pages for your content.\n\n"
"Use /help to see more information.",
reply_markup=keyboard
)
@app.on_message(filters.command("start"))
async def start(client, message):
keyboard = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Updates", url="t.me/TgBotsNetwork"),
],
[
InlineKeyboardButton("About", callback_data="about")
],
[
InlineKeyboardButton("Help", callback_data="help"),
]
]
)
add_stats(message.from_user.id)
await message.reply(
"Hello! I am a Telegraph bot. I can help you create Telegraph pages for your content.\n\n"
"Use /help to see more information.",
reply_markup=keyboard
)
app.run()