Take data from a page and go to the database

Asked

Viewed 54 times

-1

I want to do sweepstakes via Whatsapp. To participate, the person would send a message with the name to the number I will announce and she receives the confirmation reply and the token number.

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

1 answer

1

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)

Only save to your Whatsapp framework in the modules folder and enable on __init__.py

  • Thank you! I’ll test and tell you

  • Friend, what name I save?

  • a name that doesn’t conflict, like sorteio.py

Browser other questions tagged

You are not signed in. Login or sign up in order to post.