N
N
neuro4live2021-03-07 18:23:56
Python
neuro4live, 2021-03-07 18:23:56

How to transfer information from a telegram bot to google tables?

There is a telegram bot that, in response to a text message, forwards it in response. If you send a video file, then the bot sends a file_id in response. There is a script for editing a Google spreadsheet. Using the subprocess module in the google spreadsheet script, it turns out to launch the telegram bot. I need text messages sent to the bot to be entered in one column in Google tables, and file_id (sent videos to the bot) in the other column. Please help make this happen.

Here is the telegram bot code:

import telebot
bot = telebot.TeleBot('токен бота, думаю, вам не нужен)))')
 
@bot.message_handler(content_types=['text'])
def repeat_all_message(message):
  print(message.text)
  bot.send_message(message.chat.id,message.text)
 
@bot.message_handler(content_types=['video'])
def command_handle_video(message):
  bot.send_message(message.chat.id, message.video.file_id)
  print(message.video.file_id)
 
if __name__ == '__main__':
  bot.polling(none_stop=True)


Here is the script for editing google spreadsheet:
# Подключаем библиотеки
import httplib2
import apiclient.discovery
from oauth2client.service_account import ServiceAccountCredentials
import subprocess
 
 
CREDENTIALS_FILE = 'cybernetic-apex-306610-trtr21fdf.json'  # Имя файла с закрытым ключом, вы должны подставить свое
 
# Читаем ключи из файла
credentials = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, ['https://www.googleapis.com/auth/spreadsheets', 'https://www.googleapis.com/auth/drive'])
 
httpAuth = credentials.authorize(httplib2.Http()) # Авторизуемся в системе
service = apiclient.discovery.build('sheets', 'v4', http = httpAuth) # Выбираем работу с таблицами и 4 версию API
 
 
results = service.spreadsheets().values().batchUpdate(spreadsheetId = '1FX11pgtcvm6OPgmxTEao-25LOW22cbokVJ_pMXZYHvw', body = {
    "valueInputOption": "USER_ENTERED", # Данные воспринимаются, как вводимые пользователем (считается значение формул)
    "data": [
        {"range": "Лист номер один!A2:B10",
         "majorDimension": "ROWS",     # Сначала заполнять строки, затем столбцы
         "values": [
                    ["Text message", "File_id"], # Заполняем первую строку
                    ['result = p.communicate()[0]', 'Id']  # Заполняем вторую строку
                   ]}
    ]
}).execute()
 
p=subprocess.Popen(['/usr/bin/python3', '/home/roman/PycharmProjects/uznaemfileID/file_id.py', 'stdout=subprocess.PIPE', 'stderr=subprocess.STDOUT'])
result = p.communicate()[0]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roktober, 2021-03-07
@Roktober

What a coincidence, yesterday I wrote such a thing
, I advise you to use gspread
https://gitlab.com/roktober/money_observer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question