|
| 1 | +Title: Bot telegram mais web scraping |
| 2 | +Slug: bot-telegram-mais-web-scraping |
| 3 | +Date: 2016-10-23 20:30 |
| 4 | +Tags: python,blog,tutorial,aulas |
| 5 | +Author: Pedro Souza. |
| 6 | +About_author: Just another Programmer and Security Researcher, just a noob., |
| 7 | +Email: souza.vipedro@gmail.com |
| 8 | +Github: Pedro-Souza |
| 9 | +Facebook: https://www.facebook.com/DeveloperPS |
| 10 | +Category: Python, Bot, Telegram, Scraping |
| 11 | + |
| 12 | + |
| 13 | +Irei separa o artigo em 2 partes para não ficar extenso. Nessa primeira |
| 14 | +parte irei falar um pouco como criar um bot no telegram e como |
| 15 | +programa-lo para nos responder. |
| 16 | + |
| 17 | +[**Bot simples.**](/bot-telegram-e-web-scraping.md) |
| 18 | +**Bot e Web Scraping** |
| 19 | + |
| 20 | +Primeiro de tudo precisamos cria o bot, para isso usamos o próprio bot |
| 21 | +do telegram que faz isso para gente. Para isso bastar iniciar uma |
| 22 | +conversa com o [@BotFather](https://web.telegram.org/#/im?p=@BotFather), ele irá nós da algumas |
| 23 | +opções: |
| 24 | + |
| 25 | +``` |
| 26 | +/newbot - create a new bot |
| 27 | +/token - generate authorization token |
| 28 | +/revoke - revoke bot access token |
| 29 | +/setname - change a bot's name |
| 30 | +/setdescription - change bot description |
| 31 | +/setabouttext - change bot about info |
| 32 | +/setuserpic - change bot profile photo |
| 33 | +/setinline - change inline settings |
| 34 | +/setinlinegeo - toggle inline location requests |
| 35 | +/setinlinefeedback - change inline feedback settings |
| 36 | +/setcommands - change bot commands list |
| 37 | +/setjoingroups - can your bot be added to groups? |
| 38 | +/setprivacy - what messages does your bot see in groups? |
| 39 | +/deletebot - delete a bot |
| 40 | +/newgame - create a new game |
| 41 | +/listgames - get a list of your games |
| 42 | +/editgame - edit a game |
| 43 | +/deletegame - delete an existing game |
| 44 | +/cancel - cancel the current operation |
| 45 | +
|
| 46 | +``` |
| 47 | + |
| 48 | +As que nós interessa por enquanto são: |
| 49 | + |
| 50 | +``` |
| 51 | +/newbot - Cria um novo bot. |
| 52 | +/setdescription - Adiciona uma descrição ao nosso bot. |
| 53 | +/setuserpic - Adiciona uma imagem ao nosso bot. |
| 54 | +``` |
| 55 | + |
| 56 | +Feito isso agora temos um token, que iremos usar para dar funções e vida |
| 57 | +ao bot. Para isso iremos usar a lib telegram-bot, ela irá facilitar a |
| 58 | +nosso vida, assim não iremos precisar mexer diretamente com a API do |
| 59 | +telegram. |
| 60 | + |
| 61 | +### Instalando telegram-bot utilizando o pip |
| 62 | + |
| 63 | +```bash |
| 64 | +pip install python-telegram-bot |
| 65 | +``` |
| 66 | + |
| 67 | +Agora com a biblioteca instalada iremos programar um mini bot para nós falar as horas. |
| 68 | + |
| 69 | +```python |
| 70 | + |
| 71 | +#!/usr/bin/env python3 |
| 72 | +# -*- coding:utf-8 -*- |
| 73 | + |
| 74 | +from telegram.ext import Updater, CommandHandler |
| 75 | +from time import strftime |
| 76 | + |
| 77 | +up = Updater('Insira o token aqui.') |
| 78 | + |
| 79 | + |
| 80 | +def Horas(bot, update): |
| 81 | + |
| 82 | + msg = "Olá {user_name} agora são: " |
| 83 | + msg += strftime('%H:%M:%S') |
| 84 | + |
| 85 | + bot.send_message(chat_id=update.message.chat_id, |
| 86 | + text=msg.format( |
| 87 | + user_name=update.message.from_user.first_name)) |
| 88 | + |
| 89 | + |
| 90 | +up.dispatcher.add_handler(CommandHandler('horas', Horas)) |
| 91 | +up.start_polling() |
| 92 | + |
| 93 | +``` |
| 94 | + |
| 95 | +### Entendendo o código. |
| 96 | + |
| 97 | +1 - Importamos tudo que iremos utilizar. <br > |
| 98 | +2 - Informamos o token do nosso bot. <br > |
| 99 | +3 - Criamos uma função que pega a horas com strftime e responde no chat. <br > |
| 100 | +4 - Criamos um comando para o nosso bot, no caso o /horas. <br > |
| 101 | +5 - Startamos o bot.<br > |
| 102 | + |
| 103 | +Feito isso quando mandar um /horas para o bot ele irá nos responder com: "Olá SeuNome agora são |
| 104 | +Horas." |
| 105 | + |
| 106 | +Caso você queira adicionar mais funções ao bot, |
| 107 | +[aqui](http://python-telegram-bot.readthedocs.io/en/latest/) está a documentação da biblioteca. |
| 108 | + |
| 109 | +Na próxima parte iremos escolher alguns site que fale sobre Python e fazer Scraping nele, assim |
| 110 | +sempre que ele tiver uma nova postagem nosso bot vai nós enviar uma mensagem informando. |
| 111 | + |
| 112 | + |
| 113 | + |
0 commit comments