S
S
StenMarsh13372020-07-01 14:29:12
Python
StenMarsh1337, 2020-07-01 14:29:12

The user sends a file to the bot, and the bot sends it to gmail?

Guys, please help with the code: 1. The user throws a file to the bot 2. The bot sends the file

import telebot
from telebot import types
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import requests

bot = telebot.TeleBot('v2hO6B0M')

@bot.message_handler(commands=['start'])
def first(message):
    keyboard = types.ReplyKeyboardMarkup(True, False)
    keyboard.add('Меню')
    send = bot.send_message(message.chat.id, "Добро пожаловать, {0.first_name}!\nЯ - <b>{1.first_name}</b>, бот созданный чтобы быть подопытным кроликом.".format(message.from_user, bot.get_me()),
        parse_mode='html', reply_markup=keyboard)
    bot.register_next_step_handler(send, second)


def second(message):
    if message.text == 'Меню':
        keyboard = types.ReplyKeyboardMarkup(True, False)
        keyboard.row('Отправить ', 'Рассчёт ')
        keyboard.add('Обратная связь', 'Помощь')
        send = bot.send_message(message.chat.id, 'Вибери что тебе нужно', reply_markup=keyboard)
        bot.register_next_step_handler(send, third)
    else:
        bot.send_message(message.chat.id, 'Твои данные отправлены, ожидай ответа')
        addr_from = "serhii"  # Адресат
        addr_to = "serhii"  # Получатель
        password = "nvrk"  # Пароль
        msg = MIMEMultipart()  # Создаем сообщение
        msg['From'] = addr_from  # Адресат
        msg['To'] = addr_to  # Получатель
        msg['Subject'] = 'Данные от ЭЦП'  # Тема сообщени
        sms = message.text
        body = sms
        msg.attach(MIMEText(body, 'plain'))  # Добавляем в сообщение текст
        server = smtplib.SMTP('smtp.gmail.com', 587)  # Создаем объект SMTP
        server.set_debuglevel(True)  # Включаем режим отладки - если отчет не нужен, строку можно закомментировать
        server.starttls()  # Начинаем шифрованный обмен по TLS
        server.login(addr_from, password)  # Получаем доступ
        server.send_message(msg)  # Отправляем сообщение
        server.quit()  # Выходим

def third(message):
    if message.text == 'Отправить':
        keyboard = types.ReplyKeyboardMarkup(True, False)
        keyboard.row('Отмена')
        send = bot.send_message(message.chat.id, 'Напиши мне данные в таком формате: ФИО|Пароль от ЭЦП', reply_markup=keyboard)
        bot.register_next_step_handler(send, second)
    elif message.text == 'Отмена':
        first(message)


if __name__ == '__main__':
    bot.infinity_polling()
to mail via SMTP
Type so5efc742b60f67641332575.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
Xander017, 2020-07-01
@StenMarsh1337

You need to add the step of downloading the file to the hosting.
The algorithm of actions IMHO should be the following:
1. The user throws a file to the bot.
2. The bot receives it and stores the file on the telegram server.
3. With getFile you get a link to the file, which is valid for 1 hour (after that you can get it again).
4. Download the file to your hosting or to the cloud.
5. Create a letter, attach a file and send.
6. After successfully sending the letter, you delete the file from the hosting.
Done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question