Aplikasi Sederhana Dengan Python send Whatsup messages

Mau paham tentang Aplikasi Sederhana Dengan Python send Whatsup messages, hanya disiniaja

Jun 16, 2024 - 00:36
Jun 18, 2024 - 04:16
 0  287

You can watch this lesson on YouTube to understand better.

YOUTUBE LINK = https://www.youtube.com/watch?v=AzY3aVBfMRk

Don't forget to help our channel to develope further so that, there is nothing that can't be done except https://disiniaja.id.

This tutorial is a continuation of the tutorial in another post

 Aplikasi sederhana dengan python save data to excel

If u have existing twilio account then login or u can create new account https://www.twilio.com/en-us

Create new account for new project whatsup

take your phone and open camera to scan qr

then send message: join left-addition by your whatsup like this picture

Reload your current browser then click Next Step

Copy source code to notepad

OK NOW YOU HAVE USER, TOKEN AND VIRTUAL PHONE NUMBER FROM TWILIO 

def send_wasms():

    account_sid = 'account_sid your twilio'

    auth_token = 'auth_token your twilio'

    client = Client(account_sid, auth_token)

 

    message = client.messages.create(

      from_='whatsapp:+1415TWILIO VIRTUAL PHONE NUMBER',

      body='Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://www.disiniaja.id/',

      to='whatsapp:+6285YOUR NUMBER'

    )

 

    print(message.sid)

OK NOW CREATE NEW THEN COPY THIS CODE OR YOU CAN DOWNLOAD FROM HERE

import mysql.connector

import PySimpleGUI as sg

import pandas as pd

import os

import json

import requests

import smtplib

import random

from twilio.rest import Client

 

# import the corresponding modules

from email import encoders

from email.mime.base import MIMEBase

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

 

mysqldb=mysql.connector.connect(host="localhost",user="root",password="",database="aplikasi")

 

#if mysqldb.is_connected():

    #print("Berhasil terhubung ke database")

 

mycursor=mysqldb.cursor()

sg.theme('DarkGreen4')

EXCEL_FILE = 'Pendaftaran.xlsx'

df = pd.read_excel(EXCEL_FILE)

layout=[

[sg.Text('Masukan Data Kamu: ')],

[sg.Text('Nama',size=(15,1)), sg.InputText(key='Nama')],

[sg.Text('No Telp',size=(15,1)), sg.InputText(key='Tlp')],

[sg.Text('Alamat',size=(15,1)), sg.Multiline(key='Alamat')],

[sg.Text('Tgl Lahir',size=(15,1)), sg.InputText(key='Tgl Lahir'),

                                    sg.CalendarButton('Kalender', target='Tgl Lahir', format=('%Y-%m-%d'))],

[sg.Text('Jenis Kelamin',size=(15,1)), sg.Combo(['pria','wanita'],key='Jekel')],

[sg.Text('Hobi',size=(15,1)), sg.Checkbox('Belajar',key='Belajar'),

                            sg.Checkbox('Menonton',key='Menonton'),

                             sg.Checkbox('Musik',key='Musik')],

[sg.Text('OTP',size=(15,1)), sg.Button('check OTP'),sg.InputText(key='OTP')],

[sg.Submit(), sg.Button('clear'), sg.Button('view data'), sg.Button('open excel'), sg.Exit()]

 

]

 

window=sg.Window('Form pendaftaran',layout)

 

def select():

    results = []

    mycursor.execute("select nama,tlp,alamat,tgl_lahir,jekel,hobi from pendaftaran order by id desc")

    for res in mycursor:

        results.append(list(res))

 

    headings=['Nama','Tlp','Alamat','Tgl Lahir', 'Jekel', 'Hobi']

 

    layout2=[

        [sg.Table(values=results,

        headings=headings,

        max_col_width=35,

        auto_size_columns=True,

        display_row_numbers=True,

        justification='right',

        num_rows=20,

        key='-Table-',

        row_height=35)]

    ]  

 

    window=sg.Window("List Data", layout2)

    event, values = window.read()

 

def send_gdrive():

    headers={

        "Authorization":"Bearer ya29.a0AXooCgs601toKsQpCqDCGLz6aBphWS0a3zUT4TBJPuzQE6HPlzPfDpWxPf7EDXZZNvuU8rV-OJ5v614K8dFJ9qZg0aMGwAoMzmdSfR9aOyZDHzBEbQ3kcF-e7BOIBVGvei42B60C4yl6WCg1Ns6nKM-mlzld3EwH4EUDaCgYKAUkSARASFQHGX2MifeqhlbHtiQ_eIee4cyEgTg0171"

    }

    name={

        "name":"Pendaftaran.xlsx",

    }

    files={

        'data':('metadata',json.dumps(name),'application/json;charset=UTF-8'),

        'file':('mimeType',open("./Pendaftaran.xlsx","rb"))

    }

    r=requests.post(

        "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",

        headers=headers,

        files=files

    )

    print(r.text)

 

def send_email(nama,tlp,alamat,tgl_lahir,jekel,hobi):

    port = 2525

    smtp_server = "sandbox.smtp.mailtrap.io"

    login = "your login" # paste your login generated by Mailtrap

    password = "your password" # paste your password generated by Mailtrap

 

    subject = "Pendaftaran"

    sender_email = "sender_email@gmail.com"

    receiver_email = "receiver_email@gmail.com"

 

    message = MIMEMultipart()

    message["From"] = sender_email

    message["To"] = receiver_email

    message["Subject"] = subject

 

    # Add body to email

    body = "Terimakasih Anda Telah Mengisi Form Pendaftaran Sbb :\n"

    detail=body+'nama:'+nama+'\nNo Tlp:'+tlp+'\nAlamat:'+alamat+'\nTanggal Lahir'+tgl_lahir+'\nJenis Kelamin'+jekel+'\nHoby'+hobi

    message.attach(MIMEText(detail, "plain"))

 

    with open(EXCEL_FILE, "rb") as attachment:

        # The content type "application/octet-stream" means that a MIME attachment is a binary file

        part = MIMEBase("application", "octet-stream")

        part.set_payload(attachment.read())

 

    # Encode to base64

    encoders.encode_base64(part)

   

    part.add_header(

    "Content-Disposition",

    f"attachment; filename= {EXCEL_FILE}",

    )

 

    # Add attachment to your message and convert it to string

    message.attach(part)

    text = message.as_string()

 

    # send your email

    with smtplib.SMTP("sandbox.smtp.mailtrap.io", 2525) as server:

        server.starttls()

        server.login(login, password)

        server.sendmail(

            sender_email, receiver_email, text

        )

    print('Sent')

 

def send_sms():

    account_sid = 'account_sid your twilio'

    auth_token = 'auth_token your twilio'

    client = Client(account_sid, auth_token)

 

    message = client.messages.create(

      from_='+15078TWILIO VIRTUAL NUMBER',

      body='Ada Pendaftaran Baru Nih..',

      to='+6285YOUR NUMBER'

    )

 

    print(message.sid)

 

def send_wasms():

    account_sid = 'account_sid your twilio'

    auth_token = 'auth_token your twilio'

    client = Client(account_sid, auth_token)

 

    message = client.messages.create(

      from_='whatsapp:+1415TWILIO VIRTUAL PHONE NUMBER',

      body='Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://www.disiniaja.id/',

      to='whatsapp:+6285YOUR NUMBER'

    )

 

    print(message.sid)

 

def fixotp(tlp):

    codeotp = random.randint(1000,9999)

    detailotp='Masukan OTP berikut pada Form Pendaftaraan\n'

    bdy=detailotp+str(codeotp)

    angkadepan=tlp[1:13]

    enamdua='+62'

    no_wa='whatsapp:'+enamdua+angkadepan

    print (no_wa)

    account_sid = 'account_sid your twilio'

    auth_token = 'auth_token your twilio'

    client = Client(account_sid, auth_token)

 

    message = client.messages.create(

      from_='whatsapp:+14155238886',

      body=bdy,

      to=no_wa

    )

 

    print(message.sid)

   

def clear_input():

    for key in values:

        window[key]('')

        #window['Nama'].update('')

    window.Element('Nama').SetFocus(force = True)

    return None

 

while True :

    event, values = window.read()

    if event == sg.WIN_CLOSED or event == 'Exit':

        break

    if event == 'clear':

        clear_input()

    if event == 'view data':

        select() 

    if event == 'open excel':

        os.startfile(EXCEL_FILE)

    if event == 'check OTP':

       

        tlp=values["Tlp"]

        if tlp == '':

            sg.popup('ISI DENGAN BENAR SAMPLE:081222')

            window.Element('Tlp').SetFocus(force = True)

        else:

           

            fixotp(tlp)

    if event == 'Submit':

        nama=values["Nama"]

        tlp=values["Tlp"]

        alamat=values["Alamat"]

        tgl_lahir=values["Tgl Lahir"]

        jekel=values["Jekel"]

        belajar=values["Belajar"]

        menonton=values["Menonton"]

        musik=values["Musik"]

 

        if belajar == True:

            hobi="Belajar"

        if menonton == True:

            hobi="Menonton"

        if musik == True:

            hobi="Musik"

        if nama == '':

            sg.popup('MISSION FAILED')

            window.Element('Nama').SetFocus(force = True)

        else:

            sql="insert into pendaftaran(nama,tlp,alamat,tgl_lahir,jekel,hobi) values(%s,%s,%s,%s,%s,%s)"

            val=(nama,tlp,alamat,tgl_lahir,jekel,hobi)

            mycursor.execute(sql,val)

            mysqldb.commit()

            df =df._append(values, ignore_index=True)

            df.to_excel(EXCEL_FILE, index=False)

            sg.popup('Data Berhasil Di Simpan')

            send_gdrive()

            send_email(nama,tlp,alamat,tgl_lahir,jekel,hobi)

            send_sms()

            #otp(tlp)

            #send_wasms()

            clear_input()

           

window.close()      

 

SAVE AND RUN PROGRAM

There is no sense of pride in what we do, it's just to share experiences when our financial condition is not in a good condition.

If we try to count the blessings that Allah has bestowed on us, we will certainly not be able to count them.
May we all be given health and long life, so we can share for science.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow