Giter Club home page Giter Club logo

pymessengerbotapi's Introduction

pyMessengerBotAPI

A Simple Python Wrapper for Messenger Bot API

##Requirements

  1. This API is tested with Python 2.7
  2. A token from the app you will create on Developers Page
  3. Flask should be installed
  4. A webhook to receive messages. I used ngrok for this.

##Writing your first bot ###Before proceeding further please go through the documentation on developers page

A template main.py is provided. You should use that as your bot page.

###Simple Echo Bot

screenshot

def process_message(data):
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  
  bot.send_message(sender,"Welcome to Peter's Hats, what are you looking for today?")

###Using Buttons ####send_button_message(sender, text,buttons,metadata=None,quick_reply=None) #####create_buttons(type,title,payload=None,url=None)

screenshot

def process_message(data):              
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  text = data['entry'][0]['messaging'][0]['message']['text']
  
  button1 = bot.create_button("web_url","Show Website",url="http://google.com")
  button2 = bot.create_button("postback","Start Chatting",payload="test123")
  
  bot.send_button_message(sender,"What do you want to do next?",[button1,button2])

###Using Generic Messages ####send_generic(sender, elements,metadata=None,quick_reply=None): #####create_element(title,item_url=None,image=None,subtitle=None,buttons=[])

screenshot

def process_message(data):              
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  text = data['entry'][0]['messaging'][0]['message']['text']
  
  button1 = bot.create_button("web_url","hello",url="http://google.com")
  button2 = bot.create_button("postback","test",payload="test123")
  title1 = "Welcome to Peter's Hats!"
  image1 = "http://xyz.png"
  subtitle1 = "We've got the right hat for everyone"
  
  element1 = bot.create_element("Test1",image=image1,subtitle=subtitle1,buttons=[button1,button2])
  
  bot.send_generic_message(sender,[element1])

screenshot

###Sending Quick Replies #####create_quick_reply(title,payload) you can attach them with text,photo,audio,videos

def process_message(data):
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  quick_reply1 = bot.create_quick_reply("Red","red")
  quick_reply2 = bot.create_quick_reply("Green","green")
  bot.send_message(sender,"Pick a color:",quick_reply=[quick_reply1,quick_reply2])

###Send Photos, Videos, Audios ####send_attachment(self, sender, type, url,metadata=None,quick_reply=None)

screenshot

def process_message(data):
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  bot.send_attachment(sender,"image",url="http://xyx.png")
  
  bot.send_attachment(sender,"video",url="http://xyx.mp4")
  
  bot.send_attachment(sender,"audio",url="http://xyx.mp3")

###Get User Info ####get_user(self,sender)

def process_message(data):
  sender = data['entry'][0]['messaging'][0]['sender']['id']
  user = bot.get_user(sender)

Output :

{
  "first_name": "Peter",
  "last_name": "Chang",
  "profile_pic": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13055603_10105219398495383_8237637584159975445_n.jpg?oh=1d241d4b6d4dac50eaf9bb73288ea192&oe=57AF5C03&__gda__=1470213755_ab17c8c8e3a0a447fed3f272fa2179ce",
  "locale": "en_US",
  "timezone": -7,
  "gender": "male"
} 

##Thread Settings to improve user experience

You should visit Thread Setting Page

you can enable this greetings, persistent menu, get started message using curls

Note : Messenger Platoform is in beta, visiblity of different thread settings will took time.

###This is the first wrapper for messenger bot api. I tried to keep it really simple from understanding point of view. If you feel that you have something to contribute and imporve this wrapper, you are welcome to send pull requests.

###I will mention every person's name who will contribute for it's developement.

####I hope you all will accept this initial start from me and start creating bots in python for messenger too.

You can contact me for any issues pyMessengerBotApi Group on Telegram or on Messenger

###To Do's

  1. Message Handlers (Done, will update if other contribute for its development)
  2. Threading (Done, will update if other contribute for its development)
  3. Async Version

##Bots created using this API Wrapper

Sift Bot for Skype

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.