This project is built with the help of laravel-zero.
The aim of this bot is to "seamlessly" duplicate tweets from any public Twitter account to Telegram channel by leveraging Twitter API v2.0 and Telegram Bot API.
- Possibility to duplicate Twitter account feeds to multiple Telegram channels. E.g. you can have 1 Twitter account to post to multiple Telegram channels or 2+ separate Twitter to Telegram pairs.
- Possibility to extend and add your own handlers (pipes) for Twitter feed manipulation. E.g. if you want to do some manipulation with text from tweets, or maybe you don't need to see any gifs with cats in your Telegram channel you can make that each tweet can be handled as you see fit.
- I finally decided to make this bot as a standalone app (!), although you still need a php ^8.1 to run the script on your machine.
- And as a result of the previous point you can now pass all the necessary configuration as arguments to the bot itself on the command line.
If you want to use this bot, firstly you need to do some preparations on Twitter and Telegram side. This app can be used for any Twitter account.
- Create telegram channel.
- Create telegram bot (through @botfather), remember it's token to access the HTTP API.
- Add the newly created telegram bot to your telegram channel of choice and give it access to "Post messages". Note: one telegram bot can be added to multiple telegram channels.
- Create an app on developer.twitter.
PHP ^8.1
-
From the command line run:
php ttt manage --help
This will print help message for the bot.
-
If you wanna do the
.env
stuff then copy.env.example
file to.env
file and fill in all the necessary variables OR fill in the environment variables on your server. The values forTWITTER_*
can be found on developer.twitter. The value forTELEGRAM_ACCESS_TOKEN
should be retrieved when you created your telegram bot. -
If you wanna use bot's optional arguments than the same approach for filling in the values applies to all the
--twitter-*
and--telegram-access-token
optional arguments. -
Fill in bot's required argument(s) (
<ids>
). It should be a string with a digit form of twitter id on the left and telegram channel id on the right delimited with:
(colon).- Twitter account id is an id in digit form, to convert your
@twitterid
to digit form you can use this website. - Telegram channel id is the id in form
@telegramchannelid
.
This required argument (
<ids>
) can be repeated multiple times. E.g.:php ttt manage "1111:@telegram1" "2222:@telegram2" "3333:@telegram3"
- Twitter account id is an id in digit form, to convert your
If you want to build from source or to help the project and fix some bugs or add some features you
can leverage the Dockerfile
included within the project.
- Run
docker-compose up -d
in terminal. - Run
docker-compose exec app sh
to log in to the container. - If you want to build the app then run
php ttt app:build <your-build-name>
. - The build will be in the
build
folder.
If you want to add some custom handlers for the tweets, e.g. add some text to it or remove possibility to post images to Telegram channel than you can add your own Pipelines.
Pipelines lie in Pipelines
folder and there you can add you own PipelineFactory
to handle your
tweets as you see fit. PipelineFactory
has a chain of Pipes
to handle tweet content.
The default behaviour for tweet handling lies in DefaultPipelineFactory
.
Each Pipeline consists of Pipes that handle different parts of the tweet and changes the main
DataHub
object. Depending on whether tweet has some media (like videos, gifs, photos) or not,
it changes DataHub
object to represent what the resulting TelegramPayload
will be.
It's sort of like Chain of Responsibility pattern.
For example if you want to add something to the text or format it somehow you can add your
custom Pipe after default TextPipe
to format the text as you want.
The config/pipelines.php
configuration file has a map of pipelines. When you add your new Pipeline
you need to register it in this file with your custom name. Right now it has only on pipeline called
'default'
.
In order to use custom Pipeline you need to add another optional argument to the bot when running it.
The format with this argument is the same as with the required argument: telegram id to the left and
pipeline name from pipelines.php
file to the right delimited with :
(colon). E.g.:
php ttt manage 1111:@telegramchannelid --channel-pipeline=1111:customPipelineName
- Unit Tests
- Feature Tests
- Logging
- Polls