Giter Site home page Giter Site logo

messengerbot's Introduction

messengerbot

Build Status Code Health https://coveralls.io/repos/github/geeknam/messengerbot/badge.svg?branch=master

Python client for Messenger Platform API

Installation

pip install messengerbot

Usage

Read about Messenger Platform

from messengerbot import MessengerClient, messages, attachments, templates, elements

# Manually initialize client
messenger = MessengerClient(access_token='your_token')

# With env var export MESSENGER_PLATFORM_ACCESS_TOKEN=your_token
from messengerbot import messenger

recipient = messages.Recipient(recipient_id='123')

# Send text message
message = messages.Message(text='Hello World')
request = messages.MessageRequest(recipient, message)
messenger.send(request)

# Send button template
web_button = elements.WebUrlButton(
   title='Show website',
   url='https://petersapparel.parseapp.com'
)
postback_button = elements.PostbackButton(
   title='Start chatting',
   payload='USER_DEFINED_PAYLOAD'
)
template = templates.ButtonTemplate(
   text='What do you want to do next?',
   buttons=[
       web_button, postback_button
   ]
)
attachment = attachments.TemplateAttachment(template=template)

message = messages.Message(attachment=attachment)
request = messages.MessageRequest(recipient, message)
messenger.send(request)

messengerbot's People

Contributors

dhontecillas avatar geeknam avatar jlmadurga avatar navyad avatar niespodziany avatar otso avatar rdkr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

messengerbot's Issues

Missing _ in to_dict() in Element

Hi,

Thank you for your great work here.

I just discovered the underscores before title and subtitle are missing in to_dist() in your Element class. I.e. I changed:

def to_dict(self):
    serialised = {
        'title': self.title,
        'item_url': self.item_url,
        'image_url': self.image_url,
        'subtitle': self.subtitle
    }

to this with underscores
def to_dict(self):
serialised = {
'title': self._title,
'item_url': self.item_url,
'image_url': self.image_url,
'subtitle': self._subtitle
}

Br, Henrik

PS. Not sure if this the right way to post it, as I am completely newbie here...

Error Handling

I've found a case error_data is missing in error message and only message is available. It is returned when using a token from a deleted facebook page.
This is the error:

{u'message': u'This Page access token belongs to a Page that has been deleted.', u'code': 190, u'type': u'OAuthException', u'fbtrace_id': u'CWTT1sIrGQG'}

Not to get an exception message should be use it when error_data is missing. Moreover I found a bug trying to raise MessengerError instead of MessengerException

Solution

class MessengerError(object):

    def __init__(self, *args, **kwargs):
        self.__dict__.update(**kwargs)

    def raise_exception(self):
        raise MessengerException(
            getattr(self, 'error_data', self.message) 
        )

How can we act if user selects a payload? Could you provide an example here

web_button = elements.WebUrlButton(
   title='Show website',
   url='https://petersapparel.parseapp.com'
)
postback_button = elements.PostbackButton(
   title='Start chatting',
   payload='USER_DEFINED_PAYLOAD'
)
template = templates.ButtonTemplate(
   text='What do you want to do next?',
   buttons=[
       web_button, postback_button
   ]
)
attachment = attachments.TemplateAttachment(template=template)

message = messages.Message(attachment=attachment)
request = messages.MessageRequest(recipient, message)
messenger.send(request)

so if user selects USER_DEFINED_PAYLOAD then how can I execute a function? which parameter i should look for

internel server error /webhook

Any solutions newbie here . followed the documentation ended up here.

  1. downloaded module
  2. open as pycharm project
  3. done webhook setup
  4. settings.py added accesstoken verify token
  5. developers facebook page added callback url ngrok lin/webhook . with same verify token
    when ever i send message to the bot on facebook getting this error in my terminal.

resolutions will be appreciated .
when get request on http://0bfd5c1a35fb.ngrok.io/webhook
i get multi value dict key error
error log:
[12/Jan/2021 19:52:03] "POST /webhook HTTP/1.1" 500 82026
Internal Server Error: /webhook
Traceback (most recent call last):
File "/home/najeeb/.local/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/najeeb/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/najeeb/.local/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/najeeb/.local/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/najeeb/facebookbot/FacebookMessengerBot-master/bot/views.py", line 114, in web_hook
return chat_handler(request)
File "/home/najeeb/facebookbot/FacebookMessengerBot-master/bot/views.py", line 95, in chat_handler
data = json.loads(request.body)
File "/usr/lib/python3.5/json/init.py", line 312, in loads
s.class.name))
TypeError: the JSON object must be str, not 'bytes'

Instance variable reference

class Attachment(object):

    def to_dict(self):
        return {
            'type': self.attachment_type,
            'payload': self.payload
        }

The above class does not have the attachment_type and payload attributes, I guess you have kept this to be used by child classes, which make sense.

Although creating the instance of Attachment is not the idea, But instance can be created. And in that case
to_dict() will break, since the attachment_type and payload attributes are not there.

There are two solution of the problem:
Solution 1: allows instantiation of Attachment and add attributes to the class. Will not recommend it since the idea is not to allow the instantiation of this class.

Solution 2: Does not allow instance of class while maintaining the inheritance of the attributes.

AttributeError: 'MessengerError' object has no attribute 'error_data'

I have been playing around with messengerbot. It works fine on OS X, but when I run it on an Ubuntu server it says:

m.send(request)
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/messengerbot/init.py", line 37, in send
**response.json()['error']
File "/usr/local/lib/python2.7/dist-packages/messengerbot/init.py", line 16, in raise_exception
self.error_data
AttributeError: 'MessengerError' object has no attribute 'error_data'

I am running Python 2.7.11 and just trying out the basic functions of messengerbot.

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.