Skip to content
stunnelPublic

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

47 Commits

Folders and files

Repository files navigation

Twigram

This projects aims to make a Telegram bot that forwards Twitter to Telegram

How to run

Credentials

Telegram bot token

You MUST provide a valid Telegram Bot TOKEN to run the bot.

Twitter credentials

The Twitter username, email and password are optional.
NOTE: Credentials logins may be subject to risk control restrictions.
Or you can provide a Twitter cookie instead.
Cookie format: {"auth_token": "xxx", "ct0": "yyy"}

If you don't provide any of these, the bot will use a anonymous session.
But it may be subject to rate limit.

Webhook If you set WEB_URL_ENABLE to True, and provide WEB_URL and PORT, the bot will run a webhook server for Telegram server to send updates.
Otherwise, it will run a polling bot.

As a webhook server, you must provide a https web with valid certificate, and can be accessed by Telegram server.
You can run in http mode and use a reverse proxy.
Or you can provide valid certificate and key, the bot will run in https.

IMPORTANT: Ports currently supported for webhooks: 443, 80, 88, 8443. ref: https://core.telegram.org/bots/api#setwebhook

Nginx reverse proxy sample

If you enable webhook mode by setting WEB_URL_ENABLE and run the bot in plain HTTP (no CERT_FILE/KEY_FILE), you need a reverse proxy in front of it to terminate TLS, since Telegram requires HTTPS. WEB_URL must match the public HTTPS address the proxy serves (e.g. https://twigram.example.com), and the proxy forwards traffic to the bot's local PORT (default 58081).

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name twigram.example.com;

    ssl_certificate     /etc/letsencrypt/live/twigram.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/twigram.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:58081;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Replace twigram.example.com and the certificate paths with your own, and 127.0.0.1:58081 with the host/PORT the bot is actually listening on (e.g. the container's published port, if not using --network host).

Docker

  1. Clone this repo
  2. Build image:
    docker build -t twigram:latest .
    
  3. Run container:
    docker run -d --network host \
        --name twigram --restart=on-failure \
        -e TOKEN=<telegram_token> \
        -e INTERVAL=<polling_interval_in_seconds> \
        -e TWITTER_USERNAME=<twitter_username> \
        -e TWITTER_EMAIL=<twitter_email> \
        -e TWITTER_PASSWORD=<twitter_password> \
        -e TWITTER_COOKIE=<twitter_token> \
        -e WEB_URL_ENABLE=<True or False> \
        -e WEB_URL=<https_web_url> \
        -e CERT_FILE=<cert file> \
        -e KEY_FILE=<key file> \
        -e PORT=8080 \
        -e PROCESS_COUNT=1 \
        -e QUOTE=False \
        -e DEBUG=False \
        twigram:latest
    

Manually

  1. Clone this repo
  2. Create venv:
    python3 -m venv venv
    
  3. venv/bin/pip install -r requirements.txt
  4. Run venv/bin/python main.py
    Or using systemd
    [Unit]
    Description=Twigram - Telegram bot that forwards Twitter to Telegram
    After=syslog.target network-online.target nss-lookup.target
    
    [Service]
    Type=forking
    Environment=TOKEN=<telegram_token>
    Environment=INTERVAL=<polling_interval_in_seconds>
    Environment=TWITTER_USERNAME=<twitter_username>
    Environment=TWITTER_EMAIL=<twitter_email>
    Environment=TWITTER_PASSWORD=<twitter_password>
    Environment=TWITTER_COOKIE=<twitter_token>
    Environment=WEB_URL_ENABLE=<True or False>
    Environment=WEB_URL=<https_web_url>
    Environment=CERT_FILE=<cert file>
    Environment=KEY_FILE=<key file>
    Environment=PORT=8080
    Environment=PROCESS_COUNT=1
    Environment=DEBUG=False
    Environment=QUOTE=False
    ExecStart=/bin/bash run.sh
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

Deploy to fly.io

  1. Clone this repo
  2. Install Docker, flyctl and login to fly.io
  3. Auth registry.fly.io for docker
    flyctl auth docker
    
  4. Create a fly app
    app_name="your_app_name"
    flyctl apps create "${app_name}"
    
  5. Build image locally, and push to fly
     docker build -t "${app_name}":latest .
     docker tag "${app_name}":latest registry.fly.io/"${app_name}":latest
     docker push registry.fly.io/"${app_name}":latest;
    
  6. Deploy
    flyctl deploy --app "${app_name}" \
      --env "TOKEN=1423456789:AAAA" \
      --env "WEB_URL_ENABLE=<True or False>" \
      --env "WEB_URL=https://${app_name}.fly.dev" \
      --env "CERT_FILE=<cert file>" \
      --env "KEY_FILE=<key file>" \
      --env "PORT=8080" \
      --env "INTERVAL=30" \
      --env "TWITTER_COOKIE=" \
      --env "INTERVAL=<polling_interval_in_seconds>" \
      --env "TWITTER_USERNAME=<twitter_username>" \
      --env "TWITTER_EMAIL=<twitter_email>" \
      --env "TWITTER_PASSWORD=<twitter_password>" \
      --env "TWITTER_COOKIE=<twitter_token>" \
      --env "PROCESS_COUNT=1" \
      --env "QUOTE=False" \
      --env "DEBUG=false" \
      --image registry.fly.io/"${app_name}":latest
    

References

This is based on former work:

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages