A step-by-step instruction on how to set up Telegram channels’ ads for conversions on Facebook Ads. After each successful subscription to our channel on Facebook you will get conversion. It is implemented due to the Python script (written by Adam) that tracks information about new subscribers via the Telethon library. Conversions are sent to the Keitaro tracker and through Facebook’s Conversion API.
To install and properly run the script we will need:
Server installation and preparation
Buy a VDS-server, for example on FriendHosting, choose Start tariff plan (you can choose another one), then the operating system Ubuntu 22.04 64 bit and FastPanel.
After 30 minutes you should get an activation email:
Follow the address from the FastPanel email, log in with the username and password:
Click Create a website:
Choose manual creation of a website:
In the “Which domain to connect” field, type “telegram-keitaro-capi.com” – we will use it in commands for the terminal/console in the future. Click to proceed to the next step:
Turn off the database by clicking on the appropriate toggle switch and click Create a website:
The website is created, now go to the website card:
Scroll down and go to FTP accounts:
Click on Edit an already created FTP account on the right side:
Generate or enter your own password. Save the login and password in the notepad to avoid losing data. Save:
Next, use an FTP client. We use Filezilla, click on the Site Manager on the top left of the program:
Select a protocol – FTP. Add IP-servers to the Host field. Add the login of the FTP account to the User field. Click connect:
In the new window enter the password of the FTP account and click OK:
After logging in, delete the files in the root directory:
Script preparation
Download the ready-to-use script from the “Adam’s Blog” Telegram channel:
You need to add the missing text file “links.txt” to the archive with the script. To do this unzip the downloaded archive and create an empty file “links.txt” in the “invites_rotator_php” folder:
Zip all the script files and upload them to the server via FileZilla:
Return to the FastPanel tab, go to Files:
Click on the uploaded file, then click on the triplet on the right and select “Unzip”:
Open the file “config.py”:
The data we need to change:
Next, let’s see where to get this data.
How to get data from Telegram and Keitaro
- api_id = ‘20212727’
- api_hash = ‘1fa0ce4a271a1971a396ff26316f0112’
Go to https://my.telegram.org/auth?to=apps, enter the phone number from Telegram and click Next:
Enter the code that you receive via Telegram, click Sign In:
Enter random names in App title and Short name lines. Choose Platform -> Android and click Create application:
The api_id and api_hash data is obtained:
- phone_number = “+123467890”
Enter the phone number from your Telegram account.
- channel_id = -123467890
In the same Telegram account create a Telegram channel with a private access by an invite link (the script’s performance depends on it):
Go to the Telegram-bot and forward the message from the Telegram channel to find out the ID:
- kt_address = ‘https://domen.com’
Enter the domain that is linked to the Keitaro tracker and uses the https protocol.
- kt_token = ’37d3312100te0s9a1a01e999f7d92e3e’
To create an API key in the Keitaro tracker, go to Admin -> Profile -> API keys -> Create a new key:
- kt_timezone = ‘Europe/Moscow’
Set the timezone that you have in the Keitaro tracker profile:
After entering all the data, save the “config.py” file:
Creating invite links and saving hashes
Create 10 or more invite links on the Telegram channel for the script to work correctly with the Keitaro tracker:
Open Fastpanel and go to the “invites_rotator_php” folder and open the “links.txt” file:
Copy all the created invite links into it in hash format (without https://t.me/+) and click Save:
Installing the script in the server
Next, open Console (Windows) or Terminal (macOS), in our case it is Terminal:
Connect to the server using the command:
ssh LOGIN@IP-SERVER -p PORT
In the example:
ssh [email protected]**.**3.26 -p 3333
Enter the password:
Enter the command:
cd root directory
In the example:
cd /var/www/telegram_kei_usr74/data/www/telegram-keitaro-capi.com
Take the address of the root directory from FastPanel -> Site Map:
Installing Python:
Command for macOS:
apt install python3-pip
Command for Windows:
apt install python-pip
Install dependencies:
Command for macOS:
pip3 install -r requirements.txt
Command for Windows
pip install -r requirements.txt
Update packages:
sudo apt-get update
Install the screen package:
sudo apt-get install screen
Create a new session in console/terminal to run the script offline:
screen -S telegram-keitaro-capi
After the new tab appears in the console or terminal enter the command:
Command for macOS:
python3 main.py
Command for Windows:
python main.py
Enter the password that you have received via your Telegram account:
*If you have errors after signing up, you may have logged out of your Telegram account or entered incorrect data in the “Config.py” file.
**We do not recommend creating a Telegram account with a virtual number as you can lose access to it after logging out.
Offline mode
IMPORTANT: After logging in to the Telegram account, press the following key combination in the opened Terminal or Console tab to run the script in the offline mode:
- CONTROL + A + D (on macO)
- CTRL + A + D (on Windows)
Setting up a Facebook source in Keitaro
Create or edit a source for Facebook:
In “External ID” remove the value “{fblid}” in the “fbclid” parameter, since Facebook passes this value automatically:
Put the parameters into “sud_id_7“
- Name: pixel ID;
- Parameter: pixel;
- Value: insert the pixel from the Facebook ad account (from which you’ll drive the traffic);
Put the parameters into “sud_id_14“
- Name: Campaign Domain;
- Parameter: domain;
- Value: Specify the campaign domain (without the protocol) for which you run ads from Facebook;
Put parameters into “sud_id_15”
- Name: FB Token;
- Parameter: token;
- Value: In the example, the token is taken from the Facebook app. Instructions on how to create it;
*Do not change other parameters and leave them as standard.
Configuring Offers in Keitaro
Next, you need to create local offers for each invite in the tracker and customize them.
Detailed instructions on how to add local offers in the Keitaro tracker: https://cpa.rip/services/keitaro-tracker/#Lokalnyj_offer
First, follow each previously prepared invite link from the Telegram channel and download the pages opened in your browser with the plugin:
Each downloaded page will be uploaded to a separate local offer in the tracker. Before downloading, be sure to change “index.html” to “index.php”:
Then in each local offer in the file “index.php” put the following code at the beginning:
<?php function updateParams($params) { global $rawClick; $subId=$rawClick->getSubId(); if (!empty($subId)) { $result = file_get_contents('http://ТРЕКЕР_ДОМЕН/?_update_tokens=1&sub_id=' . urlencode($subId) . '&' . http_build_query($params)); } } updateParams(array( 'sub_id_13' => 'ХЭШ ссылки Telegram-канала' , )); ?>
In this code it is necessary to put the IP address of your Keitaro tracker instead of “TRACKER_DOMAIN” and enter the invite hash (corresponding to the downloaded page) in the parameter “sub_id_13” instead of “Telegram channel link hash”.
Important! For each downloaded landing page, you need to put the corresponding hash into the code. Example: if we downloaded a page by following the link http://t.me/+wfla4raHVVAyM2NI, we should insert the hash from this link as sub_id_13, i.e. wfla4raHVVAyM2NI.
You can do this before uploading the file to the tracker or in the tracker itself by clicking the “<>” icon in the Keitaro’s offer editing mode:
Make the necessary edits in the editor:
Setting up a campaign in Keitaro
- Select the domain from which the ads traffic will drive to Facebook;
- Select the customized Facebook source;
- Select localized offers;
- Choose to rotate streams by weight and link them to streams, landings, and offers.
Facebook ad launch settings
Copy the link from the Keitaro campaign and paste it into the Website URL but without parameters (https://domain.com/identifier).
Go below and paste all the parameters into the “URL parameters” field:
After all the settings we launch the campaign.
Transferring leads via API from Telegram to Facebook
When a user subscribes via our Keitaro link, the script sends an API event to the Facebook campaign:
Data transfer to Keitaro tracker
The script passes the following data about the user who has subscribed to Keitaro: first name, last name, @username (nickname), phone number (if not hidden), etc.
Additional commands
The following commands may be useful while working with the script.
Check open sessions
screen -ls
Go to an open session
screen -r telegram-keitaro-capi
A working session is opened:
Create a new session
screen -S telegram-keitaro-capi
Quit the session
screen -X -S SESSION NAME quit
Example: screen -X -S 57612.telegram-keitaro-capi quit