Git Product home page Git Product logo

tlg-chatbot-render's Introduction

OpenAI-integrated Telegram Chatbot

This is a project to create a Telegram chatbot that is integrated with OpenAI to enable natural language processing and intelligent responses in messaging.

Additionally, we will provide guidelines for deploying the bot with zero downtime on Render for free, an alternative to Heroku.

The following set of comprehensive instructions will guide you through creating the chatbot, which includes registering a new Telegram bot, configuring OpenAI API keys, integrating the two services, and deploying the application as a 24/7 service.

PREREQUISITES

To run this project, you will need the following:

  • A Github account (Obviously)
  • An OpenAI API Key
  • Telegram App ID and hash
  • A Telegram Bot token
  • A Render account
  • Python (version 3.8 or newer) - if running in local

INSTALLATION

  1. Clone this repository by typing this command in the terminal.
git clone https://github.com/mitumh3/tlg-chatbot-render.git
  1. Install the required packages by this command.
pip install -r requirements.txt

SETUP KEYS

Get OpenAI API Key

  1. Make sure you have an OpenAI account.
  2. Go to https://platform.openai.com/account/api-keys to create or get an API Key.

Note: In gpt-3.5-turbo model, $0.002 will be charged for 1k tokens. However, there are account plans that give you certain amount of granted credit (my case is a free trial of $18).

Get Telegram App ID and Hash

To get the API ID and Hash of your Telegram app (your bot), you need to follow the below steps:

  1. Please ensure that you have a Telegram account and are already logged in on your phone.
  2. Open your web browser and go to https://my.telegram.org/auth.
  3. Enter your phone number associated with your Telegram account and click on the Next button.
  4. An OTP will be sent to your Telegram app, enter the OTP in the given field.
  5. Now, you will be redirected to the Developer Tools page. Here, you can find the API ID and Hash after clicking "Create a new application" section and fill out the form.
  6. Note down the API ID and Hash somewhere securely.

Note: Please make sure you keep your API ID and Hash secret and do not share them with anyone else as they can be used to access your Telegram account, groups, and channels.

Get a Telegram Bot token

  1. Visit https://telegram.me/BotFather or find @BotFather on your Telegram to register a new bot.
  2. Send /newbot command to BotFather.
  3. Set a name for the bot and a username (ending with 'bot').
  4. When registration process is completed, BotFather will provide an HTTP API token for the bot.
  5. You should further configure the bot for group chat permission by: Bot Settings >> Group Privacy >> Turn Off

Declare your keys in environment

Run the bot in local will required creating an .env file with the following contents:

OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
API_ID="YOUR_TELEGRAM_APP_ID"
API_HASH="YOUR_TELEGRAM_APP_HASH"
BOTTOKEN=YOUR_BOT_TOKEN

Note:

  • Remember to replace YOUR_OPENAI_API_KEY, YOUR_BOT_TOKEN, YOUR_TELEGRAM_APP_ID, and YOUR_TELEGRAM_APP_HASH with the real keys that you've just got from the above.
  • All the keys should be put in quotes as a string, except YOUR_BOT_TOKEN since it is an integer.
  • For security, .env file is only suitable for local run. In case of deployment, your keys should be kept as SECRETS or ENVIRONMENT VARIABLES that can only be accessed by you.

RUN BOT

Run the command below in your terminal to initiate the bot.

uvicorn src.main:app --port=${PORT:-8080}

FEATURES

v1.0.x will include the following use:

  • Private chat: You can freely chat with the bot using the bot username (@your_bot_username) derived from the BotFather
  • Group chat: The bot can be invited into groupchat and user can interact with it through command /slave.
  • Bash: /bash {command} to run bash script.
  • Clear: /clear to clear all existing conversations.
  • Search: /search {keywords} to send search request to duckduckgo and openAI will summarize the search for you. Tip: you can update bot's knowledge with this, since search summary will be added to your current conversation.

Example:

  • In private: "Who is your creator?"
  • Group chat: "/slave Who's your daddy?"
  • "/bash ls -a"
  • "/clear"
  • "/search avatar 2023"

Deploy bot on Render

The bot can be deployed freely on https://render.com for 24/7 runtime as a web application.

SETUP RENDER

  1. Make sure you have a Github repo containing all the files.
  2. Get yourself a render.com account (Logging in with your github account should be the most convenience).
  3. Follow these clicks: Dashboard >> New >> Web Service
  4. Choose your Github repo.
  5. Complete the form with:
  • Name, Region
  • Runtime: Python
  • Build Command: pip install -r requirements.txt
  • Start Command: uvicorn src.main:app --port=${PORT:-8080} --log-config=log/logging.ini
  • Instance Type: Free
  • Click on Advanced and add the 4 keys as environment variables. Add another variable: PYTHON_VERSION with value of 3.10.2 to define the runtime version.
  • Health Check Path: /health
  • Auto-Deploy: No

RUN

  1. For the first time, click Create Web Service after filling out the form to start deploying the bot.
  2. Manual deployment can be performed in your bot web service found in Dashboard of render.

Note:

  • The 4 keys (OPENAI_API_KEY, BOTTOKEN, API_ID, API_HASH) and their values don't need quotes.
  • Logs can be found on https://{your_setup_name}.onrender.com/log and additional bash command can be executed on https://{your_setup_name}.onrender.com/terminal
  • Deployment will take about 15 minutes to complete, but some problems within the server can happen if we interact with the bots in this period (Usually, the bot will get duplicated responses. In case of private chats, without preceded command in message, the bot will "chat" to itself and create a messy looped conversation that will burn out your credits). Therefore, a total of 30m to 1 hour should be taken to avoid the above problem. Or you can chat in a group with preceded commands.

Possible Improvements

  • Add more functionalities to the bot.
  • Increase the accuracy of OpenAI responses.
  • Refine the conversation handling.
  • Fix problem that Render server will be reset after every 15 minutes.
  • Fix bot looped conversation in private chat at deployment start up.
  • To limit the number of tokens generated for each prompt, the tiktoken package is commonly used. However, this package can only be used on newer versions of Python, which can be inconvenient. As a solution, we recommend removing the use of tiktoken and exploring alternatives for limiting token usage.

Contributing

We are grateful for contributions of any magnitude. Also, a big thanks to DirtyG2120 for his exceptional and expert contribution.

tlg-chatbot-render's People

Contributors

mitumh3 avatar dirtyg2120 avatar

Stargazers

BlackHat avatar  avatar Dev Tanna avatar Mark Clift avatar Keima Senpai avatar Yusuf avatar Richard Elinam Nutsugah avatar  avatar Tuan Duc Tran avatar Nima Khajehpour avatar  avatar  avatar  avatar DragAdi avatar

Watchers

 avatar Dan Cruz avatar

tlg-chatbot-render's Issues

The automated release is failing 🚨

🚨 The automated release from the main branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you can benefit from your bug fixes and new features again.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can fix this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the main branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here are some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


The analyzeCommits plugin returned an invalid value. It must return a valid semver release type.

The analyzeCommits plugin must return a valid semver release type. The valid values are: patch, minor, major.

The analyzeCommits function of the @semantic-release/commit-analyzer returned prepatch instead.

We recommend to report the issue to the @semantic-release/commit-analyzer authors, providing the following informations:


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.