Sync Tweets to a Telegram Channel Using Account Activity API
Tags: tutorial, telegram, python, twitter
Published: 2020-03-21 06:30:00

---
title: Sync Tweets to a Telegram Channel Using Account Activity API
published: true
date: 2020-03-20 16:52:04 UTC
tags: tutorial,Telegram,python,twitter
canonical_url: https://blog.1a23.com/2020/03/21/sync-tweets-to-a-telegram-channel-using-account-activity-api/?utm_source=rss&utm_medium=rss&utm_campaign=sync-tweets-to-a-telegram-channel-using-account-activity-api
cover_image: https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/Alt-Copy-2.png
---

Yet another post that [has something to do with Telegram](https://blog.1a23.com/tag/telegram/). Yeah, I know, but there’s never such thing as too much when you talk about blog articles.

A lot of people around my Telegram circle has been maintaining their own channels, and a lot of them has had a few hundreds or even thousands of subscribers. I think that I may make one too, but I also don’t want to give up with [my Twitter account](https://twitter.com/blueset) which is more accessible to search engines. So why not sync my tweets to the channel? Given the openness of both Telegram and Twitter, this shouldn’t be much of an issue.

The first option I turned to is, of course, [IFTTT](https://ifttt.com/). It is one of the most famous solution for casual automation. I have also used it [in another script](https://blog.1a23.com/2020/03/03/monitor-connected-devices-to-an-asus-router-using-raspberry-pi/(opens%20in%20a%20new%20tab)). The problem of IFTTT is that the option it provides is _too_ simple. There is no text transformation, no condition and no everything I would need for an ideal forwarder. It even drops line breaks when quoting the tweet content.

Soon after I got time, I replaced the IFTTT bot with my own one for a better presentation and granular control over it.

## The bot

Setting up a Telegram bot for this purpose isn’t hard, especially when it doesn’t require any input. In fact you can even reuse bot that you already have for this (which is what I was doing).

Add the bot to a channel as admin, get the ID/username of the channel, and you are good to go.

The hard part is to create a Twitter app to use the Account Activity API. You have to fill a quite lengthy survey telling them why you want to make an app and how are you going to use the API. Only after they have approved your request, then you can proceed to the next step.

_Note that Twitter has been pretty strict on the quota of the Account Activity API ever since they have moved from the Streaming API ([which has broken most third-party clients](https://www.theverge.com/2018/8/16/17699626/twitter-third-party-apps-streaming-api-deprecation)). You can’t share your API key and secret to more than 15 accounts if you are on the Sandbox (free) plan, or you may be asked to pay for the bill._

Twitter ended support for push notifications and auto-refreshing timelines today, saying it needed to focus on its native apps

Also, since the new Account Activity API is webhook-like, that means you have to figure out, in one way or another, to expose your bot as an HTTP entry point. I used my existing Nginx web server to forward request to the bot. Other methods should also work.

I am using a Python library called [Twitivity](https://pypi.org/project/twitivity/) which provides a simple Flask web server and some helper functions, [PickleDB](https://pypi.org/project/pickleDB/) for simple file-based key-value storage, and [Python Telegram Bot](https://github.com/python-telegram-bot/python-telegram-bot) for the bot.

### Setup environment

Once you have set up your Twitter App, you can go to the [Dev Environments](https://developer.twitter.com/en/account/environments) page to create an environment for your Account Activity API. The environment label (e.g. `env_name`) will be used later in the code.

If you are running the bot on your own account, go to your app page and choose “Keys and tokens”. From there you can get your API key/token and access key/token in one go. For users other than yourself, you need to setup authentication manually to get the tokens.

When you have these tokens ready, you can then register your webhook with Twitter.

{% gist https://gist.github.com/blueset/390e146c322d277b61f99066b5ca4ecf file=register.py %}
<figcaption>Python source to register webhook with Twitter.</figcaption>

Once the webhook is registered, you can copy over the config to the actual bot file, and start it up. Since it runs flask in the background, you can actually use all the fancy uWSGI and Gunicorn stuff to maintain the bot, but a simple flask dev server should suffice if you don’t tweet 100 times per second.

{% gist https://gist.github.com/blueset/390e146c322d277b61f99066b5ca4ecf file=tweet_to_telegram.py %}
<figcaption>The bot script</figcaption>

As you might have seen above, the bot itself has quite a complicated logic. The bot would identify the nature of a tweet and treat each kind of tweet differently.

- For plain tweets, the bot would expand all shortened links back as the length limit here isn’t that strict. Link preview will only be enabled if a link is found in the tweet.
- For tweets with media, the bot will send them as picture/video (or media group) messages. Thank to the fact that Telegram Bot API accepts external media URL, we don’t need to download and upload again.
- For retweets with comments, only the comment is copied over.
- For likes and plain comments, the original tweet is only shown as link preview.
- Every message sent has links to the original tweet, and the source tweet too if its a like or retweet. (The links are on the emoji at the end of the messages.)
- If the tweet is a reply to something that we already have, it will reply to the previous message in the channel.

Some screenshots here:

![A plain tweet](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-1.png)<figcaption>A plain tweet.</figcaption>

![A retweet](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-2.png)<figcaption>A retweet</figcaption>

![A retweet with comment](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-3.png)<figcaption>A retweet with comment</figcaption>

![A liked tweet](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-4.png)<figcaption>A liked tweet</figcaption>

![A plain tweet with a picture](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-5.png)<figcaption>A plain tweet with a picture</figcaption>

![A plain tweet with multiple pictures.](https://blog.1a23.com/wp-content/uploads/sites/2/2020/03/image-6.png)<figcaption>A plain tweet with multiple pictures.</figcaption>

Last but not least, don’t forget to follow me on Twitter and Telegram 🙂

* [Follow me on Twitter](https://twitter.com/blueset)
* [Subscribe on Telegram](https://t.me/blueset_tweets)

The post [Sync Tweets to a Telegram Channel Using Account Activity API](https://blog.1a23.com/2020/03/21/sync-tweets-to-a-telegram-channel-using-account-activity-api/) appeared first on [1A23 Blog](https://blog.1a23.com).