chat bot for telegram
Budget: €8 – €30 EUR
implement the following python code to make the chat bot for telegram working. Also, apply this code in a online IDE environment, giving clear instruction on how to change the code if needed, for a begginer person.
python code:
import telebot
import time
import datetime
import requests
import gspread
def calculate_discount(value, discount_type):
"""
Calculates a 5%, 10%, or 7% discount on a value in euros.
Args:
value: The value in euros.
discount_type: The discount type (5, 10, or 7).
Returns:
The discount value.
"""
if discount_type == 5:
return value * 0.05
elif discount_type == 10:
return value * 0.1
else:
return value * 0.07
def find_euros_value(message):
"""
Finds the euros value in the received message.
Args:
mensagem: The received message.
Returns:
The euros value.
"""
text = message.text.replace("promo", "")
text = text.lower()
pos = text.find("€")
if pos != -1:
valor = text[:pos]
else:
valor = text.split("euros")[0]
return valor
def calculate_final_price(value, discount):
"""
Calculates the final price of the product after the discount.
Args:
valor: The original product value.
discount: The discount value.
Returns:
The final product price.
"""
return value - discount
def calculate_loyalty_points(value):
"""
Calculates the loyalty points based on the purchase value.
Args:
valor: The purchase value.
Returns:
The number of loyalty points.
"""
return int(value)
def send_discount_message(message):
"""
Sends a message to the customer with the discount and asks for the product photo.
Args:
mensagem: The received message.
"""
valor = find_euros_value(message.text)
desconto = calculate_discount(valor, tipo_desconto)
preco_final = calculate_final_price(valor, desconto)
pontos_da_compra = calculate_loyalty_points(valor)
mensagem_desconto = f"""
Valor do produto: {valor}€
Valor do desconto: {desconto}€
Preço final: {preco_final}€
Pontos da compra: {pontos_da_compra}
Obrigado pela preferência.
"""
bot.send_message(message.contact, mensagem_desconto)
def update_google_sheets_data(sheet, telefone, desconto, preco_final, pontos_da_compra):
"""
Updates the data of a customer in the Google Sheets sheet.
Args:
sheet: The Google Sheets sheet.
telefone: The customer's phone number.
desconto: The discount value.
preco_final: The final product price.
pontos_da_compra: The number of points accumulated in the purchase.
"""
# Adds the customer data to the Google Sheets sheet
sheet.append_row([telefone, desconto, preco_final, pontos_da_compra])
# Updates the points accumulated column
sheet.update_cell(telefone, 4, sheet.cell(telefone, 4).value + pontos_da_compra)
python code:
import telebot
import time
import datetime
import requests
import gspread
def calculate_discount(value, discount_type):
"""
Calculates a 5%, 10%, or 7% discount on a value in euros.
Args:
value: The value in euros.
discount_type: The discount type (5, 10, or 7).
Returns:
The discount value.
"""
if discount_type == 5:
return value * 0.05
elif discount_type == 10:
return value * 0.1
else:
return value * 0.07
def find_euros_value(message):
"""
Finds the euros value in the received message.
Args:
mensagem: The received message.
Returns:
The euros value.
"""
text = message.text.replace("promo", "")
text = text.lower()
pos = text.find("€")
if pos != -1:
valor = text[:pos]
else:
valor = text.split("euros")[0]
return valor
def calculate_final_price(value, discount):
"""
Calculates the final price of the product after the discount.
Args:
valor: The original product value.
discount: The discount value.
Returns:
The final product price.
"""
return value - discount
def calculate_loyalty_points(value):
"""
Calculates the loyalty points based on the purchase value.
Args:
valor: The purchase value.
Returns:
The number of loyalty points.
"""
return int(value)
def send_discount_message(message):
"""
Sends a message to the customer with the discount and asks for the product photo.
Args:
mensagem: The received message.
"""
valor = find_euros_value(message.text)
desconto = calculate_discount(valor, tipo_desconto)
preco_final = calculate_final_price(valor, desconto)
pontos_da_compra = calculate_loyalty_points(valor)
mensagem_desconto = f"""
Valor do produto: {valor}€
Valor do desconto: {desconto}€
Preço final: {preco_final}€
Pontos da compra: {pontos_da_compra}
Obrigado pela preferência.
"""
bot.send_message(message.contact, mensagem_desconto)
def update_google_sheets_data(sheet, telefone, desconto, preco_final, pontos_da_compra):
"""
Updates the data of a customer in the Google Sheets sheet.
Args:
sheet: The Google Sheets sheet.
telefone: The customer's phone number.
desconto: The discount value.
preco_final: The final product price.
pontos_da_compra: The number of points accumulated in the purchase.
"""
# Adds the customer data to the Google Sheets sheet
sheet.append_row([telefone, desconto, preco_final, pontos_da_compra])
# Updates the points accumulated column
sheet.update_cell(telefone, 4, sheet.cell(telefone, 4).value + pontos_da_compra)