├── README.md
└── bard.py
/README.md:
--------------------------------------------------------------------------------
1 |
Google Bard Telegram Bot 🤖
2 |
3 |
4 | Google Bard is a Chat Based AI Tool from Google, This is a Python Telegram Bot script leveraging the aiogram library to interact with Google Bard through cookie value. This repository contains the core script that facilitates the communication between the user and Bard,
5 |
6 |
7 |
8 |
9 | ## 🌟 Features
10 |
11 | - 🔄 Asynchronous Communication: Leverage the power of async for seamless interactions
12 | - ⚙️ Error Handling: Robust error handling for a smoother user experience
13 | - 🍪 Accepts text prompts and generates text using the Google Bard through cookie value
14 | - 📲 Provides generated text as a response in Telegram
15 | - ✍️ Supports the `/start` command for welcoming users 👋 and the `/bard` command for generating text
16 |
17 | ## 🚀 Getting Started
18 |
19 | ### Prerequisites
20 |
21 | - Python 3.7 or higher
22 | - Libraries: `aiogram,` `requests`
23 | - A Telegram bot token.
24 | - `__Secure-1PSID` and `__Secure-1PSIDTS` from bard.google.com.
25 |
26 | ### How to get the 1PSID and 1PSIDTS Key
27 |
28 | 1. **Secure-1PSID Key**:
29 | - Visit the [Google Bard](https://bard.google.com/).
30 | - Open Inspect or `CTLR+Shift+I`
31 | - Go to the : Application → Cookies → Copy the value of `__Secure-1PSID cookie`.
32 | - Warning Do not expose the `Secure-1PSID`
33 |
34 | 2. **Secure-1PSIDTS Key**:
35 | - Visit the [Google Bard](https://bard.google.com/).
36 | - Open Inspect or `CTLR+Shift+I`
37 | - Go to the : Application → Cookies → Copy the value of `__Secure-1PSIDTS cookie`.
38 |
39 | ### Installation
40 |
41 | 1. Install the required Python packages:
42 |
43 | - This is a framework for Telegram Bot API. You can install it using pip:
44 | `````
45 | pip install aiogram==2.6
46 | - This is a popular Python HTTP library. You can install it with pip:
47 | `````
48 | pip install requests
49 | 2. Update the `bard.py` file with your [Telegram bot token], `V = Secure-1PSID cookie key`, and `W = Secure-1PSIDTS cookie key`
50 |
51 | 3. Run the Google Bard bot:
52 | `````
53 | python bard.py
54 | `````
55 | ## 📚 Usage
56 |
57 | 1. Start a chat with your bot on Telegram.
58 | 2. Ask question to Bard: `/bard `.
59 |
60 | ## Author
61 |
62 | - Name: Bisnu Ray
63 | - Telegram: [@SmartBisnuBio](https://t.me/SmartBisnuBio)
64 |
65 | Feel free to reach out if you have any questions or feedback.
66 |
--------------------------------------------------------------------------------
/bard.py:
--------------------------------------------------------------------------------
1 | """
2 | Author: Bisnu Ray
3 | Telegram: https://t.me/SmartBisnuBio
4 | """
5 |
6 | from aiogram import Bot, Dispatcher, executor, types
7 | from aiogram.types import ParseMode
8 | import random as R, string as S, json as C, requests as T, re
9 |
10 | # Replace 'YOUR_BOT_TOKEN_HERE' with your actual bot token
11 | API_TOKEN = 'IHUREIUHRUHIRIUGRE'
12 |
13 | # Initialize bot and dispatcher
14 | bot = Bot(token=API_TOKEN)
15 | dp = Dispatcher(bot)
16 |
17 | # BardBot initialization...
18 | V = 'HFDEBIRBOGJNBOTHGY67Y' #__Secure-1PSID Key
19 | W = 'ERUFGVR4IUGTIYG5R4YTG' #__Secure-1PSIDTS
20 |
21 |
22 | Q = 'utf-8'
23 | P = False
24 | O = 'https://bard.google.com/'
25 | N = print
26 | M = True
27 | B = Exception
28 | L = 'content'
29 | K = 'conversation_name'
30 | J = 'choice_id'
31 | I = 'SNlM0e'
32 | H = ''
33 | G = None
34 | F = '_reqid'
35 | E = 'response_id'
36 | D = 'conversation_id'
37 |
38 | class color:
39 | def red(*args):
40 | return print("\033[0;31m" + ' '.join(args) + '\033[0m')
41 |
42 | def cyan(*args):
43 | return print("\033[0;36m" + ' '.join(args) + '\033[0m')
44 |
45 |
46 | class BardBot:
47 | __slots__ = [
48 | 'headers', F, I, D, E, J, 'proxy', 'secure_1psidts',
49 | 'secure_1psid', 'session', 'timeout'
50 | ]
51 |
52 | def __init__(A, secure_1psid, secure_1psidts, proxy=G, timeout=20):
53 | D = proxy
54 | C = secure_1psid
55 | B = secure_1psidts
56 | E = {
57 | 'Host': 'bard.google.com',
58 | 'X-Same-Domain': '1',
59 | 'User-Agent':
60 | 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
61 | 'Content-Type':
62 | 'application/x-www-form-urlencoded;charset=UTF-8',
63 | 'Origin': 'https://bard.google.com',
64 | 'Referer': O
65 | }
66 | A._reqid = int(H.join(R.choices(S.digits, k=4)))
67 | A.proxy = D
68 | A.conversation_id = H
69 | A.response_id = H
70 | A.choice_id = H
71 | A.secure_1psid = C
72 | A.secure_1psidts = B
73 | A.session = T.session()
74 | A.session.headers = E
75 | A.session.proxies = D
76 | A.session.cookies.set('__Secure-1PSID', C)
77 | if B: A.session.cookies.set('__Secure-1PSIDTS', B)
78 | A.timeout = timeout
79 |
80 | def __m0e(A):
81 | if not (A.secure_1psid and
82 | A.secure_1psidts) or A.secure_1psid[-1] != '.':
83 | raise B('cookieError: Invalid cookies provided')
84 | C = A.session.get(O, timeout=10, allow_redirects=M)
85 | if C.status_code != 200:
86 | raise B(f"Invalid response: {C.status_code}")
87 | D = re.search('SNlM0e\\":\\"(.*?)\\"', C.text)
88 | if not D: raise B('cookieError: Get cookies from: bard.google.com (Must logged In)')
89 | return D.group(1)
90 |
91 | def ask(A, message):
92 | N = 'choices'
93 | O = {
94 | 'bl': 'boq_assistant-bard-web-server_20230713.13_p0',
95 | F: str(A._reqid),
96 | 'rt': 'c'
97 | }
98 | P = [[message], G,
99 | [A.conversation_id, A.response_id, A.choice_id]]
100 | Q = {
101 | 'f.req': C.dumps([G, C.dumps(P)]),
102 | 'at': A.__m0e()
103 | }
104 | J = A.session.post(
105 | 'https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate',
106 | params=O,
107 | data=Q,
108 | timeout=A.timeout)
109 | K = C.loads(J.content.splitlines()[3])[0][2]
110 | if not K: return {L: f"Unknown error: {J.text}."}
111 | B = C.loads(K)
112 | M = []
113 | if len(B) >= 3:
114 | if len(B[4][0]) >= 4:
115 | if B[4][0][4]:
116 | for R in B[4][0][4]:
117 | M.append(R[0][0][0])
118 | I = {
119 | L: B[4][0][1][0],
120 | D: B[1][0],
121 | E: B[1][1],
122 | 'factualityQueries': B[3],
123 | 'textQuery': B[2][0] if B[2] is not G else H,
124 | N: [{
125 | 'id': A[0],
126 | L: A[1]
127 | } for A in B[4]],
128 | 'images': M
129 | }
130 | A.conversation_id = I[D]
131 | A.response_id = I[E]
132 | A.choice_id = I[N][0]['id']
133 | A._reqid += 100000
134 | return I
135 |
136 | bard = BardBot(V, W)
137 |
138 | @dp.message_handler(commands=['bard'])
139 | async def handle_bard_command(message: types.Message):
140 | query = message.text[6:].strip()
141 | if not query:
142 | await message.answer("Please provide a Prompt after /bard", parse_mode="HTML")
143 | return
144 | loading_msg = await message.answer("Please wait for Bard response...", parse_mode="HTML")
145 |
146 | try:
147 | bard_response = bard.ask(query)
148 | await loading_msg.delete()
149 | await message.answer(f"[Google Bard -> {bard_response['content']} ]", parse_mode=ParseMode.MARKDOWN)
150 | except Exception as e:
151 | await loading_msg.delete()
152 | await message.answer(f"Sorry ! Bro Something Wrong : {e}", parse_mode="HTML")
153 |
154 |
155 | async def handle_empty_bard_command(message):
156 | await message.answer("Please provide a query after /bard", parse_mode="HTML")
157 |
158 | if __name__ == '__main__':
159 | # Start the bot
160 | executor.start_polling(dp, skip_updates=True)
161 |
--------------------------------------------------------------------------------