{"id":18712373,"url":"https://github.com/codenameakshay/telegram-twitter-bot","last_synced_at":"2025-04-12T11:53:44.684Z","repository":{"id":43231886,"uuid":"280944379","full_name":"codenameakshay/telegram-twitter-bot","owner":"codenameakshay","description":"This script allows one to run a Telegram bot that can tweet stuff for you, like tweets with images, with features like watermarking images and the option to download them in high-quality from the given link.","archived":false,"fork":false,"pushed_at":"2022-12-08T11:11:48.000Z","size":6224,"stargazers_count":8,"open_issues_count":7,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T06:34:38.724Z","etag":null,"topics":["python3","telebot","telegram-bot","tweepy","twitter","twitter-api","twitter-bot"],"latest_commit_sha":null,"homepage":"https://twitter.com/PrismWallpapers","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codenameakshay.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-19T20:24:23.000Z","updated_at":"2025-01-03T17:22:21.000Z","dependencies_parsed_at":"2023-01-25T12:01:09.746Z","dependency_job_id":null,"html_url":"https://github.com/codenameakshay/telegram-twitter-bot","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codenameakshay%2Ftelegram-twitter-bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codenameakshay%2Ftelegram-twitter-bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codenameakshay%2Ftelegram-twitter-bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codenameakshay%2Ftelegram-twitter-bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codenameakshay","download_url":"https://codeload.github.com/codenameakshay/telegram-twitter-bot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248565044,"owners_count":21125414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python3","telebot","telegram-bot","tweepy","twitter","twitter-api","twitter-bot"],"created_at":"2024-11-07T12:42:36.043Z","updated_at":"2025-04-12T11:53:44.663Z","avatar_url":"https://github.com/codenameakshay.png","language":"Python","readme":"# telegram-twitter-bot\nThis script allows one to run a Telegram bot that can tweet stuff for you, like tweets with images, with features like watermarking images and the option to download them in high-quality from the given link.\n\nAutomating a simple Telegram bot can let you serve different types of data to various people including channels and group of which you are a part. I use my bot to serve my [channel](https://t.me/codenametech) members Tech related news directly, plus I also use this bot to tweet stuff from the Official Prism Wallpapers Twitter account. Today I am going to share on how you can make such a simple EchoBot and deploy it on Heroku, so that it works 24x7. So without any further ado, let's get started.\n\n# Step-1 Talk to the BotFather\n\n![](/images/echobot/1.png)\n\nGo to the BotFather (@BotFather) on Telegram and then type `/start`.\n![](/images/echobot/2.png)\nAfter that just type `/newbot` to create your first bot. Then give it a name and other information like its address. The BotFather will also give you the `API Token` for your bot, save it, we need it in a couple of minutes.\n![](/images/echobot/3.png)\n\n# Step-2 Talk to your bot\n\nNow go to the address of your bot and type `/start` and send it. Now type anything that you like, it is just for making sure that your unique user id gets stored with the bot.\n![](/images/echobot/4.png)\n\n# Step-3 Finding the user id\n\nNow got to this link `https://api.telegram.org/bot[YOUR_API_TOKEN]/getUpdates` just replace [YOUR_API_TOKEN] by your `API Token`.\nHere you will see the message that you sent to your bot, now simply just copy the user ID and store it for later use.\n\n# Step-4 Getting Started with Python\n\nNow simply create a directory where you will be going to store your code and open `VSCode` or any other editor in it.\nThen type `python -m venv botenv/` to create a virtual environment for your code.\nThen you need to activate it by simply typing `source botenv/Scripts/activate` in you command shell.\nNow we need the following modules for our project so get ahead and download them.\n\n```\npip install flask\npip install python-telegram-bot\npip install requests\npip install bs4\npip install telegram\n```\nNow that you have installed the modules, simply create your project directory tree like this,\n```\n.\n├── app.py\n├── telebot\n│   ├── credentials.py\n│   |   .\n│   |   you can build your engine here\n│   |   .\n│   └── __init__.py\n└── botenv\n```\n\n# Step-5 Creating the `credentials.py` file\n\nSimply write these lines of code in `credentials.py`,\n```\nbot_token = \"here goes your access token from BotFather\"\nbot_user_name = \"the username you entered\"\nURL = \"the heroku app link that we will create later\"\n```\n\n# Step-6 Creating the `app.py` file\n\nNow go to your `app.py` file and write this in it,\n\n```\nfrom flask import Flask, request\nimport telegram\nfrom telebot.credentials import bot_token, bot_user_name,URL\n```\n\n```\nglobal bot\nglobal TOKEN\nTOKEN = bot_token\nbot = telegram.Bot(token=TOKEN)\n```\n\nNow to start our Flask app, write this also in `app.py`,\n```\napp = Flask(__name__)\n```\n\n# Step-7 Defining functions\n\nNow we will be creating the functions which get called when anybody talks to the bot.\n```\n@app.route('/{}'.format(TOKEN), methods=['POST'])\ndef respond():\n   update = telegram.Update.de_json(request.get_json(force=True), bot)\n   chat_id = update.message.chat.id\n   msg_id = update.message.message_id\n   text = update.message.text.encode('utf-8').decode()\n   print(\"got text message :\", text)\n   if text == \"/start\":\n       bot_welcome = \"\"\"\n       Welcome to CNA bot, this bot print the same message that you type. Try it out by typing and sending anything. Developed by @CodeName_Akshay\"\n       \"\"\"\n       bot.sendMessage(chat_id=chat_id, text=bot_welcome, reply_to_message_id=msg_id)\n   else:\n       bot.sendMessage(chat_id=chat_id, text=text, reply_to_message_id=msg_id)\n   return 'ok'\n```\nThis function below basically lets you start the bot. We will see later how to call this function.\n```\n@app.route('/setwebhook', methods=['GET', 'POST'])\ndef set_webhook():\n    # we use the bot object to link the bot to our app which live\n    # in the link provided by URL\n    s = bot.setWebhook('{URL}{HOOK}'.format(URL=URL, HOOK=TOKEN))\n    # something to let us know things work\n    if s:\n        return \"webhook setup ok\"\n    else:\n        return \"webhook setup failed\"\n```\nTo create a homepage for your bot on Heroku, we will define a function that just prints a `period` on the mainpage.\n```\n@app.route('/')\ndef index():\n    return '.'\nif __name__ == '__main__':\n    # note the threaded arg which allow\n    # your app to have more than one thread\n    app.run(threaded=True)\n```\n\nNow your basic bot is complete.\n\n# Step-8 Initializing launch of the bot on Heroku\n\nFirst we need a way to make sure that `Heroku` knows the requirements we need, so run this command.\n```\npip freeze \u003e requirements.txt\n```\nNow we need the `Procfile` which tells Heroku where our app starts, so create a `Procfile` file and add the following command,\n```\nweb: gunicorn app:app\n```\nNow save everything and then go to your `Heroku Dashboard` and create a new app. Once you do, it will direct you to the Deploy page. Then, open the Settings tab in a new window and copy the domain of the app which will be something like `https://appname.herokuapp.com/`and paste it in the `URL` variable inside `credentials.py`.\n\n# Step-9 Launching the bot on Heroku\n\nFirst login into your `Heroku` account by the following command,\n```\nheroku login\n```\nThen initialize a `Git` repository in your directory by the following commands,\n```\ngit init\nheroku git:remote -a [heroku-project-name]\n```\nReplace `[heroku-project-name]` by your project name.\n\nNow deploy the app by the following commands,\n```\ngit add .\ngit commit -m \"first commit\"\ngit push heroku master\n```\nNow after it completes pushing, simply navigate to `https://yourappname.herokuapp.com/setwebhook` to start the bot.\nIf you see `webhook setup ok` that means you are ready to chat with your bot.\n\n# Step-10 Chatting with the bot\n\nNow you can go to your bot and simply type in a message and wait for it's reply. It will reply the same text. This is called an Echo bot. You can now explore the possibilities, just by changing the definition of the `respond()` function you can make your bot do alot of cool stuff.\n\n![](/images/echobot/demo.gif)\nWith this basic bot, now you can extend the functionality to do almost everything you like. By the way, for tweeting you also need Twitter Developer Account and API keys.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodenameakshay%2Ftelegram-twitter-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodenameakshay%2Ftelegram-twitter-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodenameakshay%2Ftelegram-twitter-bot/lists"}