{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Session 3: 组合 Telegram 与 QQ (Connecting QQ with Telegram)\n",
    "\n",
    "* 把 QQ 消息和 Telegram 消息转发到服务器终端"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 借助工具\n",
    "\n",
    "**king-chat**: https://github.com/yingshaoxo/king-chat"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 原理\n",
    "\n",
    "TCP/IP 协议"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### IP 的两层含义\n",
    "\n",
    "1. Internet Protocol\n",
    "2. IP address (IP地址)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 实现要求\n",
    "\n",
    "光有传输控制协议是传不了数据的，至少有一个公网地址"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 举一些地址的示例\n",
    "\n",
    "`127.0.0.1` 只适用于当前电脑，只有该电脑的程序能够访问它\n",
    "\n",
    "`192.168.1.1` 是你家路由器分配的地址，任何连上你家WiFi的设备都可以访问这个地址\n",
    "\n",
    "`74.125.224.72` 是在世界范围内可访问的网址"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 具体实现\n",
    "\n",
    "把 QQ、Telegram 都抽象为`Client`\n",
    "\n",
    "把`有公网地址的服务器`上运行的总控程序当成`Server`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 安装 king-chat\n",
    "\n",
    "```\n",
    "git clone -b trunk https://github.com/twisted/twisted.git\n",
    "cd twisted\n",
    "sudo -H python3 setup.py sdist\n",
    "cd dist\n",
    "sudo pip3 install * --upgrade\n",
    "\n",
    "git clone https://github.com/yingshaoxo/king-chat.git\n",
    "cd king-chat\n",
    "sudo -H python3 setup.py sdist\n",
    "cd dist\n",
    "sudo pip3 install * --upgrade\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 写一个demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# server\n",
    "from king_chat import Server\n",
    "\n",
    "server = Server(ip=\"127.0.0.1\", port=5920)\n",
    "\n",
    "@server.on_received\n",
    "def handle(protocol, text):\n",
    "    print(text)\n",
    "    protocol.send_to_all_except_sender(text)\n",
    "\n",
    "server.start(wait=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# client\n",
    "from king_chat import Client\n",
    "\n",
    "client = Client(name=\"qq\", ip=\"127.0.0.1\", port=5920)\n",
    "\n",
    "@client.on_received\n",
    "def on_received(protocol, text):\n",
    "    print(text)\n",
    "\n",
    "client.start(wait=False)\n",
    "\n",
    "while 1:\n",
    "    text = input('words: ')\n",
    "    client.send(text)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
