danielcardeenas / whatsapp-framework

⚗️Whatsapp python api
1.17k stars 360 forks source link

synchronize contacts automatically config.py #129

Closed jhonarodriguez closed 6 years ago

jhonarodriguez commented 6 years ago

Hello,

I am doing the synchronization of contacts in the config.py file but my question is if the number should go with the country indicator along with the "+" symbol? or would it just be the phone number?

Option 1: +573138075532

option 2: 573138075532

option 3: 3138075532

I appreciate your help.

LabtronicsDesign commented 6 years ago

I'm not sure but can you share how you are doing the synchronization, is it automatic? Once stored, how/where is the config constants used?

jhonarodriguez commented 6 years ago

hello, so far the process has worked well, I'm saving the number as 5731360 .... and if you do it automatically, make a loop every so often where you bring back contacts from a database and send the contacts to the function self.stack.setProp (MacLayer.PROP_CONTACTS, list (contacts))

I do not know if the process will be correct.

LabtronicsDesign commented 6 years ago

How exactly to make a loop? Do you make a seperate thread that runs in the background?

Also where do you call self.stack.setProp (MacLayer.PROP_CONTACTS, list (contacts))? In another module?

Please if you could share some code snippets that'll really help. I'll be willing to make a PR if it works well as well.

jhonarodriguez commented 6 years ago

In the run.py file,

import the library threading and requests and in the class MacStack creates a new method called for example sincContacts

def sincContacts(self):
    r = requests
    urlglobal = 'urlget'
    pet = r.post(urlglobal+'getContactsBot')
    contactsJSON = pet.json()
    contacts = {
    }
    for contact in contactsJSON:
        contacts.update({contact['tel_contact']:contact['nombre_contact']})

    print(contacts)
    # print(contacts.keys())
    self.stack.setProp(MacLayer.PROP_CONTACTS,  list(contacts.keys()))

in the same file "run.py" add the following code that is equivalent to setInterval of javascript, in which it calls every few seconds to the "sincContacts" function of the MacStack class

def set_intervalContacts(sec):
    def func_wrapper():
        set_intervalContacts(sec) 
        c.sincContacts()
    t = threading.Timer(sec, func_wrapper)
    t.start()
    return t

set_intervalContacts(300)
LabtronicsDesign commented 6 years ago

Brilliant thanks, I'll give it a go.

JosemariC commented 6 years ago

Hello,

Definitely the correct syntax of writing the contacts in the config.py file is that of option 2 that comments @elchory96 (country code without the + sign and the mobile number)?

I understand that it is important to avoid being banned

Thank you very much

jhonarodriguez commented 6 years ago

hello jose, up to now I am working saving the number of the following way:

(country code) (phone number)

omitting the sign +

I hope it helps you.

JosemariC commented 6 years ago

Hello @elchory96

I have a question about this way of synchronizing contacts. So it is no longer necessary to record them in the config.py file, right?

Best regards,

jhonarodriguez commented 6 years ago

Currently, you have to manually add the contacts in the config.py since it is the first file that is processed to make the whatsapp connection ... what I did was a recursive function where every half hour is called, in order to call the contacts from a database and send them to whatsapp ... at the beginning you must place the contacts in the config.py file and then make the recursive function so that it is updated automatically.

JosemariC commented 6 years ago

Hello, to avoid working on the config.py file I made the following change in the MacStack class of run.py.

original code is:

class MacStack(object):
    def __init__(self):
        builder = YowStackBuilder()

        self.stack = builder\
            .pushDefaultLayers(encryption)\
            .push(MacLayer)\
            .build()

        self.stack.setCredentials(credentials)

        self.stack.setProp(MacLayer.PROP_CONTACTS,  list(config.contacts.keys()))
        self.stack.setProp(PROP_IDENTITY_AUTOTRUST, True)

And changed code:

