Example #1
0
def on_chat_message(msg):
    global note, chat_id
    content_type, chat_type, chat_id = telepot.glance(msg)

    keyboard_on = ReplyKeyboardMarkup(keyboard=[    
                    [KeyboardButton(text='Open Sherlock')], [KeyboardButton(text='Close Sherlock')], [KeyboardButton(text='Leave Note')] ])

    keyboard_off = ReplyKeyboardHide(hide_keyboard=True)

    keyboard_in = InlineKeyboardMarkup(inline_keyboard=[
                   [InlineKeyboardButton(text='Yes!', callback_data='press')],
               ])

    print msg['text']
    bot.sendMessage(chat_id, msg['text'], reply_markup=keyboard_on)
    if note == False:

      if (msg['text'] == "Open Sherlock"):
        GPIO.output(20, True)
      elif (msg['text'] == "Close Sherlock"):
        GPIO.output(20, False)
      elif (msg['text'] == "Leave Note"): 
        bot.sendMessage(chat_id,'Note to be written:', reply_markup=keyboard_off)
        note = True

    else:
      lcd.clear()
      lcd.message(msg['text'])
      bot.sendMessage(chat_id, 'Done?', reply_markup=keyboard_in)
Example #2
0
async def on_chat_message(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    print('Chat:', content_type, chat_type, chat_id)

    if content_type != 'text':
        return

    command = msg['text'][-1:].lower()

    if command == 'c':
        markup = ReplyKeyboardMarkup(keyboard=[
                     ['Plain text', KeyboardButton(text='Text only')],
                     [dict(text='Phone', request_contact=True), KeyboardButton(text='Location', request_location=True)],
                 ])
        await bot.sendMessage(chat_id, 'Custom keyboard with various buttons', reply_markup=markup)
    elif command == 'i':
        markup = InlineKeyboardMarkup(inline_keyboard=[
                     [dict(text='Telegram URL', url='https://core.telegram.org/')],
                     [InlineKeyboardButton(text='Callback - show notification', callback_data='notification')],
                     [dict(text='Callback - show alert', callback_data='alert')],
                     [InlineKeyboardButton(text='Callback - edit message', callback_data='edit')],
                     [dict(text='Switch to using bot inline', switch_inline_query='initial query')],
                 ])

        global message_with_inline_keyboard
        message_with_inline_keyboard = await bot.sendMessage(chat_id, 'Inline keyboard with various buttons', reply_markup=markup)
    elif command == 'h':
        markup = ReplyKeyboardHide()
        await bot.sendMessage(chat_id, 'Hide custom keyboard', reply_markup=markup)
    elif command == 'f':
        markup = ForceReply()
        await bot.sendMessage(chat_id, 'Force reply', reply_markup=markup)
Example #3
0
    async def sendNormalMessage(self, id_, answer):
        """Formats and sends a chat message.

        Args:
            id_ (dict): A dictionary containing the ID of the chat where the message
                should be sent.
            answer (.answer.Answer): The answer to send.
        """
        chatId = id_["chat_id"]
        if answer.replyKeyboardMarkup is not None:
            markup = ReplyKeyboardMarkup(keyboard = answer.replyKeyboardMarkup)
        elif answer.inlineKeyboardMarkup is not None:
            markup = InlineKeyboardMarkup(inline_keyboard = answer.inlineKeyboardMarkup)
        else:
            markup = ReplyKeyboardHide()
        sentMessage = await self.sendMessage(chatId, answer.formattedAnswer, parse_mode="Markdown", reply_markup=markup, disable_web_page_preview=answer.disableWebPagePreview)
        history_manager.setMsgId(chatId, sentMessage["message_id"])
Example #4
0
    def on_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance(msg)

        print msg
        if chat_type == 'private' and content_type == 'text':
            text_in = msg['text']
            text = ''
            if text_in == '/cancel':
                text = 'Action canceled'
                self.temp_bot = Shop()
            elif text_in == '/remove':
                self.temp_bot = Shop()
                if len(self.list_shops) == 0:
                    text = "Ops, you don't have any shops! Try creating one with /new_shop"
                else:
                    self.temp_bot = Shop()
                    self.temp_bot.state = States.DELETING
                    bot.sendMessage(
                        chat_id=chat_id,
                        text='Which shop would you like to remove?',
                        reply_markup=self.get_shops_keyboard())
            elif text_in == '/new_shop' or text_in == '/start':
                self.temp_bot = Shop()
                self.temp_bot.state = States.WAITING_SHOP_API
                text = "Let's build a new Shopify bot shop! Yay!\n" \
                       "Please send me your Shopify API key\n" \
                       "Get it from shopify.com"
            elif text_in == '/help':
                self.temp_bot = Shop()
                text = "Greetings! I'm Shopify Father Bot, I take care of your Shopify telegram Shop.\n\n" \
                       "How does it work? Just type /new_shop and follow the instructions.\n" \
                       "What exactly do I do you may ask? Well, I take already existing stores from Shopify and " \
                       "create chatbot stores with zero effort! That means you'll have a Telegram Shopify shop in " \
                       "less than one minute!\n\n" \
                       "Try it out!\n\n" \
                       "I was made for the 2nd VanHackathon by @xitz0r @brucostam and @PabloMontenegro"
            elif text_in[0] == '/':
                self.temp_bot.state = Shop()
                text = "Sorry, I don't know that command :(\n" \
                       "Please check /help"
            elif self.temp_bot.state == States.WAITING_SHOP_API:
                self.temp_bot.shopify_api_key = text_in
                self.temp_bot.state = States.WAITING_SHOP_PASS
                text = 'Good, now please send me your Shopify API password'
            elif self.temp_bot.state == States.WAITING_SHOP_PASS:
                self.temp_bot.shopify_api_password = text_in
                self.temp_bot.state = States.WAITING_HOSTNAME
                text = 'Ok, now send me your Shopify hostname'
            elif self.temp_bot.state == States.WAITING_HOSTNAME:
                shop_url = 'https://%s:%s@%s.myshopify.com/admin' %\
                           (self.temp_bot.telegram_api_key, self.temp_bot.shopify_api_password, text_in)

                #               checking if api key, api password and hostname are valid
                shopify.ShopifyResource.set_site(shop_url)
                try:
                    shopify.Shop.current()
                    self.temp_bot.shopify_hostname = text_in
                    self.temp_bot.state = States.WAITING_TELEGRAM_API
                    text = 'Lastly, send me your Telegram bot token or simply forward me the @BotFather token message'
                except:
                    text = 'Ops! Something went wrong. Please check your Shopify API key, API password,' \
                           'hostname and try again!'
                    self.temp_bot = Shop()  #cleaning up
            elif self.temp_bot.state == States.WAITING_TELEGRAM_API:
                temp = re.search(
                    r'[0-9]{1,}:[a-zA-Z0-9._-]*', text_in
                )  # look for a telegram API pattern TODO use this pattern to validate api key
                if temp:
                    text_in = text_in[temp.start():temp.end()]

                    #                   checking if bot is already running
                    if path.isdir(text_in.split(':')[0]):
                        self.temp_bot = Shop()
                        text = "Ops! It looks like that bot is already in use. Please remove it before adding it again"
                    else:
                        try:
                            #                           checking if bot works
                            bot_checking = telepot.Bot(text_in)
                            new_bot = bot_checking.getMe()

                            self.temp_bot.telegram_api_key = text_in

                            bot.sendMessage(
                                chat_id=chat_id,
                                text=
                                'Ok, now let me do some magic and your Shopify store bot,\
                                            give me a second...',
                                reply_markup=ReplyKeyboardHide())
                            result = run_generic_store.create_new_store(
                                bot_name=self.temp_bot.telegram_api_key.split(
                                    ':')[0],
                                shopify_api_key=self.temp_bot.shopify_api_key,
                                shopify_api_password=self.temp_bot.
                                shopify_api_password,
                                shopify_hostname=self.temp_bot.
                                shopify_hostname,
                                telegram_api_key=self.temp_bot.telegram_api_key
                            )
                            if result:
                                self.list_shops.append(copy.copy(
                                    self.temp_bot))
                                self.shop_names.append(
                                    self.temp_bot.shopify_hostname)
                                text = "Hoorray! You're new telegram store is online and running!" \
                                       "Check it at @%s" % new_bot['username']
                            else:
                                text = "Ops, something went wrong. Please try again :("
                        except:
                            text = "Ops! That doesn't look like a valid telegram token :("
                else:
                    text = "Ops! That doesn't look like a valid telegram token :("

                self.temp_bot = Shop()
            elif self.temp_bot.state == States.DELETING:
                if text_in in self.shop_names:
                    self.shop_names.remove(text_in)
                    for shop in self.list_shops:
                        if shop.shopify_hostname == text_in:
                            self.list_shops.remove(shop)
                            run_generic_store.kill_store(shop.telegram_api_key)
                            text = '%s successfully removed' & text_in
                            break
                else:
                    text = "Sorry, but that shop does't exist!"

                self.temp_bot = Shop()
            else:
                text = "Sorry, I did't understand :(\nPlease check /help"

            if text != '':
                bot.sendMessage(chat_id=chat_id,
                                text=text,
                                reply_markup=ReplyKeyboardHide())