Blog

Picky Assist Sample Code for Sending & Receiving Messages 

Picky Assist Sample Code in Different Programming Languages for reference

Below are the sample code for different programming languages for developers reference , please note we highly recommend to use POST method to extract data from the webhook, if you want to reply to the incoming messages then you must give reply in JSON format.

If you are facing any issues with sample code we can provide 3rd party developers contact details who may charge a small amount to guide or fix your issue , please write to support@pickyassist.com if you required any assistances.

Pre-Requisite

  1. An Picky Assist Account (Dont have one? Click Here to Create a Developers Account for free)
  2. Webhooks Configured against a Project in Picky Assist Console (See How to Configure Webhooks)

PHP Sample Code for Sending & Receiving Messages

<?php

/*Mobile number with country code from the message came, 
if the sender mobile number is saved in the phone contacts then instead of number the contact name will come here as number. 
For Whatsapp Group the number will be Group Name @Sender Name / Number */
$mobile_number = isset($_REQUEST['number']) ? $_REQUEST['number'] : 0;

/*Text message received in the application - only first 1000 characters will be pushed to the server.*/
$msg = isset($_REQUEST['message-in']) ? $_REQUEST['message-in'] : 0;

/*On which messaging app the message has received 1=Whatsapp Personal | 2 = Whatsapp Business*/
$application = isset($_REQUEST['application']) ? $_REQUEST['application'] : 0;

/*What kind of message is received , text=1, photo=2, video=3, audio=4, location=5, document=6, contact=7*/
$type = isset($_REQUEST['type']) ? $_REQUEST['type'] : 0;

/*Unique id assigned by the picky assist application*/
$uniqueid = isset($_REQUEST['unique-id']) ? $_REQUEST['unique-id'] : 0;

if (!empty($mobile_number)) {
/*Reply should be in JSON format. The response parameters are : 
1. 'message-out' - Message you need to give it as reply.
2. 'delay' - If you would like to give response by setting a delay then please pass the delay value 
in “delay” variable , delay need to be set in seconds and maximum allowed delay is 3600 seconds 
i.e delay=10 means message will send after 10 seconds
*/

/*Giving Reply should be in JSON*/
$data = array('message-out' => ' Hello Picky','delay' => 0);

echo json_encode($data);
}
?>

 

ASP.NET Sample Code Using C#

using System.Collections.Generic;
using System.Web;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        // GET api/values
        public IHttpActionResult Get()
        {

            /* Mobile number with country code from the message came, 
             * if the sender mobile number is saved in the phone contacts then instead of number the contact name will come here as number. 
             * For Whatsapp Group the number will be Group Name @Sender Name / Number  */

            var mobileNumber = HttpContext.Current.Request["number"] ?? "0";
            /*Text message received in the application - only first 1000 characters will be pushed to the server. */
            var message = HttpContext.Current.Request["message-in"] ?? "";
            /*On which messaging app the message has received 1=Whatsapp Personal | 2 = Whatsapp Business */
            var application = HttpContext.Current.Request["application"] ?? "1";
            /*What kind of message is received , text=1, photo=2, video=3, audio=4, location=5, document=6, contact=7 */
            var type = HttpContext.Current.Request["type"] ?? "0";
            /*Unique id assigned by the picky assist application */
            var uniqueid = HttpContext.Current.Request["unique-id"] ?? "0";

            if (!string.IsNullOrEmpty(mobileNumber))
            {
                /*Reply should be in JSON format. The response parameters are : 
                1. 'message-out' - Message you need to give it as reply.
                2. 'delay' - If you would like to give response by setting a delay then please pass the delay value 
                in “delay” variable , delay need to be set in seconds and maximum allowed delay is 3600 seconds 
                i.e delay=10 means message will send after 10 seconds

                /*Giving Reply should be in JSON */
                var data = new Dictionary<string, object> {{"message-out", "Hello Picky"}, {"delay", 0}};
                return Ok(data);

            }
            return BadRequest("mobile number not provided");
        }


    }
}

 

Sample Code in Node JS with an Express Server

This is an example how to implement a very simple “parrot” bot (that replies exactly what has been said to it) using Node.js with an Express server:

 

const bodyParser = require('body-parser');
const express = require('express');
const app = express();

// enable extended to let nested POST
app.use(bodyParser.urlencoded({extended: true}));

// webhook set up
// on production authenticate each POST - you could add a token to the URL
app.post('/webhook/', (req, res) => {
  // for development only - console log the post content
  console.log(req.body);

  // builds the answer replicating the message sent
  let reply = {
    'message-out': req.body['message-in'],
    'delay': 0,
  };

  // Sends the reply - ENSURE a JSON response add JSON.stringfy just to be safe
  res.status(200).send(JSON.stringify(reply));
});

// Sets server port and logs message on success
app.listen(process.env.PORT || 6000, () => console.log('server is listening'));

 

Leave a Reply


Your email address will not be published. Required fields are marked *

Related Posts


June 6, 2019

Adding Widget Into WordPress

Adding Widget Into WordPress In this guide we will show how can add Picky Assist Chat Widget into a WordPress site in less than 5 minute. Watch Video Login into your WordPress Admin Account From the sidebar menu locate “Appearance” menu and click on it and from sub menu select “Editor” Editor may prompt for

May 29, 2019

Step By Step Getting Start Guide

Step By Step Getting Start Guide This guide will help you to get started with picky assist services , before getting start you must install and configure the picky assist bridge mobile app correctly in your mobile phone where the whatsapp, sms and call automation is required. You can refer following links , if you