This slide is going to guide you of how to setup a project in python to test network connection layer and application connection layer of 2 servers through telegram group and telegram bot
4. What you need!
Googlespreetsheet: This is a list of any bank
servers or any urls which you would like to test a
connection or ping to.
TelegramBot: This would be done by botfather
in telegram. It would act as a human to perform
sending/receiving message and push to telegram
group.
TelegramGroup: We need to create one group
and add all members including the bot to
perform any actions.
5. Authorizing Google Spreadsheet
1. Google Developers Console: create a
cloud developer account with your
email
2. Create a new project and select that
project to work on
3. Click on APIs & Services
a. Confirm OAuth consent screen
b. Enable APIs & Services: Drive API
and Google Sheet API
c. Create Credential
8. Credentials
There are 3 types of credential:
1. API Keys
2. OAuth Credentials
3. Service Account
Today we’re going to user service
account through pysheets library
since we don’t want to have
refresh token.
10. TelegramBot
1. Go to telegram and search for
BotFather
2. Start conversation
3. Type /newbot
4. Make sure to keep your token in safe
Link of how to create telegram bot
13. Coding Time
After finish setting up
everything. It’s time to do
coding. In this example we
would like to use Python to
create the application. You can
find the source code in this
gitlab
14. Authorize Sheet
Remember that we have save the service account json file before. Here is the code snippet of
how we can access googlesheet with that service account using python lib:
def authorization():
# https://pygsheets.readthedocs.io/en/stable/authorization.html
creds = service_account.Credentials.from_service_account_file
(
'/home/dev/open-banking/service-account.json'
,
scopes=('https://www.googleapis.com/auth/spreadsheets'
,
'https://www.googleapis.com/auth/drive'
)
)
16. Add handler for telegram
bot
In this section, we will need a help
from python lib which is called
pyTelegramBotAPI. Actually, there
are many different libs that could do
this.
Keynote for choosing a lib, check
start on lib repository and last
update of that lib.
17. Server Configuration
In here, we are using ssh(passwordless) to login from one server to another server.
Since targeted server(open banking server) doesn’t have internet connection, we will
run our application on host server which it can access internet in order to push
message to telegram group.
1. Generate private and public keys in host server: ssh-keygen A public key file
“~/.ssh/id_rsa.pub” and a private key file “~/.ssh/id_rsa” will be generated
2. Copy the public key file to the remote machine:
ssh-copy-id remote_username@remote_server_ip_address
3. Login to your server using SSH keys:
ssh remote_username@remote_server_ip_address
18. Run the application on Server
Setup
● Copy your python files and credential file to server
● Change file_path of credential in python file
Installation with requirements
● sudo apt install python3
● pip3 install -r requirements.txt
Run service in background process
nohup python3 telegram_alert.py start > telegram_alert.log 2>&1 &
** nohup python3 file_name start > log_file_name 2>&1 &