Take data from a page and move to the database

-2

I want to make sweepstakes via WhatsApp. To participate, the person would send a message with the name to the number that I will announce and it will receive the confirmation response and the token number.

I wanted the bot to take the phone number and name, save it to a MySQL database, and return the answer to it. However, the token number would be the id of the row in the table where its data was saved in MySQL. How could this be done?

    
asked by anonymous 09.11.2018 / 15:45

1 answer

0
from app.mac import mac, signals
from sqlalchemy import create_engine, Column, Integer, Unicode
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('mysql://usuario:senha@servidor/banco')
Base = declarative_base(bind=engine)
Session = sessionmaker(bind=engine)

class Cupom(Base):
    __tablename__ = 'cupons'

    id = Column(Integer(), primary_key=True)
    nome = Column(Unicode(200))
    numero = Column(Unicode(100))

Base.metadata.create_all()

@signals.message_received.connect
def handle(message):
    cupom = Cupom(nome=message.text, numero=message.who)
    s = Session()
    s.add(cupom)
    s.commit()
    mac.send_message("Numero do seu cupom: {}".format(cupom.id),
        message.conversation)

Just save in your whatsapp framework, in the modules folder, and enable in __init__.py

    
09.11.2018 / 16:16