Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Chat Bots

Chat Bots

Myles Braithwaite

January 11, 2017
Tweet

More Decks by Myles Braithwaite

Other Decks in Technology

Transcript

  1. Telegram Is Not Secure But it's Awesome for Writing Chat

    Bots Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 2
  2. — Telegram is an instant message service — It has

    desktop and mobile clients — Uses a non-standered encryption protocol MTProto Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 3
  3. from telegram.ext import Updater, CommandHandler def start(bot, update): bot.sendMessage(update.message.chat_id, text='Hello

    World!') def hello(bot, update): bot.sendMessage(update.message.chat_id, text='Hello {}.'.format(update.message.from_user.first_name)) updater = Updater('YOUR TOKEN HERE') updater.dispatcher.add_handler(CommandHandler('start', start)) updater.dispatcher.add_handler(CommandHandler('hello', hello)) updater.start_polling() updater.idle() Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 6
  4. def command_start(self, bot, update): msg = ("Hi! I'm a bot

    to control @MylesB's" "Spotify account.") self.send_message(bot, update, msg) Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 9
  5. def command_status(self, bot, update): # Check ot see if Spotify

    is currently running. if not self.control.is_running(): return self.send_message(bot, update, 'Spotify is not running.') # Get the current Player State and Track player_state = self.control.player_state() current_track = self.control.current_track() if player_state == 'stopped': return seld.send_message(bot, update, 'Spotify is stopped.') msg_line_one = 'Spotify is {0}\n'.format(player_state) msg_line_two = ('*{name}* by {artist} from ' '{album}.').format(**current_track) Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 11
  6. def send_message(self, bot, update, msg, disable_link_preview=True, **kwargs): return bot.sendMessage(update.message.chat_id, msg,

    reply_markup=ReplyKeyboardMarkup(self.keyboard), resize_keyboard=True, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=disable_link_preview, **kwargs) Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 13
  7. @property def keyboard(self): if self.control.player_state() == 'playing': play_pause_button = KeyboardButton('Pause')

    else: play_pause_button = KeyboardButton('Play') return [[play_pause_button], [KeyboardButton('Previous'), KeyboardButton('Next')]] Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 15
  8. def command_search(self, bot, update, args): results = self.spotify.search(q=' '.join(args)) msgs

    = [] result_msg = '*{0}* by {1} /play {2}' for r in results['tracks']['items']: msgs.append(result_msg.format(r['name'], r['artists'][0]['name'], r['uri'])) self.send_messages(bot, update, msgs) Myles Braithwaite | mylesb.ca | [email protected] | @mylesb 17