GithubHelp home page GithubHelp logo

meneer-code / connect-telegram-bot-to-google-sheets-chatgpt-openai Goto Github PK

View Code? Open in Web Editor NEW
179.0 5.0 48.0 449 KB

Connect Telegram Bot to Google Sheets via Google Apps Scripts

License: GNU General Public License v3.0

JavaScript 100.00%

connect-telegram-bot-to-google-sheets-chatgpt-openai's Introduction

Connect Telegram Bot to Google Sheets via Google Apps Scripts. Includes integration with OpenAI for a ChatGPT Telegram bot

  • See (1) for a simple Telegram to Google Sheets integration
  • See (2) for an integration from Telegram > Google Sheets > OpenAI > Google Sheets > Telegram integration

1. Integrate Telegram Bot with Google Sheets

sheets-simple.gs

Connect Telegram Bot to Google Sheets

This video explains all steps in detail: https://www.youtube.com/watch?v=24EyItKfm50&list=PLGGHwNnXfci86dfqIVLc5l391SPk-RX1F

Step 1: Setting up the Telegram Bot

Step 2: Setting up Google Apps Script

Create a new Google Sheet in Google Drive. Go to script editor (Tools > Script Editor) and paste the code into the newly created code.gs file. Once done, deply the script as a web application. You will get a URL to use in the code.gs file.

  • Run getMe() and setWebhook() to initialise the integration.

Step 3: Add your Spreadsheet ID to the Script

Find your Google Sheet ID in the URL: https://docs.google.com/spreadsheets/d/{ID_HERE}/edit and past it into the code.gs file.

Step 4: Add your Telegram ID to the Script for error catching

After you seccusfully communicated with the bot, your chat ID should be in the recording spreadsheet entries. Copy and past this into the adminID variable in the script

2. Create OpenAI ChatGPT like bot in Telegram (openai.gs)

openai.gs

A simple Google Apps Script that provides an output to a user input in Telegram, and saving the question + answer + error (if applicable) to a Google Sheet. Please note that this script can not (yet) 'remember' the conversation or take feedback to previous questions. It will evaluate a single inout with a single output. This script will serve as a starting point for those who want to add extra functionality, and I welcome you to add features with pull requests.

The script starts by defining a list of allowed user IDs that are authorized to chat with the bot, as well as some constants such as the bot token, OpenAI API key, and Spreadsheet ID.

The script uses the completions API enpoint with the text-davinci-003 set to max_tokens = 100.

Below is an example of the Telegram Chat and response: Script Properties

Below is an example of the questions and answers saved to Google Sheets: Script Properties

Script Inputs:

  • ALLOWED_USER_IDS: A list of user IDs that are allowed to chat with the bot
  • BOT_TOKEN: Script Property with key bot_token defined in Settings > Script Properties. Get this from @BotFather in Telegram
  • OPENAI_API_KEY: Script Property with key openai_api_key defined in Settings > Script Properties. Get this from https://beta.openai.com/account/api-keys
  • SPREADSHEET_ID: To get this, go to your sheet URL and grab the id from here: https://docs.google.com/spreadsheets/d/{ID_HERE}/edit
  • WEBHOOK_URL: After deploying the script as web app, copy the URL and paste it here

The script then includes a number of functions that handle different tasks:

  • getMe: Sends a request to the Telegram API to get information about the bot.
  • setWebhook: Sends a request to the Telegram API to set up a webhook for the bot.
  • doPost: Handles webhook requests from Telegram. This function is called whenever the bot receives a message from a user. It parses the request body, gets the message from the request, and writes the question to the 'question' column in a Google Sheets document. It then sends the question to the OpenAI API using the sendToOpenAI function, writes the answer to the 'answer' column in the Google Sheets document, and sends the answer back to the Telegram chat using the sendToTelegram function.

The script also includes the following functions:

  • sendToOpenAI: Sends a request to the OpenAI API with the specified text and returns the response.
  • sendToTelegram: Sends a message to the specified Telegram chat with the specified text.
  • writeToSheet: Writes the specified text to the specified column in the Google Sheets document.

๐Ÿšจ Please Note ๐Ÿšจ

I'm not entirely sure if it's the best idea to define sensitive data in the Script Properties section of Google Apps Script. Please be aware of this. Script Properties

connect-telegram-bot-to-google-sheets-chatgpt-openai's People

Contributors

meneer-code avatar sudo-eugene avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

connect-telegram-bot-to-google-sheets-chatgpt-openai's Issues

First message and id limit for bot

Hi, thanks for codes!
Is it possible make first custom message after start command?
And how make bot limited only for specific telegram userid or username?

Thanks in advance

Feature req: Please integrate apipie.ai

Users want access to as much AI as they can get, they dont want to manage 50 accounts, they want the fastest AI they want the cheapest AI, and you can provide all of that for them with this update.

in addition to or in place of integrating with any aggregators - Please integrate APIpie so devs can access them all from one place/subscription and plus it also provides:

-The most affordable, reliable and fastest AI available
-One API to access ~500 Models and growing
-Language, embedding, voice, image, vision and more
-Global AI load balancing, route queries based on price or latency
-Redundancy for major models providing the greatest up time possible
-Global reporting of AI availability, pricing and performance

Its the same API format as openai, just change the domain name and your API key and enjoy a plethora of models without changing any of your code other than how you handle the models list.

This is a win win for everyone, any new AI's from any providers will be automatically integrated into your stack with this one integration. Not to mention all the other advantages.

New features

i wish that you could do more function than represented in the project bc it's so useful
unfortunately you don't respon to the youtube comments, anyway for me and what i really need function i will try to explane by an example

the exp :
user : i want discount code for shopping sites
bot : cool, choose which site you want ?
form the keyboard that appear form telegram
(Ali express) (Amazon)
user : Amazon
bot : here is the code 156456
user : back
bot : choose which site you want ?
user : back
bot : opens the man menu
etc....

do you got the idea ? i want something that have an consecutive answers, not just when i say x the bot respon with y and that's it

thanks for you effort and time, i wish that you hear me and help me cuse i have zero knowledge in coding and made it so far with alot of wasted time without a real result****

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.