├── static
└── css
│ └── custom.css
├── telepost.db
├── templates
├── final-message.html
├── index.html
├── send-messages.html
├── signin.html
├── add-new-group.html
├── verify-code.html
├── compose-message.html
├── add-new-user.html
└── layout.html
├── README.md
└── app.py
/static/css/custom.css:
--------------------------------------------------------------------------------
1 | .spaced-div {
2 | margin-bottom: 20px;
3 | }
--------------------------------------------------------------------------------
/telepost.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/HasnainAshfaq/telethon-api-web-app/HEAD/telepost.db
--------------------------------------------------------------------------------
/templates/final-message.html:
--------------------------------------------------------------------------------
1 | Verfied.
2 | Click here to go back to application.
--------------------------------------------------------------------------------
/templates/index.html:
--------------------------------------------------------------------------------
1 | {% extends("layout.html") %}
2 |
{{ notification }}
3 | {% block content %} {% endblock %}
--------------------------------------------------------------------------------
/templates/send-messages.html:
--------------------------------------------------------------------------------
1 | {% extends("index.html") %}{% import "bootstrap/wtf.html" as wtf %}{% block content %}
2 |
3 |
9 |
10 | {% endblock %}
--------------------------------------------------------------------------------
/templates/signin.html:
--------------------------------------------------------------------------------
1 | {% extends("layout.html") %} {% import "bootstrap/wtf.html" as wtf %} {% block content %}
2 |
8 | {% endblock %}
--------------------------------------------------------------------------------
/templates/add-new-group.html:
--------------------------------------------------------------------------------
1 | {% extends("index.html") %}{% import "bootstrap/wtf.html" as wtf %}{% block content %}
2 | Your group invite link can be taken from the group in Telegram. Paste exact URL here.
3 |
9 |
10 | {% endblock %}
--------------------------------------------------------------------------------
/templates/verify-code.html:
--------------------------------------------------------------------------------
1 | {% extends("index.html") %}{% import "bootstrap/wtf.html" as wtf %}{% block content %}
2 | A message from Telegram has been sent to your mobile phone or your logged in device. Please enter the verification code here.
3 |
9 |
10 | {% endblock %}
--------------------------------------------------------------------------------
/templates/compose-message.html:
--------------------------------------------------------------------------------
1 | {% extends("index.html") %}{% import "bootstrap/wtf.html" as wtf %}{% block content %}
2 | A message from Telegram has been sent to your mobile phone or your logged in device. Please enter the verification code here.
3 |
9 |
10 | {% endblock %}
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # telethon-api-web-app
2 | Tele PostMan - a complete web app based on Telethon API to send messages on Telegram
3 |
4 | # What Tele PostMan does?
5 | It lets you add unlimited Telegram users and lets you select the users from the app and then lets you send messages.
6 |
7 | # What are the underlying technologies?
8 | This is a small webapp built on top of Python (Flask). Telethon is used to connect with Telegram. Sqlite3 is being used to store the data of users.
9 |
10 | # Where can I send messages?
11 | You can send messages to Telegram Users, Channels and Groups too.
12 |
13 | # How does it work?
14 | Add a user, verify the user and then send messages. The user won't have to get verified second time.
15 |
16 | # What's Tele PostMan?
17 | It is a name for the app, I couldn't be bothered to come up with better one.
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/templates/add-new-user.html:
--------------------------------------------------------------------------------
1 | {% extends("index.html") %} {% import "bootstrap/wtf.html" as wtf %} {% block content %}
2 | Add a new user in Database
3 | Step 01: Go here and login using user's phone number.
4 | Step 02: Under API development tools, Create new application window will appear. Fill in your application details. There is no need to enter any URL, and only the first two fields (App title and Short name) can currently
5 | be changed later.
6 | Step 03: Click on Create application at the end. Remember that your API hash is secret and Telegram won’t let you revoke it. Don’t post it anywhere!
7 | Step 04: Enter the API key and API ID
8 |
9 |
10 |
16 | {% endblock %}
--------------------------------------------------------------------------------
/templates/layout.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | Tele PostMan | Post on Telegram with ease
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
24 |
25 | Tele PostMan
26 |
27 |
28 |
29 |
30 |
56 |
57 |
58 |
59 |
60 |
61 | {% block content %} {% endblock %}
62 |
63 |
64 |
65 |
66 |
67 |
68 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 | from flask import Flask, request, render_template, redirect, url_for, session
2 | from flask_wtf import FlaskForm
3 | from wtforms import StringField, PasswordField, BooleanField, SelectField, TextAreaField
4 | from wtforms.validators import InputRequired, Length
5 | from flask_bootstrap import Bootstrap
6 | from flask_sqlalchemy import SQLAlchemy
7 | from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
8 | from telethon import TelegramClient
9 |
10 | APP = Flask(__name__)
11 | APP.config['SECRET_KEY'] = 'somethingsectret'
12 | APP.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////path-to-sqlite-db/telepost.db'
13 | Bootstrap(APP)
14 | DB = SQLAlchemy(APP)
15 | LOGIN_MANAGER = LoginManager()
16 | LOGIN_MANAGER.init_app(APP)
17 | LOGIN_MANAGER.login_view = 'login'
18 |
19 | class User(UserMixin, DB.Model):
20 | id = DB.Column(DB.Integer, primary_key=True)
21 | username = DB.Column(DB.String(15), unique=True)
22 | password = DB.Column(DB.String(80))
23 |
24 | class TelegramUser(DB.Model):
25 | u_id = DB.Column(DB.Integer, primary_key=True)
26 | api_id = DB.Column(DB.String(15), unique=True)
27 | api_hash = DB.Column(DB.String(80))
28 | phone_number = DB.Column(DB.String(80))
29 | status = DB.Column(DB.Boolean())
30 | u_name = DB.Column(DB.String(15))
31 |
32 | class TelegramGroups(DB.Model):
33 | g_id = DB.Column(DB.Integer, primary_key=True)
34 | g_link = DB.Column(DB.String(80))
35 | g_name = DB.Column(DB.String(15))
36 |
37 |
38 | class LoginForm(FlaskForm):
39 | username = StringField('username', validators=[InputRequired(), Length(min=4, max=15)])
40 | password = PasswordField('password', validators=[InputRequired(), Length(min=8, max=80)])
41 | remember = BooleanField('remember me')
42 |
43 |
44 |
45 |
46 | class TelegramUserForm(FlaskForm):
47 | api_id = StringField('API ID', validators = [InputRequired(), Length(min=1, max=15)])
48 | api_hash = StringField('API hash', validators=[InputRequired(), Length(min=8, max=80)])
49 | phone_number = StringField('Phone Number')
50 | u_name = StringField('Name of the User')
51 |
52 |
53 | class SendMessageForm(FlaskForm):
54 | telegram_user = SelectField('Telegram users from Database', coerce=int)
55 |
56 | class VerifyCodeForm(FlaskForm):
57 | telegram_code = StringField('Telegram Verification Code')
58 |
59 | class ComposerMessageForm(FlaskForm):
60 | composed_message = TextAreaField('Message to send')
61 | group_link = SelectField('Select Group', coerce=int)
62 |
63 |
64 | class GroupForm(FlaskForm):
65 | new_group = TextAreaField('Group invite URL')
66 | group_name = StringField('Group name')
67 |
68 |
69 | @LOGIN_MANAGER.user_loader
70 | def load_user(user_id):
71 | return User.query.get(int(user_id))
72 |
73 |
74 | @APP.route('/')
75 | @login_required
76 | def index():
77 | return render_template("index.html", name = current_user.username)
78 |
79 |
80 | @APP.route('/login', methods=['GET', 'POST'])
81 | def login():
82 | form = LoginForm()
83 | if form.validate_on_submit():
84 | user = User.query.filter_by(username=form.username.data).first()
85 | if user:
86 | if user.password == form.password.data:
87 | login_user(user, remember=form.remember.data)
88 | return redirect(url_for('index'))
89 | return ' Invalid Username or Password '
90 |
91 | return render_template('signin.html', form=form)
92 |
93 | @APP.route('/logout')
94 | @login_required
95 | def logout():
96 | logout_user()
97 | return redirect(url_for('login'))
98 |
99 | @APP.route('/add-new-user', methods=['GET', 'POST'])
100 | @login_required
101 | def add_new_user():
102 | form = TelegramUserForm()
103 | if form.validate_on_submit():
104 | new_telegram_user = TelegramUser(api_id=form.api_id.data, api_hash=form.api_hash.data, phone_number=form.phone_number.data, u_name = form.u_name.data)
105 | DB.session.add(new_telegram_user)
106 | DB.session.commit()
107 | return redirect(url_for('index'))
108 | return render_template("add-new-user.html", name = current_user.username, form=form)
109 |
110 | @APP.route('/send-messages', methods=['GET','POST'])
111 | @login_required
112 | def send_messages():
113 | form = SendMessageForm()
114 | users_collection = TelegramUser.query.all()
115 | form.telegram_user.choices = [(g.u_id, g.u_name) for g in users_collection]
116 | if form.validate_on_submit():
117 | global value
118 | value = request.form.get("telegram_user")
119 | user_collection_by_id = TelegramUser.query.get(value)
120 | global api_id
121 | api_id = int(user_collection_by_id.api_id)
122 | session_name = str(api_id)
123 | global api_hash
124 | api_hash = user_collection_by_id.api_hash
125 | global phone_number
126 | phone_number = user_collection_by_id.phone_number
127 | global client
128 | client = TelegramClient(session_name, api_id, api_hash)
129 | status = sendToGroup()
130 | return status
131 |
132 |
133 | return render_template("send-messages.html", name = current_user.username, form=form)
134 |
135 |
136 | @APP.route('/add-new-group', methods=['GET','POST'])
137 | @login_required
138 | def add_new_group():
139 | form = GroupForm()
140 | if form.validate_on_submit():
141 | new_group = TelegramGroups(g_link=form.new_group.data)
142 | DB.session.add(new_group)
143 | DB.session.commit()
144 | return redirect(url_for('index'))
145 | return render_template("add-new-group.html", form = form)
146 |
147 |
148 | @APP.route('/send-to-group', methods = ['GET', 'POST'])
149 | @login_required
150 | def sendToGroup():
151 | code = request.form.get("telegram_code")
152 | message = request.form.get("composed_message")
153 | g_id = request.form.get("group_link")
154 | if g_id:
155 | selected_group = TelegramGroups.query.get(g_id)
156 | selected_group_link = selected_group.g_link
157 | print(selected_group_link)
158 | if not code and not message:
159 | form = ComposerMessageForm()
160 | telegram_groups = TelegramGroups.query.all()
161 | form.group_link.choices = [(g.g_id, g.g_name) for g in telegram_groups]
162 | return render_template("compose-message.html", form = form)
163 | assert client.connect()
164 | if not client.is_user_authorized():
165 | if not code:
166 | client.send_code_request(phone_number)
167 | form = VerifyCodeForm()
168 | return render_template("verify-code.html", form = form)
169 | me = client.sign_in(phone_number, int(code))
170 | return render_template("final-message.html")
171 |
172 | return redirect(url_for('verify_code', api_id = api_id, api_hash = api_hash, phone_number = phone_number))
173 |
174 | else:
175 | lonami = client.get_entity(selected_group_link)
176 | if client.send_message(lonami, message):
177 | return "Message sent. Click here to go back to dashboard "
178 |
179 |
180 | @APP.route('/verify-code', methods = ['GET', 'POST'])
181 | @login_required
182 | def verify_code():
183 | code = request.form.get("telegram_code")
184 | if not code:
185 | form = VerifyCodeForm()
186 | return render_template("verify-code.html", form = form)
187 | send_it(api_id, api_hash, phone_number, code)
188 | return render_template("final-message.html")
189 |
190 |
191 | def send_it(api_id, api_hash, phone_number, code):
192 | assert client.connect()
193 |
194 | if __name__ == '__main__':
195 | APP.run(debug=True, port=3000, host="0.0.0.0")
196 |
197 |
--------------------------------------------------------------------------------