Announcement

Collapse
No announcement yet.

vB 5.4: how intercept a new post to forward using whatsapp?

Collapse
X
Collapse
First Prev Next Last
  • Filter
  • Time
  • Show
Clear All
new posts

    vB 5.4: how intercept a new post to forward using whatsapp?

    Hi there,
    I'm running a vB 5.4.3 installation here.
    The idea is to catch a new post to a forum by some user and forward it to a Whatsapp group (using something like https://www.phphive.info/category/whatsapi/).
    Is that possible?

    Currently, I have no idea, where to start using vB.
    Admittingly, I'm also no PHP expert, I'm coming from the Java world.

    The way I would like to set things up is as follows:
    1. the user(s) provide their Whatsapp-registered phone number in their personal settings
    2. the admin registers a specific thread as "Whatsappable" (I dont want the entire forum to be exported, but single threads that need immediate response) and creates a new Whatsapp group for it (manual task, outside of vB; vB just needs the reference)
    3. a user subscribes to that thread
    4. when a new post is created to that forum, the interceptor gets that message and copies it to the Whatsapp library, which identifies the Whatsapp group and forwards the content to it

    5. if a user responds using Whatsapp, there is a listener in the Whatsapp library, which takes that message, needs to identify the user, who just created the Whatsapp response and adds it as a new post to the vB instance; the new post may not be forwarded to Whatsapp

    I found the hooks.txt, but that does not describe any hook to the create message part of vB. Neither do I know which class to extend (I searched http://vb5support.com/resources/api/, but at lease "post" and "message" did not return anything usable).

    Can you give me some hints on this one?

    Thanks a lot.
    Stefan

    #2
    I think there's a hook before and after a post is saved. Check the list of hooks in one of the files in the do_not_upload folder.

    Comment


      #3
      Hey Glenn,

      thanks for your reply.
      Yes, I found the hook - it just wasnt documented (the function itself, not its contents). So I assumed, it would be part of the previous hook. Fixed that.

      Two more things on this one:
      1. do you know, how I can add a posting for a user (who is not logged in)? I need to call this, when a message comes in via Whatsapp
      2. sending the message to Whatsapp will probably take some time - and I dont want the user to wait for it. Can I somehow run that in the background? cron or sending to some other service on the same machine is fine - just shouldnt take too long for it to be send

      Thanks again.

      Stefan

      Comment


        #4
        1. You need to create a user session in order to post.
        2. I don't totally understand. Sending to Whatsapp will be triggered by the hook when a post is saved. I don't understand why you would need a cron or something.

        Comment


          #5
          Hello Glen,

          sorry, I didnt notice your reply.

          1. user session: all I know is the user's id. Can I create a user session for the user defined by that ID, not knowing the user's password? How? And is there any documentation on how to post a message?
          2. cron etc.: I'm not sure, how those hooks works in PHP. But I assume, the hook gets called somewhere in between and any further processing of the user's request continues only after the hook has finished to process. Now, if the hook's task (here: sending a message via Whatsapp) takes some time, the user has to wait. That's what I want to avoid. That's why I thought, starting any kind of a background task (cron, whatever) would finish the user's process quickly and have the message being send out at the same time. Of course, if the hook is run in the meantime (while the user's request gets processed) anyway, this problem is solved already.

          Thanks a lot.
          Stefan

          Comment


            #6
            I assume you would use cURL to call Whatsapp API to send a message. Maybe you could use asynchronous cURL call?

            https://stackoverflow.com/questions/...request-in-php

            Comment


              #7
              Hey Glen,

              this sounds good. I will try
              Any idea on the login / adding a post part (question 1)?

              Thanks a lot

              Stefan

              Comment


                #8
                Hey Glen,

                alright, I just gave those pThreads a try. But failed, pThreads is not available on Apache.
                Now, I can use the async cURL (as you suggested), but then I need to open some kind of listener / service (whatever you call it in PHP) that takes the internally sent request and processes it. How do I do that using vB?
                Thanks a lot
                ​​​​​​​Stefan

                Comment


                  #9
                  You have to install pthreads.

                  http://php.net/manual/en/pthreads.installation.php

                  I haven't used async cURL yet so I have no idea.

                  Comment


                    #10
                    Hmh, ok, I'll try something different.

                    When the page (any page, as far as I know) gets loaded/processed/whatever, vB runs certain processes in the background before or after page usage. This "load" also leads to the various cronjobs defined be run.
                    Do you know, how I can add a file/function/hook, such that it will be run, each time the page gets hit?

                    I will probably just write the content received by the addContent-hook to the database and let the background process do the actual processing.
                    During that time, it can also check for incoming messages from Whatsapp.

                    I will also add my stuff as a cronjob, but that's just additional (since this would run just every 5 minutes).

                    Thanks a lot

                    Stefan

                    Comment


                      #11
                      Hey,
                      well, sending an async AJAX request seems to be the way to go.
                      What do I need to do, to have vB export an interface, that I can call? What does that method need to look like? Do I need to "register" it anywhere? Can I restrict access to it in any way?

                      Thanks.
                      Stefan

                      Comment


                        #12
                        Use sockets to make async calls in PHP.

                        https://stackoverflow.com/questions/...924987#2924987
                        https://segment.com/blog/how-to-make...quests-in-php/

                        Comment


                          #13
                          Thanks,

                          yes, that's the client side.
                          But I'm talking about the server-side.
                          Like, I need to create some kind of interface, exported by the server such that I can send requests there. I want to deploy that interface as a vB-extension. So I need to know
                          - what does the interface have to look like (not from the content Json, but the environment needed for the server)
                          - do I need to register that file somewhere within vB, such that it gets exported
                          - can I restrict access to it - I know there is that .htaccess file

                          Thanks.
                          Stefan

                          Comment


                            #14
                            No, that's server-side. In the hook for saving a post, send an async request to WhatsApp using sockets.Then the hook doesn't need to wait for WhatsApp to finish the request and send the response back to you.

                            Comment


                              #15
                              right. That's one (definitely viable) option.
                              But here I'm having several issues, which I just dont know how to handle / if they're really relevant:
                              - I dont know, if the Whatsapp connection may be async altogether - maybe they send some confirmation, that I need later on (havent checked on this one yet)
                              - I cannot use that request to check for new messages as well

                              That's why I wanted to send an async message to some internal interface (function), that would
                              1. process the outgoing message
                              2. check for incoming messages

                              That's why I'm asking how to export that interface and restrict access to it.

                              Stefan

                              Comment

                              Working...
                              X