Skip to content

Instantly share code, notes, and snippets.

@lucaspolo
Created November 13, 2021 03:39
Show Gist options
  • Save lucaspolo/30c19147beca8d677ef79b1d836d039c to your computer and use it in GitHub Desktop.
Save lucaspolo/30c19147beca8d677ef79b1d836d039c to your computer and use it in GitHub Desktop.
import os
from telegram.ext import Updater, CommandHandler
import requests
def hello_world(update, context):
update.message.reply_text(
"Olá Mundo! Sou o novo bot."
)
def busca_cep(update, context):
zipcode=context.args[0]
update.message.reply_text(f'Buscando endereço do CEP {zipcode}')
try:
response = requests.get(
url=f'https://viacep.com.br/ws/{zipcode}/json'
)
response.raise_for_status()
message = [
f'{k}: {v}\n'
for k, v in response.json().items()
]
update.message.reply_text(''.join(message))
except Exception as exc:
update.message.reply_text(f'Problemas ao buscar endereço do CEP {zipcode}')
def main():
token = os.getenv('TELEGRAM_TOKEN')
updater = Updater(token=token)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', hello_world));
dispatcher.add_handler(CommandHandler('cep', busca_cep, pass_args=True));
updater.start_polling()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment