Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

A

Project Based Learning Report


on
Telegram Bot

Submitted in partial fulfillment of the requirements


for the award of the degree of

Bachelor of Technology
in
Computer Science and Engineering

by
Prisha Mohan (2000970100079)
Rohit Saini (2000970100086)
Sagar Malik (2000970100089)

Under the Supervision of


Mr.Dinesh Babu M

Galgotias College of Engineering & Technology


Greater Noida, Uttar Pradesh
India-201306
Affiliated to

Dr. A.P.J. Abdul Kalam Technical University


Lucknow, Uttar Pradesh,
India-226031
December, 2021
GALGOTIAS COLLEGE OF ENGINEERING & TECHNOLOGY
GREATER NOIDA, UTTER PRADESH, INDIA- 201306.

CERTIFICATE

This is to certify that the project report entitled “Telegram Bot” submitted by Ms. Prisha Mohan
(2000970100079), Mr. Rohit Saini (2000970100086), Mr. Sagar Malik (2000970100089) to the Galgotias
College of Engineering & Technology, Greater Noida, Utter Pradesh, affiliated to Dr. A.P.J. Abdul Kalam
Technical University Lucknow, Uttar Pradesh in partial fulfillment of Degree of Bachelor of
Technology in Computer science & Engineering is a bonafide record of the Project Based Learning Report
carried out by them under my supervision during the even semester 2021-2022.

Mr. Dinesh Babu M Dr. Vishnu Sharma


Coordinator Professor and Head
Deptt. of CSE Deptt. of CSE
GALGOTIAS COLLEGE OF ENGINEERING & TECHNOLOGY
GREATER NOIDA, UTTER PRADESH, INDIA- 201306.

ACKNOWLEDGEMENT

We have taken efforts in this project. However, it would not have been possible
without the kind support and help of many individuals and organizations. We
would like to extend my sincere thanks to all of them.

We are highly indebted to Mr. Dinesh Babu M for his guidance and constant
supervision. Also, we are highly thankful to them for providing necessary
information regarding the project & also for their support in completing the
project.

We are extremely indebted to Dr. Vishnu Sharma, HOD, Department of Computer


Science and Engineering, GCET, for the valuable suggestions and constant
support throughout my project tenure. We would also like to express our sincere
thanks to faculties of Department of Computer Science and Engineering, GCET
for their support in completing this project on time.

We also express gratitude towards our parents for their kind co-operation and
encouragement which helped me in completion of this project. Our thanks and
appreciations also go to our friends in developing the project and all the people
who have willingly helped me out with their abilities.

(Prisha Mohan)

(Rohit Saini)

(Sagar Malik)
ABSTRACT

The project aims to develop a Telegram Bot, using Python. Python is an all-round valuable
programming language used in Dialog flow. It is used for the most prominent programming in
light of its versatility, straightforwardness and longstanding reputation. Python is significant
for developers as it shows their programming abilities amongst the prospective employers.
This project will be a best guide to polish your python skills and gain expertise in it. With the
help of this project, the user will be able to easily access different python material for practice
as well as study, all summarized in one place.Telegram is an evolving platform that is famous
for its security, message delivery speed, and lots of development perks that make
communication enjoyable. In recent times Telegram has become one of the most used
messaging and content sharing platforms, it has no file sharing limit like Whatsapp and it
comes with some preinstalled bots one can use in any channels (groups in case of whatsapp)
to control the behavior or filter the spam messages sent by users. Telegram Bot draws its
inspiration from so many existing bots that are available 24/7 and can deliver lots of helpful
information in a blink of an eye. This bot is a helping hand to those who are not already
oriented and need a helping hand. Chatbots are revolutionizing the way people interact with
technology. In recent years, their simplicity and low cost have helped drive adoption across
various fields and industries.
INDEX
CONTENTS

TITLE PAGE

Certificate i
Acknowlwdgement ii
Abstract iii
Contents iv

1.INTRODUCTION

INTRODUCTION
PURPOSE
EXISTING SYSTEM
PROPOSED SYSTEM

2.SYSTEM ANALYSIS

CREATING A BOT
WORKING OF BOT
SECURE
SETTING UP

