Creating a full-fledged Viberbot. Part two - first contact or "conversion_started"

Sending the first message to the user - welcome and sign


In the first part, we learned how to launch a starter, install webhook for our botviber project.

In this 2nd, we will learn how to send the first message shown to our users, create links to search and launch our bot, both inside ViberURL and NoViberURL

image


We will form links to the bot


1) Offline QR - it can be downloaded from the viber affiliate admin area

2) Link for the ViberURL messenger and messenger transfer

viber://pa?chatURI=dinner&context=fromhabr

3) NoViberURL redirect link on the Internet there are many such URL shorteners - but they all look like spam, so I recommend creating your own code from three lines of PHP and putting the file on your hosting, here is the contents of the foot.php file

<?php
header ("Location: viber://pa?chatURI=dinner");
exit();

Greetings botviber or "event": "conversation_started"


After opening the QR or clicking on the link to the address of our application from Viber (Jetty) servers, POST requests with an event ("event": "conversation_started") arrive and their full content looks like this:

INCOMING PARAMETERS POST - or JSON of 13 fields
{
  "chat_hostname": "SN-327_", #  Viber    -   "chat_hostname"
  "event": "conversation_started", #          
  "context": "fromlanding", #    UTM     
  "message_token": 5406893180055821524, #  
  "subscribed": false, #    ,   true   
  "timestamp": 1581161565470, # UNIXTIME 
  "type": "open", #  
  "user": {
    "api_version": 8,  #   API   . 
    "avatar": "https://media-direct.cdn.viber.com/avatar...", #      -   ,   
    "country": "RU", #       (UA-, BY-, IL-, MD - )
    "id": "J2k6sasdgghaazDeoXVYww==", # ID    
    "language": "ru", #      (  )
    "name": "Denis"#   Viber -      -     name  
  }
}


But in most cases we are only interested in these 5 fields
{
  "context": "fromlanding", #      
  "subscribed": false,
   "user": {
    "country": "RU", #      
    "id": "J2k6sasdgghaazDeoXVYww==", # ID    
    "name": "Denis"#   ,   .
  }
}


And sometimes we only need one field
"Id": "J2k6sasdgghaazDeoXVYww =="

This is the main Viber identifier of the user, an analogue of chat_id in telegram.
And, please note that in the chat_id cart, the one messenger in all bots is the same as the mobile number in all bots, and the id of the vibe is like a token and is unique within your one bot. In another bot, your id will be different ...

We process them and answer to get one of the three answers as in the screenshot above
a) type = "text"
b) and here we see the classic case of type = "rich_media"
c) on the third screen Type = "keyboard"

More details on the viber REST API , and below we will consider how to get the first option

As we already see, botviber can be branded for any business or a mini shop, and also tailor styles to your main site

1) to process user messages in the myviberbot / views.py project file, make the changes:


#

import requests #      
import json 
from django.shortcuts import HttpResponse
from django.views.decorators.csrf import csrf_exempt

auth_token = '45df835d27d01f-cd2e7wetwerga18a8-9a7wert786234' #!     
url = 'https://chatapi.viber.com/pa/send_message'
headers = {'X-Viber-Auth-Token': auth_token}

#     
def sending(func):
    def wrapped(*args):
        return requests.post(url, json.dumps(func(*args)), headers=headers)
    return wrapped

#  
@sending
def send_text(agent, text, track=None):
    m = dict(receiver=agent, min_api_version=2, tracking_data=track, type="text", text=text)
    return m

@csrf_exempt
def trx_bot(request):
    if request.method == "POST":
        viber = json.loads(request.body.decode('utf-8'))
        if viber['event'] == 'conversation_started':
            print(" ")
            conversation(viber) #  -    .
        elif viber['event'] == 'webhook':
            #print(viber)
            #print("Webhook  ")
            return HttpResponse(status=200)
        else:
            print("  Webhook -   ,   POSTMAN") 
            return HttpResponse(status=500)
        return HttpResponse(status=200)


def conversation(viber):
    id = viber['user']['id']
    if viber['subscribed']:
	    send_text(id, '  ')
    else:
        send_text(id, '   ?\n\n   .      ...')


Restart our Django project.

Unfortunately, “Rakuten Viber” does not give official pages to PA and the community to anyone, exceptions to large brands and public figures, therefore, for SEO promotion and PR on social networks, etc. I recommend creating a minilanding - a small page that everyone goes to from the main site or others. Here is a sample of resources: Successfully launch botviber. To be continued ... In the next article, we will analyze event ["message"], for processing user messages, you will learn what to do to conduct a dialogue, how to separate the following messages, generally how to teach botviber to understand that these are the following. (Creating a full-fledged Viberbot on Django 2 and the Viber REST API. Part Three - Message) Materials: viber REST API documentation















Django version 2.2
Radio-miner

Source: https://habr.com/ru/post/undefined/


All Articles