Skip to content

Commit 0ae06a7

Browse files
authored
Merge pull request #35 from wj-Mcat/master
add gif bot to support dynamic created gif file
2 parents 037c4d8 + 4aa2743 commit 0ae06a7

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

examples/advanced/gif-bot.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
"""doc"""
2+
# pylint: disable=R0801
3+
import asyncio
4+
import logging
5+
from typing import Optional, Union
6+
7+
from wechaty_puppet import FileBox, ScanStatus # type: ignore
8+
9+
from wechaty import Wechaty, Contact
10+
from wechaty.user import Message, Room
11+
12+
logging.basicConfig(level=logging.INFO)
13+
log = logging.getLogger(__name__)
14+
15+
16+
def create_gif():
17+
"""
18+
refer to : https://note.nkmk.me/en/python-pillow-gif/
19+
20+
you can create gif file with your own code.
21+
"""
22+
from PIL import Image, ImageDraw
23+
24+
images = []
25+
26+
width = 200
27+
center = width // 2
28+
color_1 = (0, 0, 0)
29+
color_2 = (255, 255, 255)
30+
max_radius = int(center * 1.5)
31+
step = 8
32+
33+
for i in range(0, max_radius, step):
34+
im = Image.new('RGB', (width, width), color_1)
35+
draw = ImageDraw.Draw(im)
36+
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_2)
37+
images.append(im)
38+
39+
for i in range(0, max_radius, step):
40+
im = Image.new('RGB', (width, width), color_2)
41+
draw = ImageDraw.Draw(im)
42+
draw.ellipse((center - i, center - i, center + i, center + i), fill=color_1)
43+
images.append(im)
44+
45+
images[0].save('./bot.gif',
46+
save_all=True, append_images=images[1:], optimize=False, duration=40, loop=0)
47+
48+
49+
class MyBot(Wechaty):
50+
"""
51+
listen wechaty event with inherited functions, which is more friendly for
52+
oop developer
53+
"""
54+
def __init__(self):
55+
super().__init__()
56+
57+
async def on_message(self, msg: Message):
58+
"""
59+
listen for message event
60+
"""
61+
from_contact = msg.talker()
62+
text = msg.text()
63+
room = msg.room()
64+
if text == 'gif':
65+
conversation: Union[
66+
Room, Contact] = from_contact if room is None else room
67+
await conversation.ready()
68+
await conversation.say('dong')
69+
file_box = FileBox.from_file('./bot.gif')
70+
await conversation.say(file_box)
71+
72+
async def on_login(self, contact: Contact):
73+
print(f'user: {contact} has login')
74+
75+
async def on_scan(self, status: ScanStatus, qr_code: Optional[str] = None,
76+
data: Optional[str] = None):
77+
contact = self.Contact.load(self.contact_id)
78+
print(f'user <{contact}> scan status: {status.name} , '
79+
f'qr_code: {qr_code}')
80+
81+
82+
async def main():
83+
"""doc"""
84+
create_gif()
85+
bot = MyBot()
86+
await bot.start()
87+
88+
89+
asyncio.run(main())

0 commit comments

Comments
 (0)