3.FEASIBILITY STUDY

POPULARITY
APPLICATIONS
EXAMPLES IN INDUSTRY

4.REQUIREMENTS SPECIFICATIONS

SYSTEM REQUIREMENTS
BASIC REQUIREMENTS
TOOLS NEEDED
USER SKILLS
PLATFORM USED

5.SYSTEM DESIGN

DFD DIAGRAMS
UML DIAGRAMS

6.OUTPUT SCREENS

SCREENSHOTS

7.SYSTEM TESTING

8.CONCLUSION

9.FUTURE SCOPE
CHAPTER 1
INTRODUCTION
INTRODUCTION

Chatbots are often touted as a revolution in the way users interact with technology and
businesses. They have a fairly simple interface compared with traditional apps, as they only
require users to chat, and the chatbots are supposed to understand and do whatever the user
demands from them, at least in theory. Chatbots are all the rage at the moment, with some
predicting that they will be bigger than mobile apps. The main idea of chatbots is that
instead of having to dig through awkward mobile menus and learn UIs, you'll simply have
a conversation with a bot through a familiar
instant messaging interface. If you want to order a Pizza, you start a conversation with
the Domino's Pizza bot and have the same conversation with it that you might have with a
human.

You can devise a chatbot that will help your customers when they ask certain questions
about your product, or you can make a personal assistant chatbot that can handle basic tasks
and remind you when it’s time to head to a meeting or the gym.

There are a few different platforms that allow you to build your own chatbot. One of these,
which is arguably the simplest to use and is also growing steadily in popularity,
is Telegram.

Telegram is one of the more popular IM platforms today, as it allows you to store messages
on the cloud instead of just your device and it boasts good multi-platform support, as you can
have Telegram on Android, iOS, Windows, and just about any other platform that can
support the web version. Building a chatbot on Telegram is fairly simple and requires few
steps that take very little time to complete. The chatbot can be integrated in Telegram groups
and channels, and it also works on its own.

PURPOSE

Bots are simply accounts operated by software – not people – and they'll often have AI
features. They can do anything – teach, play, search, broadcast, remind, connect, integrate
with other services, or even pass commands to the Internet of Things. Telegram Bots are
special accounts that do not require an additional phone number to set up. Bots are
everywhere. It seems that only yesterday we did not even know about their existence; now
we can barely imagine our life without them. They’ve become widely popular among
numerous active users of messengers since they have various scopes of use – from
entertaining content including step-by-step games and collecting bonus points in restaurants,
to keeping a diet plan, tracking deliveries, and even making payments for different services.
A Telegram Bot is a program that behaves like a normal chat partner with additional
functions. It performs predefined tasks independently and without the user’s involvement.
Many industries are shifting their customer service to chatbot systems. That’s because of the
huge drop in the cost compared to actual humans, and also because of the robustness and
constant availability. Chatbots deliver a degree of user support without substantial additional
cost. Today, chatbots are used in many scenarios, ranging from menial tasks such as
displaying time and weather data to more complex operations such as rudimentary medical
diagnosis and customer communication/support.

EXISTING SYSTEM

- Simple telegram accounts operated by an application

- Not actually designed for chatting

- Can be used to simplify the task

- Teach

- Play

- Search

- Broadcast

- Remind
- Connect

- Users can interact with them via

- Message

- Emoticons

- Commands

- Inline requests

Limitation of the Bot

1. Only user can initiate the chat


2. The download limit is 20MB and upload limit is 50MB only
3. Send messages limit is capped up to 30 msg per second. However, you can
ask @botsupport to increase it.
With this limitation, comes another power, bots can edit their own messages and another’s
messages in the channel where the bot has admin rights.

PROPOSED SYSTEM

The Telegram messenger stands out, compared to competing services such as


WhatsApp and Co, especially because of its special function of bots and channels.
Freely available application programming interfaces (APIs) make life easier if you
want to collect data or automate things. Thus, a Telegram bot is a good way to
deliver information from almost every conceivable area whenever you need it. In
the following article, we would like to discuss exactly this special mode of
operation and show how it can be used in practice.

