{"id":94,"date":"2020-07-29T12:46:20","date_gmt":"2020-07-29T11:46:20","guid":{"rendered":"https:\/\/harkadya.com\/blog\/?p=94"},"modified":"2022-12-27T17:44:56","modified_gmt":"2022-12-27T16:44:56","slug":"fast-building-a-simple-chatbot","status":"publish","type":"post","link":"https:\/\/harkadya.com\/blog\/fast-building-a-simple-chatbot\/","title":{"rendered":"Fast building a (simple) chatbot"},"content":{"rendered":"\n<p>Chatbots are in fashion nowadays. Almost every business in an attempt to reduce costs or to allocate more work to human customer support tries to create a chatbot. Well, on my projects I  don&#8217;t really a need a chatbot or any type of AI to provide support, so I tried to make a simple bot to use on my daily life. You know the type: to write &#8216;Hello, good morning&#8217; to a group everyday, to reply back &#8216;thanks&#8217; when it&#8217;s your birthday&#8230; Perfect for someone lazy!<\/p>\n\n\n\n<p>Chatbots aren&#8217;t really that hard to create. There are loads of guides online, and amazing code is available on github for example. One of my favorites <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/vasani-arpit\/WBOT\" target=\"_blank\">was a bot for whatsapp<\/a>, an app I use daily. Oh and by the way, Facebook doesn&#8217;t like when you run bots on WhatsApp for a long time. They will ban you. So&#8230; avoid that.<\/p>\n\n\n\n<p>Built with NodeJs it uses WAPI.js to provide a python interface for interacting with WhatsAPP Web to send and receive Whatsapp messages.<\/p>\n\n\n\n<p>In bot.json you can edit the strings and the node application behavior:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n    \"appconfig\": {\n        \"headless\": false,\n        \"isGroupReply\":false,\n        \"webhook\":\"\"\n    },\n    \"bot\": &#91;{\n            \"contains\": &#91;],\n            \"exact\": &#91;\"hi\"],\n            \"response\": \"{hello|howdy}\"\n        },\n        {\n            \"contains\": &#91;],\n            \"exact\": &#91;\"hello\"],\n            \"response\": \"hi\",\n            \"file\":\"noFile.png\"\n        },\n        {\n            \"contains\": &#91;],\n            \"exact\": &#91;\"hy\"],\n            \"response\": \"hi\",\n            \"file\":\"github.png\"\n        },\n        {\n            \"contains\": &#91;],\n            \"exact\": &#91;\"hiee\"],\n            \"response\": \"hi\"\n        },\n        {\n            \"contains\": &#91;],\n            \"exact\": &#91;\"github\"],\n            \"response\": \"Git Hub Logo\",\n            \"file\":\"github.png\"\n        }\n    ],\n    \"blocked\": &#91;],\n    \"noMatch\":\"{\ud83e\udd14|\ud83d\udc4d}\",\n    \"smartreply\":{\n        \"suggestions\":&#91;],\n        \"clicktosend\":true\n    }\n}<\/code><\/pre>\n\n\n\n<p>As you can see by some strings, there&#8217;s not really any AI here. All the replies must be hand written, all the images must be self hosted. It&#8217;s simple, but fun.<\/p>\n\n\n\n<p>There&#8217;s also some bots based on selenium that reply only on valid commands.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class BotConfig(object):\n    last_msg = False\n    last_msg_id = False\n\n    command_history = &#91;]\n    last_command = \"\"\n\n    def __init__(self, contact_list):\n        self.contacts = contact_list\n\n    def get_contacts(self):\n        return self.contacts\n\n    def set_last_chat_message(self, msg, time_id):\n        self.last_msg = msg\n        self.last_msg_id = time_id\n\n    def get_last_chat_message(self):\n        return self.last_msg, self.last_msg_id\n\n    def set_last_command(self, command):\n        self.last_command = command\n        self.command_history.append(command)\n\n    def get_command_history(self):\n        return \"You have asked the following commands: \" + \", \".join(self.command_history)<\/code><\/pre>\n\n\n\n<p>Also&#8230; keeping up with python, here&#8217;s another <a href=\"https:\/\/github.com\/cyandterry\/Like-My-GF\">fun one.<\/a> Like-My-GF: You setup the &#8220;target&#8221; name, setup a cron job and that&#8217;s it. Every time your SO posts something on instagram, your like is there!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CONFIG = dict( client_id     = config_parser.get('Client', 'client_id'),\n               client_secret = config_parser.get('Client', 'client_secret'),\n               redirect_uri  = config_parser.get('Client', 'redirect_uri'),\n                 )\nOTHER = dict( scope        = &#91;'likes'],\n              access_token = config_parser.get('Access Token', 'access_token'),\n              target       = config_parser.get('Target', 'username'),\n              log_path     = config_parser.get('Path', 'log_path')+'like_my_gf.log',\n    )<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Chatbots are in fashion nowadays. Almost every business in an attempt to reduce costs or to allocate more work to human customer support tries to create a chatbot. Well, on my projects I don&#8217;t really a need a chatbot or any type of AI to provide support, so I tried to make a simple bot&hellip;<\/p>\n","protected":false},"author":1,"featured_media":97,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/posts\/94"}],"collection":[{"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":2,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":96,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/96"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/media\/97"}],"wp:attachment":[{"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harkadya.com\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}