import logging
import random

logger = logging.getLogger(__name__)


class Messenger(object): 
    def __init__(self, slack_clients):
        self.clients = slack_clients

    def send_message(self, channel_id, msg):
        # in the case of Group and Private channels, RTM channel payload is a complex dictionary
        if isinstance(channel_id, dict):
            channel_id = channel_id['id']
        logger.debug('Sending msg: {} to channel: {}'.format(msg, channel_id))
        channel = self.clients.rtm.server.channels.find(channel_id)
        channel.send_message("{}".format(msg.encode('ascii', 'ignore')))

    def write_help_message(self, channel_id):
        bot_uid = self.clients.bot_user_id()
        txt = '{}\n{}\n{}\n{}'.format(
            "> `<@" + bot_uid + "> classify [text]` - Classify the text into different topics",
            "> `<@" + bot_uid + "> entity [text]` - Extract entities such as Organization, Location, person etc from the text",
            "> `<@" + bot_uid + "> sentiment [text]` - Retrieve the sentiment from the text",
            "> `<@" + bot_uid + "> tag [text]` - Auto-tag the text",
            "> `<@" + bot_uid + "> attachment` - I'll demo a post with an attachment using the Web API. :paperclip:")
        self.send_message(channel_id, txt)

    def write_greeting(self, channel_id, user_id):
        greetings = ['Hi', 'Hello', 'Nice to meet you', 'Howdy', 'Salutations']
        txt = '{}, <@{}>!'.format(random.choice(greetings), user_id)
        self.send_message(channel_id, txt)

    def write_prompt(self, channel_id):
        bot_uid = self.clients.bot_user_id()
        txt = "I'm sorry, I didn't quite understand... Can I help you? (e.g. `<@" + bot_uid + "> help`)"
        self.send_message(channel_id, txt)

    def write_joke(self, channel_id):
        question = "Why did the python cross the road?"
        self.send_message(channel_id, question)
        self.clients.send_user_typing_pause(channel_id)
        answer = "To eat the chicken on the other side! :laughing:"
        self.send_message(channel_id, answer)


    def write_error(self, channel_id, err_msg):
        txt = ":face_with_head_bandage: my maker didn't handle this error very well:\n>```{}```".format(err_msg)
        self.send_message(channel_id, txt)

    def demo_attachment(self, channel_id):
        txt = "Tantal is your intelligent slackbot!"
        attachment = {
            "pretext": "We bring bots to life. :sunglasses: :thumbsup:",
            "title": "Text Analytics bot here!",
            "title_link": "https://beepboophq.com/",
            "text": txt,
            "fallback": txt,
            "image_url": "http://www.logospike.com/wp-content/uploads/2015/11/T_Logo_02.jpg",
            "color": "#7CD197",
        }
        self.clients.web.chat.post_message(channel_id, txt, attachments=[attachment], as_user='true')
        
    def demo_button(self, channel_id):
        txt = "Tantal is your intelligent slackbot!"
        attachment = {
            "text": "Would you like to play a game?",
            "attachments": [
                {
                    "text": "Choose a game to play",
                    "fallback": "You are unable to choose a game",
                    "callback_id": "wopr_game",
                    "color": "#3AA3E3",
                    "attachment_type": "default",
                    "actions": [
                        {
                            "name": "game",
                            "text": "Chess",
                            "type": "button",
                            "value": "chess"
                        },
                        {
                            "name": "game",
                            "text": "Falken's Maze",
                            "type": "button",
                            "value": "maze"
                        },
                        {
                            "name": "game",
                            "text": "Thermonuclear War",
                            "style": "danger",
                            "type": "button",
                            "value": "war",
                            "confirm": {
                                "title": "Are you sure?",
                                "text": "Wouldn't you prefer a good game of chess?",
                                "ok_text": "Yes",
                                "dismiss_text": "No"
                            }
                        }
                    ]
                }
            ]
        }
        self.clients.web.chat.post_message(channel_id, txt, attachments=[attachment], as_user='true')

@@ -2886,1757 +2886,4 @@
    %0A
-    def demo_button(self, channel_id):%0A        txt = %22Tantal is your intelligent slackbot!%22%0A        attachment = %7B%0A            %22text%22: %22Would you like to play a game?%22,%0A            %22attachments%22: %5B%0A                %7B%0A                    %22text%22: %22Choose a game to play%22,%0A                    %22fallback%22: %22You are unable to choose a game%22,%0A                    %22callback_id%22: %22wopr_game%22,%0A                    %22color%22: %22#3AA3E3%22,%0A                    %22attachment_type%22: %22default%22,%0A                    %22actions%22: %5B%0A                        %7B%0A                            %22name%22: %22game%22,%0A                            %22text%22: %22Chess%22,%0A                            %22type%22: %22button%22,%0A                            %22value%22: %22chess%22%0A                        %7D,%0A                        %7B%0A                            %22name%22: %22game%22,%0A                            %22text%22: %22Falken's Maze%22,%0A                            %22type%22: %22button%22,%0A                            %22value%22: %22maze%22%0A                        %7D,%0A                        %7B%0A                            %22name%22: %22game%22,%0A                            %22text%22: %22Thermonuclear War%22,%0A                            %22style%22: %22danger%22,%0A                            %22type%22: %22button%22,%0A                            %22value%22: %22war%22,%0A                            %22confirm%22: %7B%0A                                %22title%22: %22Are you sure?%22,%0A                                %22text%22: %22Wouldn't you prefer a good game of chess?%22,%0A                                %22ok_text%22: %22Yes%22,%0A                                %22dismiss_text%22: %22No%22%0A                            %7D%0A                        %7D%0A                    %5D%0A                %7D%0A            %5D%0A        %7D%0A        self.clients.web.chat.post_message(channel_id, txt, attachments=%5Battachment%5D, as_user='true')%0A

Update messenger.py