FEATURES
 A generic and predefined text response
 A context using data that the user provides
 A specific action (the chatbot interacts with one or more applications)
 Reducing costs of personnel that provided customer service
 Immediate support and responses for users
 Better user experience
 Possibility of learning about preferences of the user when using artificial intelligence
systems
 Ease of integration in different digital platforms, without the need to download
applications or additional programs.
 The word or statement that we have written in the bot gives us the desired Youtube,
geeksforgeeks etc link related to it.
CHAPTER 2
SYSTEM ANALYSIS
Creating a Telegram Bot

How can i create an account?


To create a Telegram Bot yourself, all you have to do is install the Telegram app on
a device and log in there to create your personal account. You can find the
Telegram app for mobile devices in the app stores (e.g. Google Play Store or Apple
App Store) and for other devices e.g. on the official Telegram
website https://telegram.org/.

How can I create the bot?


The actual creation of your own bot is generally very simple. You are supported by
the virtual user “Botfather”, which is the central development tool for Telegram
bots. So you first have to search for this user on Telegram and s tart a chat with
him. Make sure that you select the verified (with a blue tick) user.

What is the Botfather?


The Botfather (the name is an allusion to the Mafia epic “The Godfather” – based
on the novel by Mario Puzo and successfully filmed in three parts with Marlon
Brando as “The Godfather” from 1972) is basically a Telegram Bot and responds to
commands. It helps with the creation of one’s own bot, organises unique bot names,
for example, and assigns access authorisations for programming.

How do I set up my bot? (A quick guide)


Start by entering the command “/newbot” as a text message in the chat with the
botfather. Now you have to give your bot a name such as “My first bot for testing”.
The user name of the bot is then requested. This must end in “bot”. In our case, for
example, “my_first_test_bot”. Now the botfather should congratulate you on setting
up the new bot and tell you the address and the personal “token”. This is, so to
speak, the password for complete control of the bot. The basic setup of th e
Telegram bot is now complete. Now the bot only needs to be filled with
functionality or programmed.
How does the programming work?

What programming languages are available for bot programming?


In principle, all programming languages that run on a serve r and can respond to
requests via HTTPS can be used. Here is a selection of popular programming
languages that are often used for web and app development:

 JavaScript (node.js)
 Java / Kotlin
 PHP
 C#
 Python
 Swift
 Ruby

When choosing a programming language for bot development, different points come
into play: How “well” do they or their team know the respective programming
language? Which programming tools are available or which libraries can be
accessed?

What does the OPC Router option offer?

With the OPC Router communication platform, a Telegram Bot can be created with
just a few clicks without complex programming. With the OPC Router Telegram
Bot Plug-in, the bot is realised through simple graphical configuration. Messages
from other Plug-ins (SAP, OPC, MQTT, etc.) can be easily sent to the bot and are
then sent to the registered chat users. The creation of commands is possible via the
simple creation of triggers in an OPC Router project. In this way, you can create a
Telegram Bot for your own system in just a few minutes.

What costs will I incur?


The use of a Telegram bot, like the use of Telegram itself, is free of charge. Only
the possible costs for the acquisition of programming languages, tools or interfaces
to machines to be connected must of course be taken into account for a self -
development.

How secure is a Telegram Bot?

Where is the bot running?


If one considers the Telegram Bot in the context of the data source, the actual data
usually remains on the local servers of the data source. The bot accesses the
required data sets there and then prepares them for message output as requested on
the end device. As listed in 2.4, only the actual programme structure is permanently
located in the cloud on the Telegram servers.

How high is the application security?


The actual Telegram bot application with the relevant data is stored on its own
computers and databases. Here, the application security depends on the
administrator of the local PC environment on site. The Telegram Bot only delivers
the message outputs defined in the application to the respective end devices. Only
these message outputs are then located in the Telegram Cloud.

How is the Telegram data encrypted?


All messages and thus also the communication with a bot are secured via client -
server/server-client encryption. In the Telegram Cloud, this data is also divided
among a number of servers and stored there in encrypted form.

Setting up

