<commit_before>import requests
import util
from plugin_handler import PluginInit


class TelegramAPI:
    def __init__(self, bot_token, plugin_list):
        self.url = "https://api.telegram.org/bot{0}/".format(bot_token)
        self.session = requests.session()
        self.update_id = 0
        self.getMe = self.get_me()
        self.plugin_handle = PluginInit(plugin_list, self.getMe)
        self.current_msg = None

    def get_update(self):  # Gets new messages and sends them to plugin_handler
        url = "{}getUpdates?offset={}".format(self.url, self.update_id)
        response = util.fetch(self.session, url)
        for i in response['result']:
            self.current_msg = i['message']
            self.route_return(self.plugin_handle.process_regex(self.current_msg))
            self.update_id = i['update_id'] + 1  # Updates update_id's value

    def route_return(self, returned_value):  # Figures out where plugin return values belong
        content = {}
        if isinstance(returned_value, str):  # If string sendMessage
            content['text'] = returned_value
            self.send_message(content)
        elif isinstance(returned_value, dict):
            if 'text' in returned_value:
                self.send_message(returned_value)
            elif returned_value['method'] == 'forwardMessage':
                del returned_value['forward_message']
                self.forward_message(returned_value)
            elif 'send' in returned_value['method']:
                method = returned_value['method'].replace('send', '')
                file = returned_value['file']
                if 'data' in returned_value:
                    data = returned_value['data']
                else:
                    data = {}
                self.send_stuff(method, file, data)

    def get_me(self):  # getMe
        url = "{}getMe".format(self.url)
        return util.fetch(self.session, url)

    def send_message(self, content):  # sendMessage
        url = "{}sendMessage".format(self.url)  # Creates URL
        default = {  # Default return
            'chat_id': self.current_msg['chat']['id'],
            'text': "",
            'parse_mode': "HTML",
            'reply_to_message_id': self.current_msg['message_id']
        }
        try:
            for k, v in content:
                default[k] = v
        except ValueError:
            default['text'] = content['text']
        return util.make_request(self.session, url, default)  # Sends it to off to be sent

    def forward_message(self, content):  # Forwards a message, note2self. add error handling
        url = "{}forwardMessage".format(self.url)  # Creates URL
        default = {  # Default return
            'chat_id': self.current_msg['chat']['id'],
            'from_chat_id': self.current_msg['chat']['id'],
            'message_id': self.current_msg['message_id']
        }
        for k, v in content:
            default[k] = v
        return util.make_request(self.session, url, default)

    def send_stuff(self, method, file, data):
        url = "{}send{}".format(self.url, method)
        default = {
            'chat_id': self.current_msg['chat']['id'],
        }
        for k, v in data:
            default[k] = v<commit_msg>Fix bug with iterating<commit_after>import requests
import util
from plugin_handler import PluginInit


class TelegramAPI:
    def __init__(self, bot_token, plugin_list):
        self.url = "https://api.telegram.org/bot{0}/".format(bot_token)
        self.session = requests.session()
        self.update_id = 0
        self.getMe = self.get_me()
        self.plugin_handle = PluginInit(plugin_list, self.getMe)
        self.current_msg = None

    def get_update(self):  # Gets new messages and sends them to plugin_handler
        url = "{}getUpdates?offset={}".format(self.url, self.update_id)
        response = util.fetch(self.session, url)
        for i in response['result']:
            self.current_msg = i['message']
            self.route_return(self.plugin_handle.process_regex(self.current_msg))
            self.update_id = i['update_id'] + 1  # Updates update_id's value

    def route_return(self, returned_value):  # Figures out where plugin return values belong
        content = {}
        if isinstance(returned_value, str):  # If string sendMessage
            content['text'] = returned_value
            self.send_message(content)
        elif isinstance(returned_value, dict):
            if 'text' in returned_value:
                self.send_message(returned_value)
            elif returned_value['method'] == 'forwardMessage':
                del returned_value['forward_message']
                self.forward_message(returned_value)
            elif 'send' in returned_value['method']:
                method = returned_value['method'].replace('send', '')
                file = returned_value['file']
                if 'data' in returned_value:
                    data = returned_value['data']
                else:
                    data = {}
                self.send_stuff(method, file, data)

    def get_me(self):  # getMe
        url = "{}getMe".format(self.url)
        return util.fetch(self.session, url)

    def send_message(self, content):  # sendMessage
        url = "{}sendMessage".format(self.url)  # Creates URL
        default = {  # Default return
            'chat_id': self.current_msg['chat']['id'],
            'text': "",
            'parse_mode': "HTML",
            'reply_to_message_id': self.current_msg['message_id']
        }
        try:
            for k, v in content:
                default[k] = v
        except ValueError:
            default['text'] = content['text']
        return util.make_request(self.session, url, default)  # Sends it to off to be sent

    def forward_message(self, content):  # Forwards a message, note2self. add error handling
        url = "{}forwardMessage".format(self.url)  # Creates URL
        default = {  # Default return
            'chat_id': self.current_msg['chat']['id'],
            'from_chat_id': self.current_msg['chat']['id'],
            'message_id': self.current_msg['message_id']
        }
        for k, v in content:
            default[k] = v
        return util.make_request(self.session, url, default)

    def send_stuff(self, method, file, data):
        url = "{}send{}".format(self.url, method)
        default = {
            'chat_id': self.current_msg['chat']['id'],
        }
        for k, v in data.items():
            default[k] = v
{'fraction_non_alphanumeric': 0.11854010121147063, 'fraction_numerical': 0.0012268057046465265, 'mean_word_length': 2.1986267778322706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1575998', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1824, 'n_words': 422}