class MacStack(object):
    def __init__(self):
        builder = YowStackBuilder()

        self.stack = builder\
            .pushDefaultLayers(encryption)\
            .push(MacLayer)\
            .build()

        self.stack.setCredentials(credentials)

        with open('/root/whatsapp-aux/contactos.json') as json_data:
          contactsJSON = json.load(json_data)
          contacts = {
          }
          for contact in contactsJSON:
            contacts.update({contact['tel_contact']:contact['nombre_contact']})
          self.stack.setProp(MacLayer.PROP_CONTACTS,  list(contacts.keys()))
        self.stack.setProp(PROP_IDENTITY_AUTOTRUST, True)

This way we can save the task of keeping the contacts in config.py Do you think it's okay?

Is there any way to get a list of contacts really synchronized in the whatsapp environment?

Best regards,

jhonarodriguez commented 6 years ago

Remember that the init function is only executed once when you call the command ./start.sh, what you have is not wrong, you are reading a file where you have your contacts and you add them to whatsapp, but the idea is that when you write someone new to the bot, save the number in your contacts.json file with a random contact name, and obviously do not repeat it, and after saving it you have to send it back to Whatsapp, that's why I made a loop where you read each half an hour the database and bringing the contacts and adding them again in WhatsApp.

See that my example, I call normal in the init to config.contacts, but outside I loop, where I call the data and send it to self.stack.setProp (MacLayer.PROP_CONTACTS, list (contacts.keys ()))

JosemariC commented 6 years ago

Hi @elchory96 , sometimes (2 or 3 times every day synchronizing every 5 minutes) I get this warning or error during the synchronization of my contacts:

Stream Error type: xml-not-well-formed
{'xml-not-well-formed': None}

The json file in which I keep my contacts updated has this format:

 [
   { "nombre_contact": "Pepe", "tel_contact": "34666123456" },
   { "nombre_contact": "Juan", "tel_contact": "34666123457" },
   { "nombre_contact": "Luis", "tel_contact": "34667123456" }
 ]

What can be happening?

Thank you very much and best regards

jhonarodriguez commented 6 years ago

Effectively you have an error in the json, it must be {Key: Value}, it must be {numtel: contactname} being as follows:

[
   { "34666123456": "Pepe" },
   { "34666123457": "Juan"},
   { "34667123456": "Luis"}
 ]

try the change and tell me.

JosemariC commented 6 years ago

But that structure is that of my file in the operating system. When loaded with the for loop, it adapts to the form {key: value}:

  for contact in contactsJSON:
             contacts.update ({contact ['tel_contact']: contact ['contact_name']})?

Is this right?

jhonarodriguez commented 6 years ago

Exactly this way should be the filling of the array of contacts, {tel: namecontact}

danielcardeenas commented 6 years ago

Numbers must have country code, the exact same way they appear in the whatsapp application.

Example:

contacts = {
    "5218124340440": "Daniel Cardenas",
    "5218123113861": "Idk"
}

Where the entire number will be: +5218124340440 for Daniel Cardenas. This way the contacts should be synced automatically

ngethe commented 6 years ago

In the run.py file,

import the library threading and requests and in the class MacStack creates a new method called for example sincContacts

def sincContacts(self):
  r = requests
  urlglobal = 'urlget'
  pet = r.post(urlglobal+'getContactsBot')
  contactsJSON = pet.json()
  contacts = {
  }
  for contact in contactsJSON:
      contacts.update({contact['tel_contact']:contact['nombre_contact']})

  print(contacts)
  # print(contacts.keys())
  self.stack.setProp(MacLayer.PROP_CONTACTS,  list(contacts.keys()))

in the same file "run.py" add the following code that is equivalent to setInterval of javascript, in which it calls every few seconds to the "sincContacts" function of the MacStack class

def set_intervalContacts(sec):
  def func_wrapper():
      set_intervalContacts(sec) 
      c.sincContacts()
  t = threading.Timer(sec, func_wrapper)
  t.start()
  return t

set_intervalContacts(300)

Please give the entire two file I am getting bugs when trying to run this

ghost commented 6 years ago

@elchory96 What should be the 'urlget'?

I've tried the path, but I'm getting the error requests.exceptions.MissingSchema: Invalid URL '/home/ualas/Documents/whatsapp-framework/getContactsBot': No schema supplied. Perhaps you meant http:///home/ualas/Documents/whatsapp-framework/getContactsBot?

Thanks in advance.