Nearly everything we do will be achievable using only the standard Python libraries, but
we'll also be using the third-party requests module which provides a number of
improvements to Python's urllib, and allows us to make HTTP requests very simply and
concisely. Install this through pip using a command similar to the following (you may need
to use pip instead of pip3 and/or add the --user flag, based on how you usually install
Python libraries).

pip3 install requests

If you usually use a virtual environment for new Python projects, then set one of those up
first, and install requests inside that.
CHAPTER 3
FEASIBILITY STUDY
Why are they so popular?

1) Minimalistic and simple design.

Well, the bot simply cannot have colorful design. But beyond any doubts, in comparison to
numerous apps with different design when you have to remember where and what to tap, bot
is more universal and easy; it offers a simple communication via texts.

2) Bot has minimum of advertising and focuses on the users’ needs.

You do not have to install hundreds of apps for each service if you can receive all necessary
assistance from a bot. This is particularly useful for restaurants and shops. Clients are rarely
eager to install apps from a bunch of places they’ve visited. Due to this, business owners
miss clients’ feedback and lose communication with them. If each of these places had their
own bot available in different messengers, it would be more convenient and friendly to users.
Nobody likes to fill up the storage of their phones with unnecessary apps that will be used
once or twice. However, clients need to engage with the service owners and they will
appreciate doing this through their favorite messenger.

3) No need for signing up, authorization, and constant relogin

Using a bot, user passes authorization only once when the bot is added to the chat. Client can
use the bot as much as it is necessary, and when there is no need in it anymore, user just
blocks the bot. That’s all, easy! No password resets anymore. No need to remember
passwords or logins used. Adding bot to the site or app increases the number of the user
audience as it makes communication with clients and providing them with the assistance
much easier and user-friendly.

APPLICATIONS
 Tutoring
 Increasing student engagement
 Efficient teaching assistants
 Assisting students
 Student support
 Providing institutional information
 Helping with holiday homework
 Assessments

Examples of applications in industry

1. How can you use a Telegram bot for resource control?


In the injection moulding industry, for example, a Telegram bot can send an
automated message to a smartphone when the level of plastic granulate required for
the production of injection moulded products falls below a certain level. In this
way, it is possible to react ahead of time with a reorder or additional delivery and
there is no downtime due to a shortage of material.

2. What machine information can be transmitted?


Relevant machine data such as temperature or pressure information can be sent
regularly via Telegram Bot to the tablets, smartphones or PC workstations of the
department managers. In this way, a complete overview of machine production is
possible for this user group at any time.

3. How can I use the functionality for fault messages or alarms?


Effective fault or alarm reporting systems with smartphone connection can be
configured via bot. For this purpose, fault messages that are managed, generated
and stored in an SQL database, for example, are sent to the logged -in Telegram
clients via the bot. The OPC Router can also offer commands to the clients so that
fault message acknowledgements and comment functions can be designed. With the
Telegram Plug-ins access to the SQL data, the Telegram user can also be offered
queries, for example to access detailed data on fault messages.
4. What maintenance information can a Telegram bot send me?
If the functionality of production machines reaches certain predefined threshold
values, a Telegram bot could automatically generate maintenance recommendations
and send them to the respective machine operators. In this way, maintenance -related
failures can be avoided and downtimes can be effectively planned in advance.
CHAPTER 4
REQUIREMENT
SPECIFICATION
SYSTEM REQUIREMENTS

 OPERATING SYSTEM – Microsoft Windows 7 or Higher

 CPU PROCESSOR - Intel Core i3 or equivalent

 RAM REQUIRED – 2GB (32bit) / 4GB (64bit)

 DISK SPACE – 1.5 GB of free disk space

 Integrated Development Environment (IDE)

 Telegram Application

Basic Requirements

 A Telegram Account: If you don’t have the Telegram app installed just download it
from the play store. After downloading create an account using your mobile number just
like WhatsApp.
 .python-telegram-bot module: Here we will need a module called python-telegram-
bot, This library provides a pure Python interface for the Telegram Bot API. It’s
compatible with Python versions 3.6.8+. In addition to the pure API implementation,
this library features a number of high-level classes to make the development of bots
easy and straightforward. These classes are contained in the “telegram.ext” submodule.
For more information, you can check their official GitHub repo.

Tools Needed

 bottle – for our server; a simple and lightweight WSGI micro web-framework

 requests – for sending requests to telegram. request lib does not need to be
overrepresented. It is universally used throughout the world in a variety of projects.
Note: you have to install these tools on your computer. We will need them later. For
that, open your bash console and install it via pip
 ngrok – this is an app which provides us with public URLs for our interaction with
Telegram WebHook throughout the development phase (look for info on WebHook
below). It is useful, as Telegram will not be able to make a connection with our local
server because we cannot specify our local address in the Telegram API configuration.
You should download ngrok from the official site and put the installed app in the
folder with the project.

User Skills
 Some basic Python knowledge to follow this tutorial

 You should be comfortable with running commands in a Linux Shell, a MacOS


Terminal, or a Windows Command Prompt

 You should be able to install Python packages using the pip package manager (or
conda if you're more comfortable with that)

 Ideally, you should have written at least a basic SQL statement before, but this is not
strictly necessary (and will only be relevant in Part 2).

All of the code is aimed at Python 3.5, but it should be easily adaptable to other versions of
Python.

PLATFORM USED

 Programming Language - Python


 Compiler IDE - Visual Studio Code

INTRODUCTION OF COMPONENTS
Programming Language - PYTHON
Python is an interpreted high-level general-purpose programming language. Python's design
philosophy emphasizes code readability with its notable use of significant indentation. Its
language constructs as well as its object-oriented approach aim to help programmers write
clear, logical code for small and large-scale projects.
Python is dynamically-typed and garbage-collected. It supports multiple programming
paradigms, including structured (particularly, procedural), object-oriented and functional
programming. Python is often described as a "batteries included" language due to its
comprehensive standard library.
Python consistently ranks as one of the most popular programming languages
Why we chose python?
If you want your machine to run on your command like Jarvis did for Tony, yes, it is possible.
It is possible using Python. Python offers a good major library so that we can use it for making
a virtual assistant.
Python is the most used language for AI with multiple libraries and huge support. Apart from
being an open-source programming language, python is also one of the most versatile
programming languages.

Python is free and simple to learn. Its primary features are that it is high-level, dynamically
typed and interpreted. This makes debugging of errors easy and encourages the rapid
development of application prototypes, marking itself as the language to code with.

Features in Python

GUI Programming Support: Graphical User interfaces can be made using a module such
as PyQt5, PyQt4, wxPython, or Tk in python.
Free and Open Source: Python language is freely available at the official website and you
can download it from any given download link with Download Python keyword.

Object-Oriented Language: One of the key features of python is Object-Oriented


programming. Python supports object-oriented language and concepts of classes, objects
encapsulation, etc.

High-Level Language: Python is a high-level language. When we write programs in


python, we do not need to remember the system architecture, nor do we need to manage the
memory.

Extensible feature: Python is an Extensible language. We can write us some Python code
into C or C++ language and also we can compile that code in C/C++ language.

Python is Portable language: Python language is also a portable language. For example,
if we have python code for windows and if we want to run this code on other platforms
such as Linux, Unix, and Mac then we do not need to change it, we can run this code on
any platform.

Interpreted Language: Python is an Interpreted Language because Python code is


executed line by line at a time. like other languages C, C++, Java, etc. there is no need to
compile python code this makes it easier to debug our code.

Large Standard Library: Python has a large standard library which provides a rich set of
module and functions so you do not have to write your own code for every single thing.
There are many libraries present in python for such as regular expressions, unit-testing,
web browsers, etc.

Features Used in this project:


1. Library Functions of python:
Python has a large standard library which provides a rich set of module and functions
so you do not have to write your own code for every single thing.
Modules imported in our project:

1. import constants as keys


2. import responses as R
3. From telegram.ext import *

Compiler IDE – Visual Studio Code


Visual Studio Code is a source-code editor made by Microsoft for Windows, Linux and
macOS. Features include support for debugging, syntax highlighting, intelligent code
completion, snippets, code refactoring, and embedded Git. Users can change the theme,
keyboard shortcuts, preferences, and install extensions that add additional functionality.
CHAPTER 5
SYSTEM DESIGN
DATA FLOW DIAGRAMS

ZERO LEVEL DFD


FIRST LEVEL DFD
SECOND LEVEL DFD
UML DIAGRAMS
CHAPTER 6
OUTPUT SCREENS
SCREENSHOTS
CHAPTER 7
SYSTEM TESTING
How to Install the python-telegram-bot Module?

In order to install the Python module, we need 'pip', a framework to manage packages
required to install the modules from the trusted public repositories. Once we have 'pip', we
can install the python-telegram-bot module using the command from a Windows command
prompt (CMD) or terminal as shown below:

Syntax:

1. $ python.exe -m pip install python-telegram-bot

Verifying the Installation

Once the module is installed, we can verify it by creating an empty Python program file and
writing an import statement as follows:

File: verify.py

1. import telegram
2. Now, save the above file and execute it using the following command in a terminal:
3. Syntax:
4. $ python verify.py

If the above Python program file does not return any error, the module is installed properly.
However, in the case where an exception is raised, try reinstalling the module, and it is also
recommended to refer to the official documentation of the module.
CHAPTER 8
CONCLUSION
The Telegram messenger stands out, compared to competing services such as
WhatsApp and Co, especially because of its special function of bots and channels.
Freely available application programming interfaces (APIs) make life easier if you
want to collect data or automate things. Thus, a Telegram bot is a good way to
deliver information from almost every conceivable area whenever you need it. In
the following article, we would like to discuss exactly this special mode of
operation and show how it can be used in practice.

A Telegram bot is a useful addition to smart communication, especially in


professional areas. Compared to the most popular messaging services, Telegram
offers additional advantages in the exchange of information with the special queries
and actions of a bot listed in our article. However, like all programming, Telegram
usually requires concrete programming knowledge for extensive use. Particularly
for use in industry and machine connection, the OPC Router is a recommendable
tool that extremely simplifies precisely this complex programming.

Once programmed, the Telegram Bot can perform many beneficial tasks.
Programming requires specific knowledge or can be extremely facilitated using
software such as the OPC Router.

A potential use case is temperature data that is stored in an SQL database. If this
data exceeds or falls below a threshold value, the Telegram Bot can immediately
send a message to the responsible person or department. Additionally, this message
allows the user to perform a database query. That provides more precise information
about the temperature problem and prevents a production stop at an early stage.

A Telegram Bot can also share production data. As soon as a production order is
complete, departments involved can receive a message and initiate the next order
from there or inform logistics to start shipping. That helps to avoid downtime and
increase overall plant effectiveness. Due to its intelligent communication, the
Telegram Bot is a valuable addition, especially in industry.
CHAPTER 9
FUTURE SCOPE
We intend to expand our model of python Bot to various other skills which makes one
industry-ready through study and practice, namely by creating another set of bots, namely for
ever-growing technologies such as Artificial Intelligence, Machine Learning, Augmented
Reality, Cloud Computing; and another for learning various programming languages such as
JavaScript, Python, C# etc. We intend to administrate all these bots through a master bot,
which then directs the user towards the bot which helps him/her in learning whatever skills
that he/she wants to.

The most obvious problem with our Bot is that we have to run a Python script manually
every time we want to interact with it. Also, as mentioned before, we always download the
entire message history that Telegram provides. This is both inefficient and unreliable, as
we don't want to unnecessarily download the entire message history if we only want a
single message, and because Telegram only keeps this list of updates for 24 hours. Another
issue is that we pass our message as a string, but because this is converted to a URL before
being sent to Telegram, you'll notice that some unexpected things happen if you send
messages to the bot with special characters (for example, the + symbol will disappear from
all echoed messages). Finally, the Bot throws an index error if we try to run it when there
are no new messages to receive.

We'll now update our bot to:

 Constantly listen for new messages and reply to each.

 Acknowledge each message as it receives it and tells Telegram to not send us that
message again.

 Use Long Polling so that we don't have to make too many requests.

 Correctly encode our messages to account for URL formatting.

You might also like