|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import requests |
| 4 | +import time |
| 5 | +import argparse |
| 6 | +import os |
| 7 | +import json |
| 8 | + |
| 9 | +from requests.compat import urljoin |
| 10 | +from dialogue_manager import DialogueManager |
| 11 | + |
| 12 | + |
| 13 | +class BotHandler(object): |
| 14 | + """ |
| 15 | + BotHandler is a class which implements all back-end of the bot. |
| 16 | + It has tree main functions: |
| 17 | + 'get_updates' — checks for new messages |
| 18 | + 'send_message' – posts new message to user |
| 19 | + 'get_answer' — computes the most relevant on a user's question |
| 20 | + """ |
| 21 | + |
| 22 | + def __init__(self, token, dialogue_manager): |
| 23 | + self.token = token |
| 24 | + self.api_url = "https://api.telegram.org/bot{}/".format(token) |
| 25 | + self.dialogue_manager = dialogue_manager |
| 26 | + |
| 27 | + def get_updates(self, offset=None, timeout=30): |
| 28 | + params = {"timeout": timeout, "offset": offset} |
| 29 | + raw_resp = requests.get(urljoin(self.api_url, "getUpdates"), params) |
| 30 | + try: |
| 31 | + resp = raw_resp.json() |
| 32 | + except json.decoder.JSONDecodeError as e: |
| 33 | + print("Failed to parse response {}: {}.".format(raw_resp.content, e)) |
| 34 | + return [] |
| 35 | + |
| 36 | + if "result" not in resp: |
| 37 | + return [] |
| 38 | + return resp["result"] |
| 39 | + |
| 40 | + def send_message(self, chat_id, text): |
| 41 | + params = {"chat_id": chat_id, "text": text} |
| 42 | + return requests.post(urljoin(self.api_url, "sendMessage"), params) |
| 43 | + |
| 44 | + def get_answer(self, question): |
| 45 | + if question == '/start': |
| 46 | + return "Hi, I am your project bot. How can I help you today?" |
| 47 | + return self.dialogue_manager.generate_answer(question) |
| 48 | + |
| 49 | + |
| 50 | +def parse_args(): |
| 51 | + parser = argparse.ArgumentParser() |
| 52 | + parser.add_argument('--token', type=str, default='') |
| 53 | + return parser.parse_args() |
| 54 | + |
| 55 | + |
| 56 | +def is_unicode(text): |
| 57 | + return len(text) == len(text.encode()) |
| 58 | + |
| 59 | + |
| 60 | +class SimpleDialogueManager(object): |
| 61 | + """ |
| 62 | + This is the simplest dialogue manager to test the telegram bot. |
| 63 | + Your task is to create a more advanced one in dialogue_manager.py." |
| 64 | + """ |
| 65 | + |
| 66 | + def generate_answer(self, question): |
| 67 | + return "Hello, world!" |
| 68 | + |
| 69 | + |
| 70 | +def main(): |
| 71 | + args = parse_args() |
| 72 | + token = args.token |
| 73 | + |
| 74 | + if not token: |
| 75 | + if not "TELEGRAM_TOKEN" in os.environ: |
| 76 | + print("Please, set bot token through --token or TELEGRAM_TOKEN env variable") |
| 77 | + return |
| 78 | + token = os.environ["TELEGRAM_TOKEN"] |
| 79 | + |
| 80 | + manager = DialogueManager() |
| 81 | + bot = BotHandler(token, manager) |
| 82 | + |
| 83 | + print("Ready to talk!") |
| 84 | + offset = 0 |
| 85 | + while True: |
| 86 | + updates = bot.get_updates(offset=offset) |
| 87 | + for update in updates: |
| 88 | + print("An update received.") |
| 89 | + if "message" in update: |
| 90 | + chat_id = update["message"]["chat"]["id"] |
| 91 | + if "text" in update["message"]: |
| 92 | + text = update["message"]["text"] |
| 93 | + if is_unicode(text): |
| 94 | + print("Update content: {}".format(update)) |
| 95 | + bot.send_message(chat_id, bot.get_answer(update["message"]["text"])) |
| 96 | + else: |
| 97 | + bot.send_message(chat_id, "Hmm, you are sending some weird characters to me...") |
| 98 | + offset = max(offset, update['update_id'] + 1) |
| 99 | + time.sleep(1) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments